当前位置: > > > Swift - 自定义UIAlertController的样式1(修改标题、按钮的字体和颜色)

Swift - 自定义UIAlertController的样式1(修改标题、按钮的字体和颜色)

iOS8 起,苹果把 UIActionSheet UIAlertView 合并为了 UIAlertControllerUIAlertController 的用法我之前也写过相关文章:Swift - 告警提示框(UIAlertController)的用法。其默认样式如下:

有网友问这个 UIAlertController 默认的样式能不能修改。虽然 UIAlertController 没有直接提供相关的属性或方法来修改样式,但我们可以通过 KVC 机制(在运行时动态访问和修改对象的属性)来实现。

一、修改标题的字体和颜色

1,效果图

(1)标题抬头颜色改成红色,字号放大(20
(2)标题内容颜色改成灰色,并使用斜体字。

2,自定义UIAlertController类

为方便使用,我们这里通过继承 UIAlertController 实现一个自定义的类:MyAlertController。并在其 viewDidLoad 方法中动态改变标题样式。
import UIKit

class MyAlertController: UIAlertController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //标题字体样式(红色,字体放大)
        let titleFont = UIFont.systemFont(ofSize: 20)
        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")
        
        //消息内容样式(灰色斜体)
        let messageFontDescriptor = UIFontDescriptor.init(fontAttributes: [
            UIFontDescriptorFamilyAttribute:"Arial",
            UIFontDescriptorNameAttribute:"Arial-ItalicMT",
            ])
        
        let messageFont = UIFont.init(descriptor: messageFontDescriptor, size: 13.0)
        let messageAttribute = NSMutableAttributedString.init(string: self.message!)
        messageAttribute.addAttributes([NSFontAttributeName:messageFont,
                                        NSForegroundColorAttributeName:UIColor.gray],
                                    range:NSMakeRange(0, (self.message?.characters.count)!))
        self.setValue(messageAttribute, forKey: "attributedMessage")
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

3,使用样例

import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewDidAppear(_ animated: Bool){
        super.viewDidAppear(animated)
        
        let alertController = MyAlertController(title: "系统提示",
                                                message: "您确定要离开 hangge.com 吗?",
                                                preferredStyle: .alert)
        let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        let okAction = UIAlertAction(title: "好的", style: .default, handler: {
            action in
            print("点击了确定")
        })
        alertController.addAction(cancelAction)
        alertController.addAction(okAction)
        self.present(alertController, animated: true, completion: nil)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

二、修改按钮的样式

1,修改按钮颜色

(1)下面我们将按钮的颜色改成橙色。

(2)为方便使用,我们这里通过继承 UIAlertController 实现一个自定义的类:MyAlertController。并在其 addAction 方法中动态改变按钮样式。
import UIKit

class MyAlertController: UIAlertController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func addAction(_ action: UIAlertAction) {
        super.addAction(action)
        //通过tintColor实现按钮颜色的修改。
        self.view.tintColor = UIColor.orange
        //也可以通过设置 action.setValue 来实现
        //action.setValue(UIColor.orange, forKey:"titleTextColor")
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

2,在按钮上添加图标

(1)下面我们在确定和取消按钮上分别添加相应的图标,同时两个按钮的文字颜色也不一样。

(2)同样,我们在 UIAlertController addAction 方法中动态改变按钮样式。
import UIKit

class MyAlertController: UIAlertController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func addAction(_ action: UIAlertAction) {
        super.addAction(action)
        
        //设置确定按钮图标和样式
        if action.style == .default {
            //使用图片原来颜色
            let iconImage = UIImage(named:"tick")?.withRenderingMode(.alwaysOriginal)
            action.setValue(iconImage, forKey: "image")
            action.setValue(UIColor.green, forKey:"titleTextColor")
            
        }
        //设置取消按钮图片和样式
        else if  action.style == .cancel {
            let iconImage = UIImage(named:"multiply")?.withRenderingMode(.alwaysOriginal)
            action.setValue(iconImage, forKey: "image")
            action.setValue(UIColor.red, forKey:"titleTextColor")
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
评论5
  • 5楼
    2017-12-24 14:51
    77Timor

    写得很不错,要是每一篇文章都直接给出demo,那就舒服了,我是自学的

    站长回复

    多谢夸奖。一般来说如果代码比较少,或者比较简单的话我就不放demo了。其它情况我都会有完整的demo的。

  • 4楼
    2017-07-27 16:01
    YaruQ

    如果改变按钮的字体怎么写?

    站长回复

    按钮字体我也不知道怎么修改,如果你找到方法可以给我留言,我更新到文章中。

  • 3楼
    2017-05-08 16:24
    老沙

    setValue这个方法在iOS8上会crash;

    站长回复

    我找了台iOS8的手机测试了下,没有问题啊?

  • 2楼
    2017-05-02 17:15
    小曹

    航哥,请教一个问题,就是preferredStyle: .actionSheet 的时候可以自定义的从屏幕顶部弹出警告框吗?

    站长回复

    这个应该实现不了,只能自己写一个弹出框或者看看有没有第三方组件。

  • 1楼
    2017-04-22 16:46
    白色天空729

    哈哈哈 这个6
    省的自定义控件了 多谢 站长

    站长回复

    不客气,很高兴我的文章能帮助到你。