refactoring taxtcontainer.

This commit is contained in:
Kevin G Christiano 2019-10-17 12:17:14 -04:00
parent 96e5f47dd0
commit b5dcf68c5d

View File

@ -505,10 +505,12 @@ public typealias ActionBlock = () -> ()
let accessibleAction = customAccessibilityAction(range: range)
clauses.append(ActionableClause(range: range, actionBlock: actionBlock, accessibilityID: accessibleAction?.hash ?? -1))
}
public static func boundingRect(forCharacterRange range: NSRange, in label: Label) -> CGRect {
guard let attributedText = label.attributedText else { return CGRect() }
/// Provides a text container in memory of how the text would appear on screen.
func abstractTextContainer() -> NSTextContainer? {
// Must configure the attributed string to translate what would appear on screen to accurately analyze.
guard let attributedText = attributedText else { return nil }
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = label.textAlignment
@ -524,9 +526,16 @@ public typealias ActionBlock = () -> ()
textStorage.addLayoutManager(layoutManager)
textContainer.lineFragmentPadding = 0.0
textContainer.lineBreakMode = label.lineBreakMode
textContainer.maximumNumberOfLines = label.numberOfLines
textContainer.size = label.bounds.size
textContainer.lineBreakMode = lineBreakMode
textContainer.maximumNumberOfLines = numberOfLines
textContainer.size = bounds.size
return textContainer
}
public static func boundingRect(forCharacterRange range: NSRange, in label: Label) -> CGRect {
let textContainer = abstractTextContainer()
var glyphRange = NSRange()
@ -669,26 +678,7 @@ extension UITapGestureRecognizer {
return true
}
// Must configure the attributed string to translate what would appear on screen to accurately analyze.
guard let attributedText = label.attributedText else { return false }
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = label.textAlignment
let stagedAttributedString = NSMutableAttributedString(attributedString: attributedText)
stagedAttributedString.addAttributes([NSAttributedString.Key.paragraphStyle: paragraph], range: NSRange(location: 0, length: attributedText.string.count))
let textStorage = NSTextStorage(attributedString: stagedAttributedString)
let layoutManager = NSLayoutManager()
let textContainer = NSTextContainer(size: .zero)
layoutManager.addTextContainer(textContainer)
textStorage.addLayoutManager(layoutManager)
textContainer.lineFragmentPadding = 0.0
textContainer.lineBreakMode = label.lineBreakMode
textContainer.maximumNumberOfLines = label.numberOfLines
textContainer.size = label.bounds.size
guard let textContainer = label.abstractTextContainer() else { return false }
let indexOfGlyph = layoutManager.glyphIndex(for: location(in: label), in: textContainer)