美文网首页iOSiOS Developer
iOS[swift] 修改UIAlertController的m

iOS[swift] 修改UIAlertController的m

作者: 一只风流的dog | 来源:发表于2017-05-17 16:49 被阅读0次

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)

相关文章

网友评论

    本文标题:iOS[swift] 修改UIAlertController的m

    本文链接:https://www.haomeiwen.com/subject/qiidxxtx.html