6

My application uses NSAttributedString to display text to the user. Most of this text is inside the paragraph HTML tags (<p>). When it's inside the tags, the font family is changed to some Times style.
It happend in the new version iOS 13, before we did not hve this problem.

On this image you can see normal font and the modified font when it is in the

tags: https://i.stack.imgur.com/zG2F8.jpg

We don't know where to start looking for the solution.

let font = UIFont.preferredFont(forTextStyle: .body)
let strDesc: String = "\(explanation)\n"
let newStringDesc = strDesc.replacingOccurrences(of: "</ul>", with: "</ul><br>")

let modifiedString = "<style>body{font-family: '\(font.fontName)'; font-size:\(font.pointSize)px;}</style>\(strDesc)"
guard let data = modifiedString.data(using: .utf8) else { return nil }

let attrString = try NSMutableAttributedString(
    data: data,
    options: [
              NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html,

NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue,
                ],
    documentAttributes: nil
)
attrString.addCustomParagraphSettings(font: font)
attrString.setCustomParagraphFontColor()

let mainAttributedText = NSMutableAttributedString()

mainAttributedText.append(attrString)
mainAttributedText.append(tmpTakenText)
descriptionTextView.attributedText = mainAttributedText

I expect to see standard iOS font.

Larme
  • 24,190
  • 6
  • 51
  • 81
Grzegorz Świerad
  • 365
  • 1
  • 2
  • 10
  • And what print `font` or `font.fontName`? – Larme Sep 13 '19 at 14:46
  • fontName = ".SFUI-Regular" – Grzegorz Świerad Sep 13 '19 at 14:51
  • font-family: shouldn't byt font.familyName instead? – Larme Sep 13 '19 at 14:57
  • Yes, now it works! Thanks @Larme – Grzegorz Świerad Sep 13 '19 at 15:01
  • I'd say that previously, it wasn't finding the font because it should be familyName (the param name is saying so), and by default put some SF Font or Helvetica Neue giving you the impression it was working. Now why shouldn't it return if not found San Francisco font is strange. – Larme Sep 13 '19 at 15:03
  • I found one more thing quite strange - when I removed this like, it was still using this wrong font. Apparently, it's the default font for the paragraphs or something like that. – Grzegorz Świerad Sep 13 '19 at 15:06
  • 2
    This solution seems to work, but it only works when the required font is the regular one, because it is the one returded by default from the font family. `font.fontName` is supposed to specify exactly which font from the family is required, i.e. thin, bold, heavy... – Rado Oct 02 '19 at 09:43

1 Answers1

3

@Larme found a bug in the code. The value of the font-family CSS property should be font.familyName and not font.fontName.

Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
Grzegorz Świerad
  • 365
  • 1
  • 2
  • 10
  • I confirm that I run in the same problem, the CSS does not accept fontName and fall backs to default font, with familyName it works – AleGiovane Aug 04 '21 at 12:28