1

First wanna say thank you before if this can be solved..so i have textview that get the data with html tag..so i use attributedText and function to render html..it worked..but i need to change the font family..right now by default it "times new roman" i want to change to "Helvetica" any clue? i use the extension for this :

extension Data {
var attributedString: NSAttributedString? {
    do {
        return try NSAttributedString(data: self, options:[NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil)
    } catch {
        print(error)
    }
    return nil
}}

extension String {
var data: Data {
    return Data(utf8)
}}

then i use it with :

cell.txtview.attributedText = contentText.data.attributedString

it worked but the font default become "times new roman" i need to change it..any idea? i am very appreciate it before..thank you!

enter image description here

I also already try this...see image below

enter image description here

1 Answers1

0

Your attributed string must be like below :

using this link

let data = "vini".data(using: .utf8)

let attributedString = data!.attributedString
let newAttributedString = NSMutableAttributedString(attributedString: (attributedString)!)

newAttributedString.enumerateAttribute(NSFontAttributeName, in: NSMakeRange(0, (newAttributedString.length)), options: []) { value, range, stop in
    guard let currentFont = value as? UIFont else {
        return
    }

    let fontDescriptor = currentFont.fontDescriptor.addingAttributes([UIFontDescriptorFamilyAttribute: "Helvetica"])

    if let newFontDescriptor = fontDescriptor.matchingFontDescriptors(withMandatoryKeys: [UIFontDescriptorFamilyAttribute]).first {
        let newFont = UIFont(descriptor: newFontDescriptor, size: currentFont.pointSize)
            newAttributedString.addAttributes([NSFontAttributeName: newFont], range: range)
        }
    }

    print(newAttributedString)
Vini App
  • 7,339
  • 2
  • 26
  • 43
  • still not worked..i already try that..i think should like looping the content then replace it..i can't found the way for that..ugh..thank you! – user1400140 Sep 28 '17 at 02:53
  • Updated the answer. Please check. – Vini App Sep 28 '17 at 06:16
  • That's it..hehee...i also found that answer too..and it worked..sorry i just want to type to and you already answered..thank you sooo much @Vini App for all your help and support! soo great! – user1400140 Sep 28 '17 at 12:34