NSAttributedString not working as expected in iOS 13

核能气质少年 提交于 2019-12-24 20:09:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!