StatusContentLabel: remove unnecessary height correction

This commit is contained in:
Shadowfacts 2018-08-26 15:07:03 -04:00
parent fdcca20cf0
commit 40ba5c371a
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 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)
}
}