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("取消")

        }

 

posted @ 2017-08-07 18:10  LiziBlog  阅读(431)  评论(0编辑  收藏  举报