refactoring taxtcontainer.
This commit is contained in:
parent
96e5f47dd0
commit
b5dcf68c5d
@ -505,10 +505,12 @@ public typealias ActionBlock = () -> ()
|
|||||||
let accessibleAction = customAccessibilityAction(range: range)
|
let accessibleAction = customAccessibilityAction(range: range)
|
||||||
clauses.append(ActionableClause(range: range, actionBlock: actionBlock, accessibilityID: accessibleAction?.hash ?? -1))
|
clauses.append(ActionableClause(range: range, actionBlock: actionBlock, accessibilityID: accessibleAction?.hash ?? -1))
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func boundingRect(forCharacterRange range: NSRange, in label: Label) -> CGRect {
|
/// Provides a text container in memory of how the text would appear on screen.
|
||||||
|
func abstractTextContainer() -> NSTextContainer? {
|
||||||
guard let attributedText = label.attributedText else { return CGRect() }
|
|
||||||
|
// 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()
|
let paragraph = NSMutableParagraphStyle()
|
||||||
paragraph.alignment = label.textAlignment
|
paragraph.alignment = label.textAlignment
|
||||||
@ -524,9 +526,16 @@ public typealias ActionBlock = () -> ()
|
|||||||
textStorage.addLayoutManager(layoutManager)
|
textStorage.addLayoutManager(layoutManager)
|
||||||
|
|
||||||
textContainer.lineFragmentPadding = 0.0
|
textContainer.lineFragmentPadding = 0.0
|
||||||
textContainer.lineBreakMode = label.lineBreakMode
|
textContainer.lineBreakMode = lineBreakMode
|
||||||
textContainer.maximumNumberOfLines = label.numberOfLines
|
textContainer.maximumNumberOfLines = numberOfLines
|
||||||
textContainer.size = label.bounds.size
|
textContainer.size = bounds.size
|
||||||
|
|
||||||
|
return textContainer
|
||||||
|
}
|
||||||
|
|
||||||
|
public static func boundingRect(forCharacterRange range: NSRange, in label: Label) -> CGRect {
|
||||||
|
|
||||||
|
let textContainer = abstractTextContainer()
|
||||||
|
|
||||||
var glyphRange = NSRange()
|
var glyphRange = NSRange()
|
||||||
|
|
||||||
@ -669,26 +678,7 @@ extension UITapGestureRecognizer {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Must configure the attributed string to translate what would appear on screen to accurately analyze.
|
guard let textContainer = label.abstractTextContainer() else { return false }
|
||||||
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
|
|
||||||
|
|
||||||
let indexOfGlyph = layoutManager.glyphIndex(for: location(in: label), in: textContainer)
|
let indexOfGlyph = layoutManager.glyphIndex(for: location(in: label), in: textContainer)
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user