zoukankan      html  css  js  c++  java
  • 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 = " " + 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

  • 相关阅读:
    《软件方法》读书笔记2
    《代码阅读方法与实践》读书笔记3
    课堂讨论记录
    《代码阅读方法与实践》读书笔记2
    [洛谷] P1948 [USACO08JAN]Telephone Lines S(二分+SPFA)
    2020 CCPC秦皇岛 正式赛题解
    [洛谷] P3146 [USACO16OPEN]248 G (区间DP)
    [进阶指南] 最大子序和
    [训练] 图的K步移动最大收获
    [计蒜客] 受力平衡(组合数学 + 乘法逆元)
  • 原文地址:https://www.cnblogs.com/qingzZ/p/13390961.html
Copyright © 2011-2022 走看看