【问题标题】:Swift 5.5 AttributedString doesn't show in interfaceSwift 5.5 AttributedString 未显示在界面中
【发布时间】:2021-10-31 23:43:28
【问题描述】:

这是 Swift 5.5 制作属性字符串的方法:

let s = "Hello"
var attrib = AttributedString(s)
if let range = attrib.range(of: "ell") {
    attrib[range].foregroundColor = .red
    self.label.attributedText = NSAttributedString(attrib)
}

问题是标签没有显示任何红色字符。事实上,我可以打印self.label.attributedText,我可以看到红色属性甚至没有进入NSAttributedString。怎么回事?

【问题讨论】:

    标签: swift nsattributedstring ios15


    【解决方案1】:

    默认情况下,Swift 认为您正在应用 SwiftUI 属性,而 UIKit 不理解这些属性。您必须指定这是什么种类属性字符串属性:

    attrib[range].uiKit.foregroundColor = .red
    //            ^^^^^
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-09
      • 2012-03-31
      • 2017-02-10
      • 2014-12-27
      • 2021-03-16
      • 1970-01-01
      • 2012-12-13
      • 1970-01-01
      相关资源
      最近更新 更多