Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swift 4 goodies #4

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
swift 4
  • Loading branch information
nikans authored Apr 10, 2018
commit ae0020937623f8c0897b42f7b00db526d3ae7a87
32 changes: 10 additions & 22 deletions Source/UIGestureRecognizer+UILabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,30 @@ import UIKit.UIGestureRecognizerSubclass
extension UIGestureRecognizer {
//Returns a character that was touched, or nil if none.
func indexOfCharacterTouched(label: UILabel) -> Int? {
let locationOfTouchInLabel = self.location(in: label)

return label.indexOfCharacter(atPoint: locationOfTouchInLabel)
}
}

public extension UILabel {
public func indexOfCharacter(atPoint point: CGPoint) -> Int? {
if self.attributedText == nil {
if label.attributedText == nil {
return nil
}

let layoutManager = NSLayoutManager()
let textContainer = NSTextContainer()
let textStorage = NSTextStorage(attributedString: self.attributedText!)
let textStorage = NSTextStorage(attributedString: label.attributedText!)

layoutManager.addTextContainer(textContainer)
textStorage.addLayoutManager(layoutManager)

textContainer.lineFragmentPadding = 0
textContainer.lineBreakMode = self.lineBreakMode
textContainer.maximumNumberOfLines = self.numberOfLines
textContainer.size = self.bounds.size
textContainer.lineBreakMode = label.lineBreakMode
textContainer.maximumNumberOfLines = label.numberOfLines
textContainer.size = label.bounds.size

let locationOfTouchInLabel = self.location(in: label)
let textBoundingBox = layoutManager.usedRect(for: textContainer)

if !textBoundingBox.contains(point) {
return nil
}

let textContainerOffset = CGPoint(
x: (self.bounds.size.width - textBoundingBox.size.width) * 0.5 - textBoundingBox.origin.x,
y: (self.bounds.size.height - textBoundingBox.size.height) * 0.5 - textBoundingBox.origin.y)
x: (label.bounds.size.width - textBoundingBox.size.width) * 0.5 - textBoundingBox.origin.x,
y: (label.bounds.size.height - textBoundingBox.size.height) * 0.5 - textBoundingBox.origin.y)
let locationOfTouchInTextContainer = CGPoint(
x: point.x - textContainerOffset.x,
y: point.y - textContainerOffset.y)
x: locationOfTouchInLabel.x - textContainerOffset.x,
y: locationOfTouchInLabel.y - textContainerOffset.y)
let indexOfCharacter = layoutManager.characterIndex(
for: locationOfTouchInTextContainer,
in: textContainer,
Expand Down