【问题标题】:NSMutableAttributedString text color not working correctlyNSMutableAttributedString 文本颜色无法正常工作
【发布时间】:2018-07-16 04:23:38
【问题描述】:

我有以下 swift 4.1 代码:

    let timeduration = NSMutableAttributedString.init(string: "a dynamic string of time");

    timeduration.addAttribute(kCTFontAttributeName as NSAttributedStringKey, value: UIFont.systemFont(ofSize: 16), range: NSRange(location: 0, length: timeduration.length));
    timeduration.addAttribute(kCTForegroundColorAttributeName as NSAttributedStringKey, value: UIColor(red: 200/255, green: 200/255, blue: 200/255, alpha: 1.0), range: NSRange(location: 0, length: timeduration.length));
    timeduration.addAttribute(kCTFontAttributeName as NSAttributedStringKey, value: UIFont.systemFont(ofSize: 50), range: NSRange(location: 0, length: timecount));
    timeduration.addAttribute(kCTFontAttributeName as NSAttributedStringKey, value: UIFont.systemFont(ofSize: 25), range: NSRange(location: timecount, length: 2));

我使用的是 Swift 3 并使用 Xcode 的工具迁移到 swift 4.1,但前景色似乎不起作用。 XCode 工具“修复”添加了 kCT 代码。不过,字体名称和大小似乎可以正常工作。

【问题讨论】:

    标签: ios iphone swift swift4 ios11


    【解决方案1】:

    这里是您的问题的解决方案。在 Swift 4.0 及更高版本中,有一个名为 NSAttributedStringKey 的结构,它定义了字符串属性的键。

    let timeduration = NSMutableAttributedString.init(string: "a dynamic string of time")
    
    timeduration.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize: 16), range: NSRange(location: 0, length: timeduration.length))
    
    timeduration.addAttribute(NSAttributedStringKey.foregroundColor, value: 
    
    UIColor(red: 200/255, green: 200/255, blue: 200/255, alpha: 1.0), range: 
    NSRange(location: 0, length: timeduration.length))
    
    timeduration.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize: 50), range: NSRange(location: 0, length: timecount))
    
    timeduration.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize: 25), range: NSRange(location: timecount, length: 2))
    

    【讨论】:

    • 提高声誉的好方法。很好(Y)
    【解决方案2】:

    fixit 给了你错误的键。你想改用这样的东西:

    timeduration.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.red, range: NSRange(location: 0, length: timeduration.length))
    

    其他的也一样。

    您使用的键用于较低级别的核心文本内容。

    【讨论】:

      【解决方案3】:

      你可以这样做,希望问题能得到解决:-

      let label = UILabel()
      let labelText = "String Text"
      
      let strokeTextAttributes = [
           NSAttributedStringKey.strokeColor : UIColor.black,
           NSAttributedStringKey.foregroundColor : UIColor.white,
           NSAttributedStringKey.strokeWidth : -2.0,
           NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 18)
         ] as [NSAttributedStringKey : Any]
      
      label.attributedText = NSAttributedString(string: labelText, attributes: strokeTextAttributes)
      

      【讨论】:

      • 我试过这个。它只是忽略了foregroundColor 属性。其他一切都有效。你知道为什么吗?
      • 试试这个 NSAttributedStringKey.foregroundColor.rawValue: UIColor.white,@DinuNicolae
      猜你喜欢
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      • 2016-09-06
      • 1970-01-01
      • 2015-09-18
      • 2019-01-05
      • 2023-03-06
      • 2014-11-15
      相关资源
      最近更新 更多