0

I am appending a text on the end of a string that I have received by making the API Call in Xcode. Now, I want to add colour and make only the text "Tap to Read More" bold. Any suggestions on how can I achieve that? Would appreciate your help:)

self.content[i] = recievedData.title! + "\nTap to Read More "

let str = "Tap to Read More"
    
    let attributes: [NSAttributedString.Key: Any] = [
        .foregroundColor: UIColor.red,
    ]

    let attributedQuote = NSAttributedString(string: str, attributes: attributes)
Pressing_Keys_24_7
  • 1,755
  • 2
  • 7
  • 33
  • 3
    You are looking for `NSAttributeString`. – Larme Oct 23 '20 at 15:58
  • 1
    Does this answer your question? [Use multiple font colors in a single label](https://stackoverflow.com/questions/27728466/use-multiple-font-colors-in-a-single-label) – Rob Oct 23 '20 at 16:05

1 Answers1

1

You can try NSAttributedString.

Example:

let str = "Tap to Read More"

// Create your attributed string
let attributes: [NSAttributedString.Key : Any] = [.foregroundColor : UIColor.red]
let attributedStr = NSAttributedString(string: str, attributes: attributes)

// Example for a UILabel
let lbl = UILabel()
lbl.attributedText = attributedStr

Doc on NSAttributedString: https://developer.apple.com/documentation/foundation/nsattributedstring

Eric Hua
  • 976
  • 2
  • 11
  • 29