Is there a way to add as an attribute for NSAttributedString ( NSMuttableAttributeString ) not just a font, but a font family for a range ? The client-server application receives the product description formatted in HTML, it is necessary to preserve the size, thickness, italics (if any) of the font, but replace its family. If you specify a font with a size as an attribute, the formatting associated with the size and thickness of the font is erased.

UPDATED: Decided with a crutch

 let d : NSMutableAttributedString = try NSMutableAttributedString(data: ((здесь html текст).dataUsingEncoding(NSUTF8StringEncoding))!, options: [NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType], documentAttributes: nil) d.enumerateAttribute(NSFontAttributeName, inRange: NSMakeRange(0, d.length), options: NSAttributedStringEnumerationOptions()) { value, range, stop in let oldFont = value as! UIFont var newFontName : String if oldFont.fontName.lowercaseString.containsString("bold") { newFontName = Decorator.fontArray["Bold"]! } else if oldFont.fontName.lowercaseString.containsString("italic") { newFontName = Decorator.fontArray["Italic"]! } else { newFontName = Decorator.fontArray["Regular"]! } let newFont = UIFont(name: newFontName, size: 14) d.addAttribute(NSFontAttributeName, value : newFont!, range : range) } 

Where Decorator.fontArray is a Dictionary<String, String> with keys with font type names, values ​​— font names for these types. It will not work if the font name is not bold or italic, which means the question remains valid))

    2 answers 2

    I will offer two options:

    1) change the font in html before you convert it to string

     var source: NSString = "Text <strong>Here</strong>" source = source.stringByAppendingString("<style>strong{font-family: 'Avenir-Roman';font-size: 14px;}</style>") do { let d:NSMutableAttributedString = try NSMutableAttributedString(data: source.dataUsingEncoding(NSUTF8StringEncoding)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: NSNumber(int:Int32(NSUTF8StringEncoding))], documentAttributes: nil) var label:UILabel! label = UILabel(frame: CGRectMake(100,0,200,100)) label.attributedText = d self.view.addSubview(label) } catch { } 

    2) take each character separately and change the font on it, leaving the remaining attributes

     extension NSMutableAttributedString { func convertFontTo(font: UIFont) { var range = NSMakeRange(0, 0) while (NSMaxRange(range) < length) { let attributes = attributesAtIndex(NSMaxRange(range), effectiveRange: &range) if let oldFont = attributes[NSFontAttributeName] { let newFont = UIFont(descriptor: font.fontDescriptor().fontDescriptorWithSymbolicTraits(oldFont.fontDescriptor().symbolicTraits), size: font.pointSize) addAttribute(NSFontAttributeName, value: newFont, range: range) } else { addAttribute(NSFontAttributeName, value: font, range: range) } } } } 

    Use this:

     let desc = NSMutableAttributedString(attributedString: someNSAttributedString) desc.convertFontTo(UIFont.systemFontOfSize(16)) 
    • There is no font name in html - there are tags - <b> </ b> and so on. They are converted to a specific font that is pre-installed by default (in the IDE, I don’t know in the NSMutableAttributedString class) and this attribute is no longer stored in the string, it is stored in the font name (OpenSans-Bold - for example). Formatting html is a thankless task, you have to impose a serious parser, and not the fact that there will be a result, but the second method disappears, since the attribute (thickness, italics, hyperlinks) is not stored, which means formatting the string is lost again - iamthevoid

    I still found a very elegant solution.

     extension UILabel { func setHTMLFromString(text: String) { let modifiedFont = NSString(format:"<span style=\"font-family: \(self.font!.fontName); font-size: \(self.font!.pointSize)\">%@</span>", text) as String let attrStr = try! NSAttributedString( data: modifiedFont.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding], documentAttributes: nil) self.attributedText = attrStr } } 

    I admit that I was honestly mistaken about the passer html, everything turned out much easier

    • so what will you do with a hundred rubles? :) - Max Mikheyenko
    • ))) like added) - iamthevoid