I have a method that sets a NSAttributedString to be bold:
func setBold(text: String) -> NSMutableAttributedString {
guard let font = UIFont.CustomNormalBoldItalic() else {
fatalError("font not found")
}
let string = NSMutableAttributedString(string:"\(text)", attributes: [NSFontAttributeName : font])
self.setAttributedString(string)
return self
}
And this is how it's called, which works normally:
let formattedString = NSMutableAttributedString()
formattedString.setBold("Your text here")
However I am trying to set the text of a substring of an NSLocalizedString to be bold. So I would try it like so:
let formattedString = NSMutableAttributedString()
return NSAttributedString(string: String.localizedStringWithFormat(
NSLocalizedString("message", comment: ""),
formattedString.setBold(NSLocalizedString("message.day", comment: "")),
NSLocalizedString("message.time", comment: "")
))
Instead of being "Today starting at 10pm", it gives the following output:
Today{
NSFont = "<UICTFont: 0x7fb75d4f1330> font-family: \"CustomText-MediumItalic\"; font-weight: normal; font-style: italic; font-size: 14.00pt";
} starting at 10pm {
}
Can anyone tell me where I am going wrong or how I can fix this? The reason I have another method is because I have many LocalizedStrings to set bold and thought this might be a simple solution. Open to other ideas/solutions that don't involve lots of repetition/lines of code.