[iOS]UIAlertController各标签属性的改变
经常需要设置message左对齐、段间距这些属性
- (IBAction)tapAlertControllerButton:(id)sender { NSString *titleString = @"商家发货规则"; NSString *messageString = @"1、买家下单付款后,请尽快发货;\n2、买家下单付款后,若超过最迟发货的时间,商家仍未发货,那么后台将自动取消订单;\n3、若遇到物流高峰期,比如春节,双十一等,请点击【延迟发货】来延长最迟发货时间;\n4、每个订单只允许【延迟发货】一次,每次可延迟5天;"; NSString *alertTitle = @"知道了"; UIAlertController *_alertController = [UIAlertController alertControllerWithTitle:titleString message:messageString preferredStyle:UIAlertControllerStyleAlert]; [_alertController addAction:[UIAlertAction actionWithTitle:alertTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]]; /* 修改title */ NSMutableAttributedString *attTitleString = [[NSMutableAttributedString alloc] initWithString:titleString]; [_alertController setValue:attTitleString forKey:@"attributedTitle"]; /* 修改message */ NSMutableAttributedString *attMsgString = [[NSMutableAttributedString alloc] initWithString:messageString]; // 设置字体 [attMsgString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, attMsgString.length)]; // 设置颜色 [attMsgString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 10)]; NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init]; // 设置行间距 [paragraph setLineSpacing:3]; // 设置段间距 [paragraph setParagraphSpacingBefore:5]; // 设置对齐方式 [paragraph setAlignment:NSTextAlignmentLeft]; // 设置书写方向 [paragraph setBaseWritingDirection:NSWritingDirectionLeftToRight]; [attMsgString addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, attMsgString.length)]; [_alertController setValue:attMsgString forKey:@"attributedMessage"]; /* 修改按钮的颜色 */ NSArray *actionArr = [_alertController actions]; [actionArr.firstObject setTitle:alertTitle]; [actionArr.firstObject setValue:[UIColor orangeColor] forKey:@"titleTextColor"]; [self presentViewController:_alertController animated:YES completion:nil]; } 示意图:系统默认样式:
改变属性样式: