session-ios/Session/Utilities/UILabel+Interaction.swift
2020-11-12 08:48:41 +11:00

15 lines
656 B
Swift

extension UILabel {
func characterIndex(for point: CGPoint) -> Int {
let textStorage = NSTextStorage(attributedString: attributedText!)
let layoutManager = NSLayoutManager()
textStorage.addLayoutManager(layoutManager)
let textContainer = NSTextContainer(size: bounds.size)
textContainer.lineFragmentPadding = 0
textContainer.maximumNumberOfLines = numberOfLines
textContainer.lineBreakMode = lineBreakMode
layoutManager.addTextContainer(textContainer)
return layoutManager.characterIndex(for: point, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
}
}