zoukankan      html  css  js  c++  java
  • Swift 封装AleartController

    import UIKit
    
    
    class Healp: NSObject {
     //alertController title message messageAlignment message对齐方式 messageLineSpacing文字行间距  messgageFont message字体大小 sureText确认按钮文字 sureColor确认按钮颜色 canclText取消文字cancleColor取消字体颜色 vc 所在控制器 sureAction点击方法  canclAction取消点击方法
        class func customAlertC(title:String , message:String ,messageColor:UIColor ,messageAlignment:NSTextAlignment ,messageLineSpacing:CGFloat ,messgageFont:CGFloat  , sureText:String , sureColor:UIColor , canclText:String ,  cancleColor:UIColor ,  vc:UIViewController ,sureAction:@escaping (_ sure :UIAlertAction ) -> () , canclAction :@escaping (_ cancle:UIAlertAction) ->()){
            
            
            let  aleartC = UIAlertController (title:title , message:message , preferredStyle:UIAlertControllerStyle.alert)
            /*
             设置message行间距 对齐方式  字体大小 字体颜色
             */
            
            let   paragraphStyle = NSMutableParagraphStyle ()
            paragraphStyle.alignment = messageAlignment
            paragraphStyle.lineSpacing = messageLineSpacing
            
            let attributes : NSDictionary = [ NSFontAttributeName : UIFont .systemFont(ofSize: messgageFont) , NSParagraphStyleAttributeName:paragraphStyle , NSForegroundColorAttributeName:messageColor]
            let mutableString = NSAttributedString(string:message , attributes:attributes as? [String : Any])
            aleartC .setValue(mutableString, forKey: "attributedMessage")
            
            
            
            /*
             let   sureAction  = UIAlertAction (title:sureText , style:UIAlertActionStyle.destructive) {(UIAlertAction)-> Void in
     
                  sureAction (UIAlertAction)
             }
             let   cancleAction  = UIAlertAction (title:canclText , style:UIAlertActionStyle.cancel) {(UIAlertAction)-> Void in
     
                 canclAction (UIAlertAction)
             }
             */
            
            //上两句和下边这两句作用相同
            //确定按钮
            let  sureAction   = UIAlertAction (title:sureText ,style:UIAlertActionStyle.default ,handler:sureAction)
            sureAction .setValue(sureColor, forKey: "_titleTextColor")
            
            //取消按钮
            let  cancleAction = UIAlertAction (title:canclText ,style:UIAlertActionStyle.cancel ,handler:canclAction)
             cancleAction .setValue(cancleColor, forKey: "_titleTextColor")
        
            
            aleartC.addAction(sureAction)
            aleartC.addAction(cancleAction)
            
            vc.present(aleartC,animated:true,completion:nil)
        }
    
    
    }

    调用

      Healp .customAlertC(title: "提示", message: "哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈",messageColor:UIColor.blue ,messageAlignment:NSTextAlignment.center ,messageLineSpacing:7 ,messgageFont:14  , sureText: "确定", sureColor:UIColor.red  , canclText: "取消", cancleColor: UIColor.green, vc: self, sureAction: { (action) in
                 print("确定")
            }) { (action) in
                print("取消")
    
            }
  • 相关阅读:
    搭建Git服务器
    shell脚本的使用
    谈谈递归和回溯算法的运用
    给 Qt 添加模块
    QtQuick 中的 qml 与 Qt 的 C++
    QT 中使用 c++ 的指针
    QT 的使用及编写代码遇到的问题和解决方法
    Centos 7 上安装使用 vscode
    PHP 数组转json格式,key的保存问题
    PHP compact
  • 原文地址:https://www.cnblogs.com/Lrx-lizi/p/7300539.html
Copyright © 2011-2022 走看看