改变UILabel部分颜色属性以及UIAlertController

1.改变label的部分颜色

 UILabel  *textLabel = [UILabel alloc] init];                                                                                                    
 textLabel.textColor = TextGrayColor;
 textLabel.font = [UIFont systemFontOfSize:12];
 NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"网址是:https://www.baidu.com"];
//更改字体  
 [string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:20] range:NSMakeRange(4, 21)];  
//修改颜色
 [string addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(4, 21)];
 textLabel.attributedText = string;

2.改变UIAlertController的控件属性颜色

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"提示内容" preferredStyle:UIAlertControllerStyleAlert];
//UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"提示内容" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Default" style:UIAlertActionStyleDefault handler:nil];  
UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:@"Destructive" style:UIAlertActionStyleDestructive handler:nil];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];

[alertController addAction:defaultAction];
[alertController addAction:destructiveAction];
[alertController addAction:cancelAction];

[self presentViewController:alertController animated:YES completion:nil];


//修改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, 4)];
[alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 4)];
[alertController setValue:alertControllerMessageStr forKey:@"attributedMessage"];
//修改按钮
if (cancelAction valueForKey:@"titleTextColor") {
    [cancelAction setValue:[UIColor redColor] forKey:@"titleTextColor"];
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,049评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,269评论 4 61
  • 1、窗体 1、常用属性 (1)Name属性:用来获取或设置窗体的名称,在应用程序中可通过Name属性来引用窗体。 ...
    Moment__格调阅读 4,614评论 0 11
  • 由于没注意看通知,不知道小区停天然气,结果临时被动了,女儿说:“我好像有看到。”我说:“那你为什么不早说呢?...
    不知有汉骥无论魏晋章阅读 85评论 0 0
  • 这世界上 有三件事永远无法隐瞒: 咳嗽、贫穷和爱 当你对一个人有了爱意 即使捂住了嘴巴 也会从眼睛里冒出来 万千心...
    权权大大阅读 1,392评论 0 1