From 3d6d9b2a917d95eb753c60f37c3658653a07a409 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 9 Apr 2022 15:05:39 -0400 Subject: [PATCH] Fix crash due to empty html element --- Tusker/Views/ContentTextView.swift | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Tusker/Views/ContentTextView.swift b/Tusker/Views/ContentTextView.swift index 3cef26e4..2019f08e 100644 --- a/Tusker/Views/ContentTextView.swift +++ b/Tusker/Views/ContentTextView.swift @@ -109,10 +109,20 @@ class ContentTextView: LinkTextView, BaseEmojiLabel { case "p": attributed.append(NSAttributedString(string: "\n\n")) case "em", "i": - let currentFont: UIFont = attributed.attribute(.font, at: 0, effectiveRange: nil) as? UIFont ?? self.font! + let currentFont: UIFont + if attributed.length == 0 { + currentFont = defaultFont + } else { + currentFont = attributed.attribute(.font, at: 0, effectiveRange: nil) as? UIFont ?? defaultFont + } attributed.addAttribute(.font, value: currentFont.withTraits(.traitItalic)!, range: attributed.fullRange) case "strong", "b": - let currentFont: UIFont = attributed.attribute(.font, at: 0, effectiveRange: nil) as? UIFont ?? self.font! + let currentFont: UIFont + if attributed.length == 0 { + currentFont = defaultFont + } else { + currentFont = attributed.attribute(.font, at: 0, effectiveRange: nil) as? UIFont ?? defaultFont + } attributed.addAttribute(.font, value: currentFont.withTraits(.traitBold)!, range: attributed.fullRange) case "del": attributed.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: attributed.fullRange)