Swift 之自定义 UIAlertController

作者: 鑫龙魂 | 来源:发表于2016-09-30 14:14 被阅读1296次

    在学习 Swift 时,想改变一下UIAlertController的显示效果,网上已经有许多热心网友分享的如何自定义UIAlertController.想着就使用 Swift 模仿一下.关于UIAlertController的具体使用在此不再详细的介绍.
    主要实现了UIAlertController中对 title,message 字体样式,大小和颜色的设置以及 UIAlertAction 的title 设置字体颜色及添加背景图等.主要是利用runtime机制获取属性名,关于属性名的获取有个地址: runtime获取类的某些信息
    最终效果图如下:

    自定义UIAlertController效果图.png

    首先创建类继承UIAlertController,在viewDidLoad 中设置UIAlertController的 title 和 message 的相关设置.
    <pre><code>
    //UIFontDescriptorSizeAttribute:"40",设置字体尺寸 ;UIFontDescriptorMatrixAttribute:NSValue.init(cgAffineTransform: .init(rotationAngle: 5))设置字体形变

        let titleFontDescriptor = UIFontDescriptor.init(fontAttributes: [
            UIFontDescriptorFamilyAttribute:"Zapfino",//设置字体家族名
            UIFontDescriptorNameAttribute:"Zapfino",//设置字体的字体名
            ])
        
        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>

    实现对UIAlertAction 字体的设置在创建的类中重写父类的
    func addAction(_ action: UIAlertAction)方法
    <pre><code>

    override func addAction(_ action: UIAlertAction) {

        super.addAction(action)
        //如果不设置 action.setValue(UIColor.purple, forKey:"titleTextColor") 设置 tintColor 也可以实现对颜色的修改
        self.view.tintColor = UIColor.red
        //注意图片的大小,如果太大, action 显示效果为2行
        let image = UIImage.init(named: "dontLike")
        action.setValue(image, forKey: "image")
        //如果设置了则self.view.tintColor 失效.
        action.setValue(UIColor.purple, forKey:"titleTextColor")
    
    }
    

    </code></pre>
    关于 self.view.tintColor ,action 添加图片和设置字体,不同的设置组合会显示的不同效果,有兴趣可以试试看.

    最后关于使用就简单了,和系统的使用方法是一样的,我创建的类的名字是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里面创建的,如果是在controller可以直接使用 self.present(alerView, animated: true, completion: nil)
        let vc = self.menuDelegate as! UIViewController
        vc.present(alerView, animated: true, completion: nil)
    

    </code></pre>

    参考网址:
    http://www.itstrike.cn/Question/e34bccb1-f1ff-4c09-9b1f-8d5f49bfd7fe.html
    http://www.jianshu.com/p/cecd1b4bbf27

    相关文章

      网友评论

        本文标题:Swift 之自定义 UIAlertController

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