1:系統(tǒng)提供的提示框如下所示:
2: 自定制提示框如下:
3: 所用到的方法
(1)可同時改變字體大小及顏色(第一個參數(shù)為字典店茶,第二個為結構體)
alertTitleStr.addAttributes(<#T##attrs: [String : AnyObject]##[String : AnyObject]#>, range: <#T##NSRange#>)
(2)各自設置字體大小定踱、顏色 (參數(shù)使用見代碼如下)
alertTitleStr.addAttribute(<#T##name: String##String#>, value: <#T##AnyObject#>, range: <#T##NSRange#>)
具體代碼如下:
func createMyAlert(){
let alert = UIAlertController.init(title: "點錯了", message: "只能相鄰的圖交換", preferredStyle: .Alert)
let action = UIAlertAction.init(title: "繼續(xù)", style: .Default, handler: nil)
//1.修改title的字體大小及顏色
let alertTitleStr = NSMutableAttributedString.init(string: "點錯了")
alertTitleStr.addAttributes([NSFontAttributeName : UIFont.boldSystemFontOfSize(25), NSForegroundColorAttributeName: UIColor.redColor()], range: NSRange.init(location: 0, length: 3))
alert.setValue(alertTitleStr, forKey: "attributedTitle")
//2.修改message的字體大小及顏色
let alertMessageStr = NSMutableAttributedString.init(string: "只能相鄰的圖交換")
alertMessageStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.blueColor(), range: NSRange(location: 0, length: 8))
alertMessageStr.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFontOfSize(20), range: NSRange(location: 0, length: 8))
alert.setValue(alertMessageStr, forKey: "attributedMessage")
//3.修改action的顏色 (不能修改action字體大小)
//("_titleTextColor", "titleTextColor"都可以)
//(建議使用“_titleTextColor”, 因為當我們查看 UIAlertAction的屬性列表中并沒有它"titleTextColor",“_titleTextColor”是的它的成員變量列表中的一員。)
action.setValue(UIColor.greenColor(), forKey: "_titleTextColor")
alert.addAction(action)
self.presentViewController(alert, animated: true, completion: nil)
}