StatusContentLabel: remove unnecessary height correction

This commit is contained in:
Shadowfacts 2018-08-26 15:07:03 -04:00
parent 62bc57e169
commit 078e73b161
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 6 additions and 20 deletions

View File

@ -42,7 +42,6 @@ class StatusContentLabel: UILabel {
private lazy var textStorage = NSTextStorage() private lazy var textStorage = NSTextStorage()
private lazy var layoutManager = NSLayoutManager() private lazy var layoutManager = NSLayoutManager()
private lazy var textContainer = NSTextContainer() private lazy var textContainer = NSTextContainer()
private var heightCorrection: CGFloat = 0
private var selectedElement: (range: NSRange, url: URL)? private var selectedElement: (range: NSRange, url: URL)?
private var links: [NSRange: URL] = [:] private var links: [NSRange: URL] = [:]
@ -68,10 +67,10 @@ class StatusContentLabel: UILabel {
let range = NSRange(location: 0, length: textStorage.length) let range = NSRange(location: 0, length: textStorage.length)
textContainer.size = rect.size textContainer.size = rect.size
let newOrigin = textOrigin(in: rect) let origin = rect.origin
layoutManager.drawBackground(forGlyphRange: range, at: newOrigin) layoutManager.drawBackground(forGlyphRange: range, at: origin)
layoutManager.drawGlyphs(forGlyphRange: range, at: newOrigin) layoutManager.drawGlyphs(forGlyphRange: range, at: origin)
} }
override var intrinsicContentSize: CGSize { override var intrinsicContentSize: CGSize {
@ -84,14 +83,12 @@ class StatusContentLabel: UILabel {
private func element(at location: CGPoint) -> (range: NSRange, url: URL)? { private func element(at location: CGPoint) -> (range: NSRange, url: URL)? {
guard textStorage.length > 0 else { return nil } 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) 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 return nil
} }
let index = layoutManager.glyphIndex(for: correctLocation, in: textContainer) let index = layoutManager.glyphIndex(for: location, in: textContainer)
for (range, url) in links { for (range, url) in links {
if index >= range.location && index <= range.location + range.length { if index >= range.location && index <= range.location + range.length {
@ -248,11 +245,7 @@ class StatusContentLabel: UILabel {
return return
} }
// is this necessary? textStorage.setAttributedString(attributedText)
let mutAttrString = addLineBreak(attributedText)
// let mutAttrString = NSMutableAttributedString(attributedString: attributedText)
textStorage.setAttributedString(mutAttrString)
_customizing = true _customizing = true
text = attributedText.string text = attributedText.string
_customizing = false _customizing = false
@ -276,11 +269,4 @@ class StatusContentLabel: UILabel {
return mutAttrString 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)
}
} }