NSAttributedString无法在iOS 13中按预期方式工作 [英] NSAttributedString not working as expected in iOS 13
问题描述
我正在使用NSMutableAttributedString在标签中显示多种字体和颜色的文本.NSMutableAttributedString在iOS 13中无法正常工作,但是相同的代码在iOS 11和12版本中也可以正常工作.
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
推荐答案
问题是您没有在样本"和描述"之间附加空白的任何属性.尝试为其提供一些属性...
The issue is that you don't have any attributes for the white space between "Sample" and "Description" which you appended in between them. Try to give some attributes for it ...
hdAttributedText.append(NSAttributedString(string: " ", 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)]))
尝试这样的事情.
这篇关于NSAttributedString无法在iOS 13中按预期方式工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!