I have two NSAttributedString
, where I am trying to make first one in bold.. I am trying to make it bold but it is not working.
var attrTitle : NSAttributedString
var attrBody : NSAttributedString
let mutableAttributedString = NSMutableAttributedString()
let boldAttributedString = NSAttributedString(attributedString:attrTitle)
let regularAttributedString = NSAttributedString(attributedString:attrBody)
mutableAttributedString.append(boldAttributedString)
mutableAttributedString.append(regularAttributedString)
is there any way to make it bold..Thanks for help
Seems you missed bold attributes for title string, you can achieve it via NSAttributedString.Key.font
with any bold font value.
let attrTitle = "Title"
let attrBody = "Body"
let mutableAttributedString = NSMutableAttributedString()
let attributes = [NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 15)]
let boldAttributedString = NSMutableAttributedString(string: attrTitle, attributes: attributes)
let regularAttributedString = NSAttributedString(attributedString: attrBody)
mutableAttributedString.append(boldAttributedString)
mutableAttributedString.append(regularAttributedString)
You can add attribute like this
let boldAttributedString = NSMutableAttributedString(attributedString: attrTitle)
boldAttributedString.addAttributes([.font : UIFont.boldSystemFont(ofSize: 15)], range: NSMakeRange(0, boldAttributedString.length))
Found a way to make it bold and still use NSAttributedString .
You have to assign the properties before hand.
var attrTitle : NSAttributedString
var attrBody : NSAttributedString
var mutableAttributedString = NSMutableAttributedString()
//Properties
let boldFont = UIFont.boldSystemFont(ofSize: //your size)
let attributes = [NSAttributedString.Key.font: boldFont]
let boldAttributedString = NSAttributedString(string: attrTitle.string, attributes: attributes)
let regularAttributedString = NSAttributedString(attributedString:attrBody)
mutableAttributedString.append(boldAttributedString)
mutableAttributedString.append(regularAttributedString)
Also remember that if you want to display the attributed string you need to call .attributedText property of your object before assignment.
Hope this helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.