Fix multiple lines of emojis (e.g., wordle) getting smushed together

This commit is contained in:
Shadowfacts 2022-05-15 15:42:48 -04:00
parent 68b03838a2
commit 98516e3802
1 changed files with 13 additions and 6 deletions

View File

@ -78,6 +78,11 @@ class ContentTextView: LinkTextView, BaseEmojiLabel {
mutAttrString.trimTrailingCharactersInSet(.whitespacesAndNewlines) mutAttrString.trimTrailingCharactersInSet(.whitespacesAndNewlines)
mutAttrString.collapseWhitespace() mutAttrString.collapseWhitespace()
let style = NSMutableParagraphStyle()
// 2 points is enough that it doesn't make things look weirdly spaced out, but leaves a slight gap when there are multiple lines of emojis
style.lineSpacing = 2
mutAttrString.addAttribute(.paragraphStyle, value: style, range: mutAttrString.fullRange)
self.attributedText = mutAttrString self.attributedText = mutAttrString
} }
@ -99,7 +104,9 @@ class ContentTextView: LinkTextView, BaseEmojiLabel {
switch node.tagName() { switch node.tagName() {
case "br": case "br":
attributed.append(NSAttributedString(string: "\n")) // need to specify defaultFont here b/c otherwise it uses the default 12pt Helvetica which
// screws up its determination of the line height making multiple lines of emojis squash together
attributed.append(NSAttributedString(string: "\n", attributes: [.font: defaultFont]))
case "a": case "a":
if let link = try? node.attr("href"), if let link = try? node.attr("href"),
let webURL = WebURL(link), let webURL = WebURL(link),
@ -107,7 +114,7 @@ class ContentTextView: LinkTextView, BaseEmojiLabel {
attributed.addAttribute(.link, value: url, range: attributed.fullRange) attributed.addAttribute(.link, value: url, range: attributed.fullRange)
} }
case "p": case "p":
attributed.append(NSAttributedString(string: "\n\n")) attributed.append(NSAttributedString(string: "\n\n", attributes: [.font: defaultFont]))
case "em", "i": case "em", "i":
let currentFont: UIFont let currentFont: UIFont
if attributed.length == 0 { if attributed.length == 0 {
@ -129,11 +136,11 @@ class ContentTextView: LinkTextView, BaseEmojiLabel {
case "code": case "code":
attributed.addAttribute(.font, value: UIFont.monospacedSystemFont(ofSize: defaultFont.pointSize, weight: .regular), range: attributed.fullRange) attributed.addAttribute(.font, value: UIFont.monospacedSystemFont(ofSize: defaultFont.pointSize, weight: .regular), range: attributed.fullRange)
case "pre": case "pre":
attributed.append(NSAttributedString(string: "\n\n"))
attributed.addAttribute(.font, value: UIFont.monospacedSystemFont(ofSize: defaultFont.pointSize, weight: .regular), range: attributed.fullRange) attributed.addAttribute(.font, value: UIFont.monospacedSystemFont(ofSize: defaultFont.pointSize, weight: .regular), range: attributed.fullRange)
attributed.append(NSAttributedString(string: "\n\n"))
case "ol", "ul": case "ol", "ul":
attributed.trimLeadingCharactersInSet(.whitespacesAndNewlines)
attributed.append(NSAttributedString(string: "\n\n")) attributed.append(NSAttributedString(string: "\n\n"))
attributed.trimLeadingCharactersInSet(.whitespacesAndNewlines)
case "li": case "li":
let parentEl = node.parent()! let parentEl = node.parent()!
let parentTag = parentEl.tagName() let parentTag = parentEl.tagName()
@ -143,12 +150,12 @@ class ContentTextView: LinkTextView, BaseEmojiLabel {
// we use the monospace digit font so that the periods of all the list items line up // we use the monospace digit font so that the periods of all the list items line up
bullet = NSAttributedString(string: "\(index + 1).\t", attributes: [.font: UIFont.monospacedDigitSystemFont(ofSize: defaultFont.pointSize, weight: .regular)]) bullet = NSAttributedString(string: "\(index + 1).\t", attributes: [.font: UIFont.monospacedDigitSystemFont(ofSize: defaultFont.pointSize, weight: .regular)])
} else if parentTag == "ul" { } else if parentTag == "ul" {
bullet = NSAttributedString(string: "\u{2022}\t") bullet = NSAttributedString(string: "\u{2022}\t", attributes: [.font: defaultFont])
} else { } else {
bullet = NSAttributedString() bullet = NSAttributedString()
} }
attributed.insert(bullet, at: 0) attributed.insert(bullet, at: 0)
attributed.append(NSAttributedString(string: "\n")) attributed.append(NSAttributedString(string: "\n", attributes: [.font: defaultFont]))
default: default:
break break
} }