NSAttributedString在iOS 13中无法正常工作

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

我正在使用NSMutableAttributedString在标签中显示多种字体和颜色的文本。 NSMutableAttributedString在iOS 13中无法正常工作,但是相同的代码在iOS 11和12版本中也可以正常工作。

let hdAttributedText = NSMutableAttributedString(string: "Sample", attributes: [NSAttributedString.Key.font: UIFont(name: "HelveticaNeue", size: 14.0)!, NSAttributedString.Key.foregroundColor: UIColor.black])
hdAttributedText.append(NSAttributedString(string: " "))
hdAttributedText.append(NSAttributedString(string: "Description", attributes: [NSAttributedString.Key.font: UIFont(name: "HelveticaNeue-Medium", size: 14.0)!, NSAttributedString.Key.foregroundColor: UIColor(red: 0.29, green: 0.70, blue: 0.36, alpha: 1)]))
logoTextLabel.attributedText = hdAttributedText

预期结果是“样品描述”。在此文本中,“样本”应为黑色文本的常规字体,“描述”应为绿色的中等字体

swift xcode nsattributedstring ios13 nsmutableattributedstring
1个回答
0
投票

[在iOS 13上,属性字符串在表视图中不起作用,所幸我知道此问题的解决方法,它会像一种魅力一样工作。

您要做的就是使属性字符串在主线程上运行(即使您在awakeFromNib中设置属性字符串),如下所示:

DispatchQueue.main.async {
                     //set your attributed text here
         }
© www.soinside.com 2019 - 2024. All rights reserved.