ios 原生的弹窗 messageText默认是居中的,特别丑,UI妹子提了个需求要让弹框里的文字居左对齐,所以在stackOverFlow上查了一下, 原理大概是使用KVC来修改这个样式,别的不多说了上代码:
let tipsMessage = "一只风流的dog一只风流的dog一只风流的dog一只风流的dog\n一只风流的dog一只风流的dog一只风流的dog一只风流的dog一只风流的dog一只风流的dog\n一只风流的dog一只风流的dog一只风流的dog一只风流的dog一只风流的dog"
let myAlert = UIAlertController(title: "", message: tipsMessage, preferredStyle: UIAlertControllerStyle.alert)
let sureAction = UIAlertAction(title: "确认", style: UIAlertActionStyle.default, handler: {[unowned self] (alertAction) in
//do someting...
})
let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.default, handler: {[unowned self] (alertAction) in
//do someting...
})
myAlert.addAction(cancelAction)
myAlert.addAction(sureAction)
let paragraphStyle = NSMutableParagraphStyle() // 注意一定要是mutable!
paragraphStyle.alignment = NSTextAlignment.left // 居左
let messageText = NSMutableAttributedString(
string: tipsMessage,
attributes: [
NSParagraphStyleAttributeName: paragraphStyle,
NSFontAttributeName : UIFont.systemFont(ofSize: 14), // 设置一下字号
NSForegroundColorAttributeName : COLOR_F
]
)
myAlert.setValue(messageText, forKey: "attributedMessage") // 通过KVC 进行修改
self.present(myAlert, animated: true, completion: nil)