swift UIAlertController UIAlertAction設置 font,color


 

            let alert = UIAlertController(title: "", message: "", preferredStyle: UIAlertController.Style.alert)
            
            ///標題顏色/字體
            let titleAtt = NSMutableAttributedString(string: "刪除會員卡", attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.regular),
                NSAttributedString.Key.foregroundColor:UIColor.init(hexString: "#000000")])
            alert.setValue(titleAtt, forKey: "attributedTitle")
      

      ///設置字體alignment 方法1 有問題, 漢字和字母混合系統自動換行

        let paragraph = NSMutableParagraphStyle()
        paragraph.alignment = .left
        alertControllerMessageStr.setAttributes([
        NSAttributedString.Key.paragraphStyle: paragraph
        ], range: NSRange(location: 0, length: alertControllerMessageStr.length))
        alert.setValue(alertControllerMessageStr, forKey: "attributedMessage")

      //設置字體2 
      

            let subView1 = alert.view.subviews[0]

 
         

            let subView2 = subView1?.subviews[0]

 
         

            let subView3 = subView2?.subviews[0]

 
         

            let subView4 = subView3?.subviews[0]

 
         

            let subView5 = subView4?.subviews[0]

 
         

            var title_lb: UILabel?

 
         

            var message_lb: UILabel?

 
         

 

 
         

            ///subView5.subviews[2] 是label ,就是真機的

 
         

            if subView5?.subviews[2].classForCoder == UILabel.classForCoder {

 
         

                ///真機subView5.subviews.count 是5

 
         

                title_lb = subView5?.subviews[1] as? UILabel ///title

 
         

                message_lb = subView5?.subviews[2] as? UILabel ///message

 
         

            } else {

 
         

                //模擬器 subView5.subviews.count 是4

 
         

                title_lb = subView5?.subviews[0] as? UILabel ///title

 
         

                message_lb = subView5?.subviews[1] as? UILabel ///message

 
         

            }

            title_lb?.textAlignment = .right

            message_lb?.textAlignment = textAlignment

 

            
//message 字體/顏色 let messageStr = "\n" + tipTuple.tipStr let messageAtt = NSMutableAttributedString(string: messageStr, attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular), NSAttributedString.Key.foregroundColor:UIColor.init(hexString: "#9B9B9B")]) alert.setValue(messageAtt, forKey: "attributedMessage") ///action 顏色 let leftAction = UIAlertAction(title: "我知道了", style: UIAlertAction.Style.default, handler: nil) leftAction.setValue(main_tinColor, forKey: "titleTextColor") alert.addAction(leftAction) self.present(alert, animated: true, completion: nil)

 

 

參考鏈接

https://blog.csdn.net/jacob_ios/article/details/52767411?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM