iOS开发技术专注iOS开发iOSer 的自我修养

iOS UIAlertController中Message和Ti

2016-05-18  本文已影响7113人  Senior丶
美图镇楼
 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"物品详情" message:@"轻便的移动电源很容易就电量耗尽,而大容量的移动电源往往都比较笨重,这样的体验远远比不上直接更换电池。\n对于许多 iPhone 重度使用者来说,手机续航时间不足是一个一直困扰着他们的问题,当然,从智能手机开始采用不可拆卸电池设计开始,这个问题就已经存在了。在电池技术没有突破的情况下,移动电源成为了解决这个问题的最佳方案。" preferredStyle:UIAlertControllerStyleAlert];
    UIView *subView1 = alertController.view.subviews[0];
    UIView *subView2 = subView1.subviews[0];
    UIView *subView3 = subView2.subviews[0];
    UIView *subView4 = subView3.subviews[0];
    UIView *subView5 = subView4.subviews[0];
    NSLog(@"%@",subView5.subviews);
    //取title和message:
    UILabel *title = subView5.subviews[0];
    UILabel *message = subView5.subviews[1];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil];
    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];

3.控制台打印出subView5.subviews

BE8D95C9-F6A9-4B57-9F7E-F3BA3F101A3E.png
4.这样我们就可以拿到titleLabelmessageLabel来做设置了
    //比如设置message内容居左:
    message.textAlignment = NSTextAlignmentLeft;
    title.textAlignment = NSTextAlignmentLeft;
//修改title
    NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:@"物品详情"];
    [alertControllerStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
    [alertControllerStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, 2)];
    [alertController setValue:alertControllerStr forKey:@"attributedTitle"];
    
    //修改message
    NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:@"轻便的移动电源很容易就电量耗尽,而大容量的移动电源往往都比较笨重,这样的体验远远比不上直接更换电池。"];
    [alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, 10)];
    [alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(11, 20)];
    [alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(20, 30)];
    [alertController setValue:alertControllerMessageStr forKey:@"attributedMessage"];
//修改按钮的颜色
    [cancelAction setValue:[UIColor orangeColor] forKey:@"titleTextColor"];
3.这个方法其实就是通过KVC来实现的.如果不懂得KVC可以看看iOS KVC简单理iOS 用KVC来自定义Tabbar,如果不清楚富文本的可以看iOS 富文本.
上一篇下一篇

猜你喜欢

热点阅读