1

I ask because there are a lot of similar questions with fake answers. I take my NSAttributedString and convert to html then back to NSAttributedString - the string appearance changes. My string:

let attributedString = NSMutableAttributedString(string: "Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of de Finibus Bonorum et Malorum (The Extremes of Good and Evil) by Cicero, written in 45 BC. ", attributes: [
            NSFontAttributeName: UIFont.systemFont(ofSize: 13.0, weight: UIFontWeightRegular),
            NSForegroundColorAttributeName: UIColor(white: 205.0 / 255.0, alpha: 1.0)
            ])
        attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor(red: 74.0 / 255.0, green: 144.0 / 255.0, blue: 226.0 / 255.0, alpha: 1.0), range: NSRange(location: 498, length: 63))

At least the result of double conversion has larger font.

Vyachaslav Gerchicov
  • 2,317
  • 3
  • 23
  • 49
  • Can i know what is your purpose to convert this string to HTML.? – Abhirajsinh Thakore Apr 13 '18 at 09:40
  • 1
    1)translate it; 2)string may come from server – Vyachaslav Gerchicov Apr 13 '18 at 09:42
  • Why don't you keep the original string as it is? – Kamran Apr 13 '18 at 10:03
  • I believe this is "answered" (or at least dealt with) here https://stackoverflow.com/questions/20992950/ios7-font-size-change-when-create-nsattributedstring-from-html - my own solution, which may or may not work for you, was to use RTF as the serialised representation of the `NSAttributedString` rather than HTML. – Grimxn Apr 13 '18 at 10:10
  • 1
    ... I'm not voting to close this as a duplicate, since the answer I noted above isn't actually a definitive answer, it's various "yeah, I noticed this too, here's what I did to get round it" answers, and it depends on his use case whether they are useful. – Grimxn Apr 13 '18 at 10:17
  • @Grimxn Some questions about rtf: 1)what editor do you use (or advice to use) to edit it? 2)does it support links? My example doesn't contain links but if it is html it will be easy to add missing links and convert back to attributed string – Vyachaslav Gerchicov Apr 13 '18 at 12:12
  • 1) TextEdit - free with Mac and simple. 2) I believe it does... – Grimxn Apr 13 '18 at 13:43
  • From what I remember: That's an issue on iOS SDK, because on way way it translates into points and the other one into pixels in HTML for the font size. See this question: https://stackoverflow.com/questions/28441486/html-to-nsattributedstring-and-nsattributedstring-to-html – Larme Apr 13 '18 at 14:59

0 Answers0