美文网首页
改变UILabel部分颜色属性以及UIAlertControll

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

作者: biny_ios | 来源:发表于2018-01-09 09:21 被阅读17次

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"];
}

相关文章

网友评论

      本文标题:改变UILabel部分颜色属性以及UIAlertControll

      本文链接:https://www.haomeiwen.com/subject/wbqwnxtx.html