- (void) showAlertWithMessage:(NSString *) message{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"关于开播提醒" message:message preferredStyle:UIAlertControllerStyleAlert];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
//paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentLeft;
//行间距
paragraphStyle.lineSpacing = 5.0;
NSDictionary * attributes = @{NSFontAttributeName : [UIFont systemFontOfSize:14.0], NSParagraphStyleAttributeName : paragraphStyle};
NSMutableAttributedString *attributedTitle = [[NSMutableAttributedString alloc] initWithString:message];
[attributedTitle addAttributes:attributes range:NSMakeRange(0, message.length)];
// 下面这句话用了KVC ,将attributes的值给了alert
[alertController setValue:attributedTitle forKey:@"attributedMessage"];//attributedTitle\attributedMessage
//end ---
// UIAlertAction *defaultAction1 = [UIAlertAction actionWithTitle:@"cancel"
// style: UIAlertActionStyleDefault
// handler:^(UIAlertAction *action) {
// UITextField *textField = alertController.textFields[0];
// NSLog(@"text was %@", textField.text);
// }];
UIAlertAction *defaultAction2 = [UIAlertAction actionWithTitle:@"确定"
style: UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[alertController dismissViewControllerAnimated:YES completion:nil];
}];
// [alertController addAction:defaultAction1];
[alertController addAction:defaultAction2];
UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
[rootViewController presentViewController:alertController animated: YES completion: nil];
}
自定义alert文本内容左对齐
最新推荐文章于 2021-06-10 16:30:49 发布
本文展示了如何在iOS开发中自定义UIAlertController,使消息内容左对齐,并设置行间距。通过创建NSMutableParagraphStyle,设置对其方式和行间距,然后应用到 attributedMessage 属性上,从而实现自定义样式。
摘要由CSDN通过智能技术生成