From 078e73b1614d498317424f3fd5451843a6876fab Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 26 Aug 2018 15:07:03 -0400 Subject: [PATCH] StatusContentLabel: remove unnecessary height correction --- Tusker/Views/StatusContentLabel.swift | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/Tusker/Views/StatusContentLabel.swift b/Tusker/Views/StatusContentLabel.swift index 4d322bfc..9cd078f8 100644 --- a/Tusker/Views/StatusContentLabel.swift +++ b/Tusker/Views/StatusContentLabel.swift @@ -42,7 +42,6 @@ class StatusContentLabel: UILabel { private lazy var textStorage = NSTextStorage() private lazy var layoutManager = NSLayoutManager() private lazy var textContainer = NSTextContainer() - private var heightCorrection: CGFloat = 0 private var selectedElement: (range: NSRange, url: URL)? private var links: [NSRange: URL] = [:] @@ -68,10 +67,10 @@ class StatusContentLabel: UILabel { let range = NSRange(location: 0, length: textStorage.length) textContainer.size = rect.size - let newOrigin = textOrigin(in: rect) + let origin = rect.origin - layoutManager.drawBackground(forGlyphRange: range, at: newOrigin) - layoutManager.drawGlyphs(forGlyphRange: range, at: newOrigin) + layoutManager.drawBackground(forGlyphRange: range, at: origin) + layoutManager.drawGlyphs(forGlyphRange: range, at: origin) } override var intrinsicContentSize: CGSize { @@ -84,14 +83,12 @@ class StatusContentLabel: UILabel { private func element(at location: CGPoint) -> (range: NSRange, url: URL)? { guard textStorage.length > 0 else { return nil } - var correctLocation = location - correctLocation.y -= heightCorrection let boundingRect = layoutManager.boundingRect(forGlyphRange: NSRange(location: 0, length: textStorage.length), in: textContainer) - guard boundingRect.contains(correctLocation) else { + guard boundingRect.contains(location) else { return nil } - let index = layoutManager.glyphIndex(for: correctLocation, in: textContainer) + let index = layoutManager.glyphIndex(for: location, in: textContainer) for (range, url) in links { if index >= range.location && index <= range.location + range.length { @@ -248,11 +245,7 @@ class StatusContentLabel: UILabel { return } - // is this necessary? - let mutAttrString = addLineBreak(attributedText) -// let mutAttrString = NSMutableAttributedString(attributedString: attributedText) - - textStorage.setAttributedString(mutAttrString) + textStorage.setAttributedString(attributedText) _customizing = true text = attributedText.string _customizing = false @@ -276,11 +269,4 @@ class StatusContentLabel: UILabel { return mutAttrString } - private func textOrigin(in rect: CGRect) -> CGPoint { - let usedRect = layoutManager.usedRect(for: textContainer) - heightCorrection = (rect.height - usedRect.height) / 2 - let glyphOriginY = heightCorrection > 0 ? rect.origin.y + heightCorrection : rect.origin.y - return CGPoint(x: rect.origin.x, y: glyphOriginY) - } - }