简体   繁体   English

NSMutableAttributedString 文本颜色无法正常工作

[英]NSMutableAttributedString text color not working correctly

I have the following swift 4.1 code:我有以下 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));

I was using Swift 3 and did the migration using Xcode's tool to swift 4.1 but the foreground color doesn't appear to be working.我使用的是 Swift 3 并使用 Xcode 的工具迁移到 swift 4.1,但前景色似乎不起作用。 The XCode tool "Fix" added the kCT codes. XCode 工具“修复”添加了 kCT 代码。 The font names and sizes seem to be working though.字体名称和大小似乎可以正常工作。

Here is the solution to your problem.这是您的问题的解决方案。 In Swift 4.0 and later, there is struct called NSAttributedStringKey which define the keys for string attributes.在 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))

The fixit has given you the wrong keys. fixit 给了你错误的键。 You want to be using something like this instead:你想改用这样的东西:

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

Same goes for all of the others.所有其他人也是如此。

The keys you are using are for lower level core text stuff.您使用的键用于较低级别的核心文本内容。

You can do this way, i hope the problem will be solved:-你可以这样做,我希望问题能得到解决:-

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM