Swift 之自定義 UIAlertController

在學(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獲取類的某些信息
最終效果圖如下:

自定義UIAlertController效果圖.png

首先創(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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容