UIAlertController文本对齐 [英] UIAlertController text alignment
本文介绍了UIAlertController文本对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法在iOS 8上更改UIAlertController内显示的消息的对齐方式?
Is there a way to change the alignment of the message displayed inside a UIAlertController on iOS 8?
我认为访问子视图并为UILabel更改它不是可能了。
I believe accessing the subviews and changing it for the UILabel is not possible anymore.
推荐答案
我已成功使用以下内容,用于对齐和设计UIAlertControllers的文本:
I have successfully used the following, for both aligning and styling the text of 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")
你可以用t做类似的事情itle,如果你使用attributionTitle
,而不是attributionMessage
You can do a similar thing with the title, if you use "attributedTitle"
, instead of "attributedMessage"
以上内容仍适用于Swift 3,但代码必须略微修改为:
The above still works in Swift 3, but the code has to be slightly altered to this:
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 update
Swift 4 update
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")
这篇关于UIAlertController文本对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文