Skip to content

Commit

Permalink
Fixes issues when pressing on uicollectioncells (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
mustiikhalil authored Jan 20, 2022
1 parent 87d3d94 commit d5c2774
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
2 changes: 1 addition & 1 deletion LCLabel.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'LCLabel'
s.version = '0.1.1'
s.version = '0.2.0'
s.summary = 'LCLabel is a TextKit 2 based UIView'
s.description = "LCLabel is a TextKit 2 based UIView that mimics a the behaviour of UILabel & UITextView"
s.homepage = 'https://github.com/mustiikhalil/LCLabel'
Expand Down
41 changes: 25 additions & 16 deletions Sources/LCLabel/LCLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ final public class LCLabel: UIView {
renderedStorage
}
set {
currentlySelectedLink = nil
// Removes the current text container since we are resetting it
if let text = newValue {
renderedStorage = NSTextStorage(attributedString: text)
Expand All @@ -116,7 +117,6 @@ final public class LCLabel: UIView {
}()

private var currentlySelectedLink: URL?
private var textContainer: NSTextContainer?

// MARK: - Life Cycle

Expand Down Expand Up @@ -228,6 +228,17 @@ extension LCLabel {
true
}

public override func hitTest(
_ point: CGPoint,
with event: UIEvent?) -> UIView?
{
let link = getLink(at: point)
if link == nil || !isUserInteractionEnabled || isHidden {
return super.hitTest(point, with: event)
}
return self
}

public override func touchesBegan(
_ touches: Set<UITouch>,
with event: UIEvent?)
Expand All @@ -242,8 +253,10 @@ extension LCLabel {
_ touches: Set<UITouch>,
with event: UIEvent?)
{
if currentlySelectedLink != linkAt(touches) {
currentlySelectedLink = nil
if let currentlySelectedLink = currentlySelectedLink,
currentlySelectedLink != linkAt(touches)
{
self.currentlySelectedLink = nil
} else {
super.touchesMoved(touches, with: event)
}
Expand All @@ -253,10 +266,15 @@ extension LCLabel {
_ touches: Set<UITouch>,
with event: UIEvent?)
{
if currentlySelectedLink == nil {
super.touchesEnded(touches, with: event)
if let currentSelectedLink = currentlySelectedLink {
self.currentlySelectedLink = nil
if let touch = touches.first {
delegate?.didPress(
url: currentSelectedLink,
at: touch.location(in: self))
}
} else {
currentlySelectedLink = nil
super.touchesEnded(touches, with: event)
}
}

Expand All @@ -282,20 +300,11 @@ extension LCLabel {

extension LCLabel: UIGestureRecognizerDelegate {

public override func gestureRecognizerShouldBegin(
_ gestureRecognizer: UIGestureRecognizer)
-> Bool
{
isUserInteractionEnabled
}

public func gestureRecognizer(
_ gestureRecognizer: UIGestureRecognizer,
shouldReceive touch: UITouch) -> Bool
{
let point = touch.location(in: self)
checkoutLink(at: point)
return false
return getLink(at: touch.location(in: self)) != nil
}

@objc
Expand Down

0 comments on commit d5c2774

Please sign in to comment.