Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have an idea on how you could detect the text you touch, and it actually works, here is what i cooked up:</p> <p>You add this to the label :</p> <pre><code>UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; [textLabel addGestureRecognizer:tap]; textLabel.userInteractionEnabled = YES; [tap release]; </code></pre> <p>Implementation:</p> <pre><code>- (void) handleTap:(UITapGestureRecognizer *)tap { CGPoint point = [tap locationInView:tap.view]; UILabel *label = (UILabel *)tap.view; CGSize blankSize = [@" " sizeWithFont:label.font]; CGSize textSize = [label.text sizeWithFont:label.font]; if (textSize.width - point.x &lt; 0) { // out of bounds } else { NSArray * array = [label.text componentsSeparatedByString:@" "]; CGFloat value = 0.0f; for (NSString *str in array) { CGSize textSize = [str sizeWithFont:label.font]; if (textSize.width + value &gt; point.x) { NSLog(@"touched str %@",str); break; } value += textSize.width + blankSize.width; } } //PLACE_THE_DIFFERENCE_BETWEEN_THE_CENTER_OF_THE_LABEL_AND_THE_OTHER_OBJECT_OR_POINT as width CGRect rect = CGRectMake(0, 0, labe.frame.size.width , label.frame.size.height); //i used the touch point as an end point , you just need to find the correct rect to draw in and that's it UIImage *img = [self drawLine:rect fromPoint:CGPointMake(0, label.center.y) toPoint:point withColor:[UIColor blackColor]]; //make sure you don't add imageview one over another so you could tag it and remove the subview with the specified tag UIImageView *imgview = [[UIImageView alloc] initWithFrame:CGRectMake(label.center.x, 0, rect.size.width, rect.size.height)]; imgview.image = img; imgview.backgroundColor = [UIColor redColor]; [label.superview addSubview:imgview]; [imgview release]; } -(UIImage*)drawLine:(CGRect) frame fromPoint:(CGPoint)startPoint toPoint:(CGPoint)endPoint withColor:(UIColor *)color { UIGraphicsBeginImageContext(frame.size); CGContextRef thisContext = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(thisContext,5); CGContextSetStrokeColorWithColor(thisContext, color.CGColor); CGContextMoveToPoint(thisContext, startPoint.x, startPoint.y ); CGContextAddLineToPoint(thisContext, endPoint.x , endPoint.y ); CGContextStrokePath(thisContext); UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return img; } </code></pre> <p>It will log the word touched.</p> <p>I haven't took into account the y, but that shouldn't hold anyone back.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload