Add UIGestureRecognizer for individual UILabel letters

Now I am making an iOS application, and I would like to implement the ability to delete letters in UILabel, simply by "Drop the letter." The part I'm having a problem with is adding a gesture recognizer to individual UILabel letters. I have already searched the Internet for this and have nothing. To be clear, I am NOT adding a gesture recognizer to the entire label, I just want to add it to individual letters. Any help is appreciated.

thanks

0
source share
3 answers

It seems that the easiest way to do this is to dynamically calculate the position of the letter. Use this:

CGSize textSize = [text sizeWithFont:[UIFont boldSystemFontOfSize:size] constrainedToSize:constrainedSize lineBreakMode:NSLineBreakByWordWrapping]; 

so that you can get the size for the letter in the font and the size that you use for each label and using the [stringInstance length] property and [UILabel numberOfLines] property to get an approximate center for each letter on the label, then use a simple tapGestureRecognizer for each label and where you call your method for

 - (NSString*)letterForPoint:(CGPoint)tapCenter inLabel:(UILabel*)label; 

there you use everything to calculate the approximate center for each letter and add selectableRange for the error and correct the user's response as x + - 20 pixels and y + - 20 pixels.

Apple claims that anything with a choice of less than 40 pixels for 40 pixels will be completely annoying for the user, so your font size should be quite large for user interaction.

+1
source

If I understand correctly, this sounds like subclassing UILabel makes sense.

Make the LetterLabel: UILabel and in init configure your GestureRecognizer on yourself.

Then, when you create your letters, each of them will be attached to it recognizing.

 LetterLabel *firstLetter = [[LetterLabel alloc] init] LetterLabel *secondLetter = [[LetterLabel alloc] init] 
0
source

UIGestureRecognizer can only be applied to a UIView or a subclass of it (for example, UILabel, as Adam suggested). If you are worried about performance, I think the next step will be:

1) A subclass of UIView to create a custom implementation of a UILabel-like representation.

2) Pull the custom label string into the drawInRect method:

3) Use touchhesBegan: withEvent :, touchesMoved: withEvent :, and touchesEnded: withEvent: methods for tracking finger positions to move / redraw support line characters.

EDIT:

Alternatively, you can use one UIPanGestureRecognizer per user label to track finger positions and navigate through the sublayers within the user label (each sublayer can contain a character in a line). That way, you can more easily use Core Animation to animate characters (i.e., create a dropping effect).

0
source

All Articles