NSMutableAttributedString的addAttribute的范围适用于所有字符串

问题描述 投票:0回答:1

我已经创建NSMutableAttributedString具有不同属性,然后一个范围I添加的属性用粗字体。例

let attText = NSMutableAttributedString(string: string, attributes: [
            NSAttributedString.Key.font: UIFont.systemFont(ofSize: 15),
            NSAttributedString.Key.foregroundColor: UIColor.Branding.darkText,
            NSAttributedString.Key.paragraphStyle: paragraphStyle
        ])
        if let word = string.components(separatedBy: " ").first {
            let range = attText.mutableString.range(of: word, options: .caseInsensitive)
            if range.location != NSNotFound {
                attText.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 15, weight: .semibold), range: range)
            }
        }

但是,当我启动的应用程序我看到粗体所有的字符串。我看到属性串在调试,它看起来是这样的:

 some : Create{
    NSColor = "UIExtendedSRGBColorSpace 0.247059 0.278431 0.337255 1";
    NSFont = "<UICTFont: 0x7ff1c1c169e0> font-family: \".SFUIText-Semibold\"; font-weight: bold; font-style: normal; font-size: 15.00pt";
}
tasks for people{
    NSColor = "UIExtendedSRGBColorSpace 0.247059 0.278431 0.337255 1";
    NSFont = "<UICTFont: 0x7ff1c1c388d0> font-family: \".SFUIText\"; font-weight: normal; font-style: normal; font-size: 15.00pt";
}

我的理解调试信息是好的,但为什么在iPhone模拟器我看到粗体的所有字符串?

ios swift nsattributedstring nsmutableattributedstring
1个回答
0
投票

请参阅的UILabel的attributedText的文件:

  label.attributedText =  attText
  ``label.font = UIFont.systemFont(ofSize: 15, weight: .semibold)
  print(label.attributedText) 

设置的UILabel的attributedText后,不能更改字体,文字颜色或其他样式相关的属性。否则,你会得到你现在所拥有的。我的猜测是在一些地方有忽视的修改。

© www.soinside.com 2019 - 2024. All rights reserved.