1

Hi I have a UILabel named questionLabel

let exStr = "SOME HTML STRING CONTAINING BOLD AND ITALIC WORDS"

var exAttrStr = try! NSMutableAttributedString(data: exStr.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!,
                    options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, namedNSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding],
                    documentAttributes: nil)

exAttrStr.addAttribute(NSForegroundColorAttributeName, value: UIColor(colorLiteralRed: 51/255, green: 53/255, blue: 51/255, alpha: 1), range: NSRange(location:0,length:exAttrStr.length))

exAttrStr.addAttribute(NSFontAttributeName , value: SharedConstants.textFontSFUITextRegular(15), range: NSRange(location:0,length:exAttrStr.length))

questionLabel.attributedText = exAttrStr

I am setting the font as SFUIText regular. As I am adding attributes to the exAttrStr the bold and italic part of the attributed text is being overwritten. Is there a way I could get the bold and italic words using the SFUIText font family.

JanLeeYu
  • 981
  • 2
  • 9
  • 24
  • Bold and Italic are inside the `NSFontAttributedName` (and there can be only one `NSFontAttributeName`'s value for a given range). So you have to enumerate the `exAttrStr` for its already `NSFontAttributedName`, check if it's bold and/or italic, and apply then the corresponding `SFUITextFont` corresponding. – Larme Mar 14 '16 at 14:14
  • You can mix solutions: http://stackoverflow.com/questions/31546844/attributed-text-replace-a-specific-font-by-another-using-swift/ http://stackoverflow.com/questions/28941064/change-uitextview-font-w-o-removing-bold-underline-italics-formatting/ – Larme Mar 14 '16 at 14:17

0 Answers0