swift 自定义UIAlertController

一. 自定义UIAlertController的title和message

1.系统弹窗的title和message一般满足不了项目中的要求,下面来介绍一下如何修改title和message的字体以及颜色,首先我们对UIAlertController进行一下封装:代码如下:

import UIKit

class THCBaseAlertViewController: UIAlertController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //标题字体样式
        let  titleFontDescriptor =  UIFontDescriptor . init (fontAttributes: [
            UIFontDescriptor.AttributeName.name : "PingFangSC-Medium" ,
        ])
        let  titleFont =  UIFont . init (descriptor: titleFontDescriptor, size: 15.0)
        let  titleAttribute =  NSMutableAttributedString . init (string: self .title!)
        titleAttribute.addAttributes([NSAttributedString.Key.font :titleFont,
                                      NSAttributedString.Key.foregroundColor : UIColor(r: 34, g: 34, b: 34)],
                                              range: NSMakeRange (0, ( self .title!.count)))
        self .setValue(titleAttribute, forKey:  "attributedTitle")
        
        //消息内容样式
        let  messageFontDescriptor =  UIFontDescriptor . init (fontAttributes: [
            UIFontDescriptor.AttributeName.name : "PingFangSC-Regular" ,
        ])
        let  messageFont =  UIFont . init (descriptor: messageFontDescriptor, size: 14.0)
        let  messageAttribute =  NSMutableAttributedString . init (string:  self .message!)
        messageAttribute.addAttributes([ NSAttributedString.Key.font :messageFont,
                                         NSAttributedString.Key.foregroundColor : UIColor(r: 34, g: 34, b: 34)],
                                       range: NSMakeRange (0, ( self .message!.count)))
        self .setValue(messageAttribute, forKey:  "attributedMessage" )
        
    }

如上代码成功改变了字体的颜色和大小

使用如下:

let alertVC:THCBaseAlertViewController = THCBaseAlertViewController(title: "提示", message: "您还不是会员,无法发布广告", preferredStyle: .alert)

二.自定义UIAlertController按钮的颜色,同样是在封装的这个类中,代码如下:

override  func  addAction(_ action:  UIAlertAction ) {
             super .addAction(action)
             
             if  action.style == . cancel  {
                action.setValue( UIColor(r: 34, g: 34, b: 34), forKey: "titleTextColor" )
             }
             else  if   action.style == .default {
                 action.setValue( UIColor(r: 255, g: 160, b: 0), forKey: "titleTextColor" )
             }
         }

注意一下按钮的style,在使用时会用到,使用示例代码如下:

        let alertVC:THCBaseAlertViewController = THCBaseAlertViewController(title: "提示", message: "您还不是会员,无法发布广告", preferredStyle: .alert)
        let NOButton:UIAlertAction = UIAlertAction(title: "取消", style: .cancel) { (UIAlertAction) in
        }
        let YESButton:UIAlertAction = UIAlertAction(title: "前往会员页", style: .default) { (UIAlertAction) in
            print("前往会员页")
        }
        
        alertVC .addAction(NOButton)
        alertVC .addAction(YESButton)
        self .present(alertVC, animated: true) {
        }

如上代码,style 要对应,这样按钮颜色才会对应起来

三.message文字左对齐
系统自带的message文字默认居中对齐,但是项目中会要求文字左对齐,代码如下:

let messageLabel:UILabel = alertVC.view.value(forKeyPath: "_messageLabel") as! UILabel
messageLabel.textAlignment = .left

这样message的文字就左对齐了,以上就是关于UIAlertController的简单封装!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容