zacwest / ZSWTappableLabel

UILabel subclass for links which are tappable, long-pressable, 3D Touchable, and VoiceOverable.
MIT License
169 stars 37 forks source link

Tap area is too low with a custom font #20

Closed nuudles closed 5 years ago

nuudles commented 8 years ago

Hey @zacwest! I'm trying to use your component to detect taps in my NSAttributedString, but I'm using a custom font and it seems like the detected touch area is sitting lower than it should be. I was able to verify this in the Example project by modifying the options baseAttributes in the MultipleSwiftViewController like so:

options.baseAttributes = [NSFontAttributeName: UIFont(name: "Ubuntu", size: 15)!]

Interestingly enough this didn't reproduce the issue, however if the label is set to auto size the height instead of being matching the view's edges, I'm seeing the tap area be about 5-10 pts too low:

label.snp_makeConstraints { make in
            make.center.equalTo(view)
            make.width.equalTo(250)
        }
zacwest commented 8 years ago

Interesting. Do you have a sample project that can reproduce this? If not, I'll try to put one together when I have time.

A few easy things for debugging:

  1. Can you use the view exploder feature thing to grab the frame of the view? Is it non-integral?
  2. What happens if you force the height to be a certain value? Does that work?
zacwest commented 8 years ago

And secondarily: Are you setting the font using .font = … or setting the NSFontAttributeName on the attributed string? If you flip which one you're doing, does it work?

nuudles commented 8 years ago

I've tried using both the .font and the NSFontAttributeName and variations of either or. It seems to work fine if I just don't set the font.

The frame looks fine here:

<ZSWTappableLabel: 0x7fbda1cdad00; baseClass = UILabel; frame = (12 345; 351 34);

Also interestingly enough if I manually set the height of the label instead of relying on Auto Layout and the intrinsic content size, it can detect the taps just fine, even with the custom font.

Qw4z1 commented 8 years ago

I think I'm having the same problem or at least a related problem. For me the last line in the label is not clickable.

When the text is too long for one line I call sizeToFit. If I call sizeToFit when there is only one line I can't register taps. If I don't call sizeToFit when there is only one line the taps work as expected.

I've created a gist with a slightly modified (not the real list of users) the code I use to create the label.

https://gist.github.com/Qw4z1/bfdbfd73055d41bf71bf8f4a30a76984

zacwest commented 8 years ago

Thanks for the sample code, I'll take a look and see if I can figure out what's happening. It's possible non-integral frames are confusing the size of the container I'm creating for the text (to simulate locations).

zacwest commented 5 years ago

I do not believe this occurs anymore.