在iOS 8上,有没有办法更改UIAlertController中显示的消息的对齐方式?
我认为访问子视图并为UILabel更改它是不可能的。
发布于 2014-11-16 03:14:11
我已经成功地使用了以下代码来对齐UIAlertControllers的文本并设置其样式:
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.Left
let messageText = NSMutableAttributedString(
string: "The message you want to display",
attributes: [
NSParagraphStyleAttributeName: paragraphStyle,
NSFontAttributeName : UIFont.preferredFontForTextStyle(UIFontTextStyleBody),
NSForegroundColorAttributeName : UIColor.blackColor()
]
)
myAlert.setValue(messageText, forKey: "attributedMessage")
如果您使用"attributedTitle"
而不是"attributedMessage"
,则可以对标题执行类似的操作
Swift 3更新
上面的代码在Swift 3中仍然有效,但代码必须稍微修改为:
let messageText = NSMutableAttributedString(
string: "The message you want to display",
attributes: [
NSParagraphStyleAttributeName: paragraphStyle,
NSFontAttributeName : UIFont.preferredFont(forTextStyle: UIFontTextStyle.body),
NSForegroundColorAttributeName : UIColor.black
]
)
myAlert.setValue(messageText, forKey: "attributedMessage")
Swift 4更新
let messageText = NSMutableAttributedString(
string: "The message you want to display",
attributes: [
NSAttributedStringKey.paragraphStyle: paragraphStyle,
NSAttributedStringKey.font: UIFont.preferredFont(forTextStyle: UIFontTextStyle.body),
NSAttributedStringKey.foregroundColor: UIColor.black
]
)
myAlert.setValue(messageText, forKey: "attributedMessage")
Swift 5更新
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = alignment
let messageText = NSAttributedString(
string: "message",
attributes: [
NSAttributedString.Key.paragraphStyle: paragraphStyle,
NSAttributedString.Key.foregroundColor : UIColor.primaryText,
NSAttributedString.Key.font : UIFont(name: "name", size: size)
]
)
myAlert.setValue(messageText, forKey: "attributedMessage")
发布于 2017-10-13 19:51:48
使用以下代码
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:[title lowercaseString]
message:[message lowercaseString]
preferredStyle:UIAlertControllerStyleAlert];
if (alertStyle == kUIAlertStylePlainText)
{
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
}];
}
if (okTitle) {
UIAlertAction* ok = [UIAlertAction actionWithTitle:[okTitle lowercaseString] style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
callback(alert, action, nil);
}];
[alert addAction:ok];
}
if (cancelTitle) {
UIAlertAction* cancel = [UIAlertAction actionWithTitle:[cancelTitle lowercaseString] style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
callback(alert, nil, action);
}];
[alert addAction:cancel];
}
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
paraStyle.alignment = NSTextAlignmentLeft;
NSMutableAttributedString *atrStr = [[NSMutableAttributedString alloc] initWithString:[message lowercaseString] attributes:@{NSParagraphStyleAttributeName:paraStyle,NSFontAttributeName:[UIFont systemFontOfSize:13.0]}];
[alert setValue:atrStr forKey:@"attributedMessage"];
[viewInstance presentViewController:alert animated:YES completion:nil];
发布于 2019-07-10 22:13:26
我接受了@Amos的回答,并把它变成了一个方便的扩展:
public extension UIAlertController {
func setMessageAlignment(_ alignment : NSTextAlignment) {
let paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
paragraphStyle.alignment = alignment
let messageText = NSMutableAttributedString(
string: self.message ?? "",
attributes: [
NSAttributedString.Key.paragraphStyle: paragraphStyle,
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12),
NSAttributedString.Key.foregroundColor: UIColor.gray
]
)
self.setValue(messageText, forKey: "attributedMessage")
}
}
然后您可以简单地设置对齐值:myAlert.setMessageAlignment(.left)
https://stackoverflow.com/questions/25962559
复制相似问题