0

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

Suresh Vutukuru
  • 211
  • 2
  • 8
  • What do you mean by "is not working as expected"? What does it show up as? What is `UIFont.eX_Regular_14`? – Sweeper Oct 10 '19 at 07:05
  • @Sweeper I am using above code to show label text as two different fonts and colors, but its not working. Same code working fine in iOS 11 & 12 versions... UIFont.eX_Regular_14 is my UIFont extension – Suresh Vutukuru Oct 10 '19 at 07:08
  • _**How**_ is it not working? What is the actual result of running that code? Can you show how `UIFont.eX_Regular_14` is implemented? – Sweeper Oct 10 '19 at 07:10
  • static var eX_Regular_14: UIFont {return UIFont(name: "PFDinTextArabic-Regular", size: viewWidthPercent(percent: 3.73))!} – Suresh Vutukuru Oct 10 '19 at 07:11
  • You should [edit] that info into your question, and provide a [mcve]. You still haven't said how is it not working. – Sweeper Oct 10 '19 at 07:12
  • @Sweeper I have edited my code. Now I changed my font to "HelveticaNeue". My requirement is label text should display in multi color – Suresh Vutukuru Oct 10 '19 at 07:26
  • Could you print the `hdAttributedText` in iOS11 & 13 ? – Larme Oct 10 '19 at 08:01
  • @Larme iOS 12.4 Sample{ NSColor = "UIExtendedGrayColorSpace 0 1"; NSFont = " font-family: \"Helvetica Neue\"; font-weight: normal; font-style: normal; font-size: 14.00pt"; } { }Description{ NSColor = "UIExtendedSRGBColorSpace 0.29 0.7 0.36 1"; NSFont = " font-family: \"HelveticaNeue-Medium\"; font-weight: normal; font-style: normal; font-size: 14.00pt"; } – Suresh Vutukuru Oct 10 '19 at 09:24
  • @Larme iOS 13.1 Sample{ NSColor = "UIExtendedGrayColorSpace 0 1"; NSFont = " font-family: \"Helvetica Neue\"; font-weight: normal; font-style: normal; font-size: 14.00pt"; } { }Description{ NSColor = "UIExtendedSRGBColorSpace 0.29 0.7 0.36 1"; NSFont = " font-family: \"HelveticaNeue-Medium\"; font-weight: normal; font-style: normal; font-size: 14.00pt"; } – Suresh Vutukuru Oct 10 '19 at 09:24
  • Edit your question with that log, but seems equals. So it’s about the rendering then? – Larme Oct 10 '19 at 09:25
  • @Larme Have you tried above code to show multi color text in label in iOS 13 ? – Suresh Vutukuru Oct 10 '19 at 09:28

3 Answers3

4

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
         }
Noob in Swift
  • 113
  • 1
  • 12
  • I dont think this is correct, if you build new project and run it there with only that it will work. – Markicevic Dec 12 '19 at 14:33
  • I am working in a billion dollar company and i have resolved this issue there. It's not about thinking but it's about implementing what i have told here. No wonder why people are downvoting. – Noob in Swift Dec 13 '19 at 15:03
  • This is a good solution although, not completely clear why. While trying to change the text in viewDidLoad we suppose to be on the serial queue. This is just weird – Oz Shabat Jan 19 '22 at 11:16
0

Extension for: Noob in Swift

in iOS 13.0+

// DECLARE

extension String {
    func htmlToAttributedString(_ completeHandler: @escaping (NSAttributedString?) -> Void) {
        if let data = data(using: .unicode) {
            DispatchQueue.main.async {
                do {
                    completeHandler(try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.unicode.rawValue], documentAttributes: nil))
                } catch {
                    completeHandler(nil)
                }
            }
        } else { completeHandler(nil) }
    }
}

// USE

htmlString.htmlToAttributedString { NSAttributedString in
    if let NSAttributedString = NSAttributedString {
        self.m_Detail_Lbl.attributedText = NSAttributedString
    } else {
        // error handler
    }
}
-1

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)]))

Try something like this..

David Buck
  • 3,752
  • 35
  • 31
  • 35
Ajithram
  • 37
  • 3