在學(xué)習(xí) Swift 時,想改變一下UIAlertController的顯示效果,網(wǎng)上已經(jīng)有許多熱心網(wǎng)友分享的如何自定義UIAlertController.想著就使用 Swift 模仿一下.關(guān)于UIAlertController的具體使用在此不再詳細的介紹.
主要實現(xiàn)了UIAlertController中對 title,message 字體樣式,大小和顏色的設(shè)置以及 UIAlertAction 的title 設(shè)置字體顏色及添加背景圖等.主要是利用runtime機制獲取屬性名,關(guān)于屬性名的獲取有個地址: runtime獲取類的某些信息
最終效果圖如下:

首先創(chuàng)建類繼承UIAlertController,在viewDidLoad 中設(shè)置UIAlertController的 title 和 message 的相關(guān)設(shè)置.
<pre><code>
//UIFontDescriptorSizeAttribute:"40",設(shè)置字體尺寸 ;UIFontDescriptorMatrixAttribute:NSValue.init(cgAffineTransform: .init(rotationAngle: 5))設(shè)置字體形變
let titleFontDescriptor = UIFontDescriptor.init(fontAttributes: [
UIFontDescriptorFamilyAttribute:"Zapfino",//設(shè)置字體家族名
UIFontDescriptorNameAttribute:"Zapfino",//設(shè)置字體的字體名
])
let titleFont = UIFont.init(descriptor: titleFontDescriptor, size: 20.0)
let titleAttribute = NSMutableAttributedString.init(string: self.title!)
titleAttribute.addAttributes([NSFontAttributeName:titleFont,
NSForegroundColorAttributeName:UIColor.red],
range:NSMakeRange(0, (self.title?.characters.count)!))
self.setValue(titleAttribute, forKey: "attributedTitle")
//message
let messageFontDescriptor = UIFontDescriptor.init(fontAttributes: [
UIFontDescriptorFamilyAttribute:"Papyrus",
UIFontDescriptorNameAttribute:"Papyrus",
])
let messageFont = UIFont.init(descriptor: messageFontDescriptor, size: 15.0)
let messageAttribute = NSMutableAttributedString.init(string: self.message!)
messageAttribute.addAttributes([NSFontAttributeName:messageFont,
NSForegroundColorAttributeName:RGBA(20, G: 150, B: 55, A: 1)],
range:NSMakeRange(0, (self.message?.characters.count)!))
self.setValue(messageAttribute, forKey: "attributedMessage")
</code></pre>
實現(xiàn)對UIAlertAction 字體的設(shè)置在創(chuàng)建的類中重寫父類的
func addAction(_ action: UIAlertAction)方法
<pre><code>
override func addAction(_ action: UIAlertAction) {
super.addAction(action)
//如果不設(shè)置 action.setValue(UIColor.purple, forKey:"titleTextColor") 設(shè)置 tintColor 也可以實現(xiàn)對顏色的修改
self.view.tintColor = UIColor.red
//注意圖片的大小,如果太大, action 顯示效果為2行
let image = UIImage.init(named: "dontLike")
action.setValue(image, forKey: "image")
//如果設(shè)置了則self.view.tintColor 失效.
action.setValue(UIColor.purple, forKey:"titleTextColor")
}
</code></pre>
關(guān)于 self.view.tintColor ,action 添加圖片和設(shè)置字體,不同的設(shè)置組合會顯示的不同效果,有興趣可以試試看.
最后關(guān)于使用就簡單了,和系統(tǒng)的使用方法是一樣的,我創(chuàng)建的類的名字是MPAlertViewController
<pre><code>
let alerView = MPAlertViewController.init(title: "title title", message: "message message", preferredStyle:UIAlertControllerStyle.alert)
alerView.addAction(UIAlertAction.init(title: "Cancel", style: UIAlertActionStyle.default, handler: { (UIAlertAction) in
}))
alerView.addAction(UIAlertAction.init(title: "Ok", style: UIAlertActionStyle.default, handler: { (UIAlertAction) in
}))
//我是在一個View里面創(chuàng)建的,如果是在controller可以直接使用 self.present(alerView, animated: true, completion: nil)
let vc = self.menuDelegate as! UIViewController
vc.present(alerView, animated: true, completion: nil)
</code></pre>
參考網(wǎng)址:
http://www.itstrike.cn/Question/e34bccb1-f1ff-4c09-9b1f-8d5f49bfd7fe.html
http://m.itdecent.cn/p/cecd1b4bbf27