I have a string like this
var str = "@text1 this is good @text1"
Now replace text1
with another string, say t 1
. I am able to replace the text, but i am not able to bold it. I want to bold the new string t 1
, so that the final output will be:
@t 1 this is good @t 1
How can I do it?
All the examples I am seeing are in Objective-C, but I want to do it in Swift.
Thanks in advance.
Improving upon Prajeet Shrestha answer : -
You can make a generic extension for NSMutableAttributedString which involves less code. In this case I have chosen to use system font but you could adapt it so you can input the font name as a parameter.
Here is a neat way to make a combination of bold and normal texts in a single label.
Extension:
Swift 3.0
Swift 4
Usage:
Result:
Accepting as valid the response of Prajeet Shrestha in this thread, I would like to extend his solution using the Label if it is known and the traits of the font.
Swift 4
This is the best way that I have come up with. Add a function you can call from anywhere and add it to a file without a class like Constants.swift and then you can embolden words within any string, on numerous occasions by calling just ONE LINE of code:
To go in a constants.swift file:
Then you can just call this one line of code for any UILabel:
Building on Jeremy Bader and David West's excellent answers, a Swift 3 extension:
Usage:
This could be useful