想要给UIAlertController title 设置颜色。首先看了UIAlertController的属性,并没有对title颜色对应的属性。然后通过runtime获取了其多有属性发现,UIAlertController有一个私有属性attributedTitle。然后通过富文本实现。方法如下:
-(void)alertWithTitle:(NSString*)title andMessage:(NSString*)message{
UIAlertController*alertVC=[UIAlertControlleralertControllerWithTitle:@""message:messagepreferredStyle:(UIAlertControllerStyleAlert)];
NSDictionary* dic=@{NSForegroundColorAttributeName:[UIColorredColor],
NSFontAttributeName:[UIFontsystemFontOfSize:17],
NSExpansionAttributeName:@(0.2),
};
NSMutableAttributedString*attributedStr=[[NSMutableAttributedStringalloc]initWithString:titleattributes:dic];
[alertVCsetValue:attributedStrforKey:@"attributedTitle"];
UIAlertAction*ac=[UIAlertActionactionWithTitle:@"取消"style:(UIAlertActionStyleDefault)handler:^(UIAlertAction*_Nonnullaction) {
}];
[alertVCaddAction:ac];
[selfpresentViewController:alertVCanimated:YEScompletion:nil];
}
网友评论