问题
I am using NSMutableAttributedString to show multi font and colour text in a label. NSMutableAttributedString is not working as expected in iOS 13, but same code works fine in iOS 11 and 12 versions.
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
Expected result is "Sample Description". In this text "Sample" should be in regular font with black colour text and "Description" should be in medium font with green colour
回答1:
On iOS 13 the attributed string isn't working in tableviews, fortunately i know a workaround for this and it'll work like a charm.
All you need to do is to make the attributed string run on main thread (even if you're setting the attributed string in awakeFromNib) and it is as follows :
DispatchQueue.main.async {
//set your attributed text here
}
来源:https://stackoverflow.com/questions/58316790/nsattributedstring-not-working-as-expected-in-ios-13