0

我正在开发 iPhone 应用程序,如果起点是 UILabel,我想在其中画线,我还想检测被触摸的文本。这怎么可能?请帮助我。提前谢谢,这是我画线的代码。使用这个代码我可以画线,但我想从标签画线。

- (CAShapeLayer *)createShapeLayer:(UIView *)view
{
     CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];

    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.strokeColor = [UIColor redColor].CGColor;
    shapeLayer.lineWidth = 5.0;

    [view.layer addSublayer:shapeLayer];

    return shapeLayer;
    }

    - (void)handlePanGesture:(UIPanGestureRecognizer *)gesture
    {
    static CAShapeLayer *shapeLayer;
    static CGPoint origin;

    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        shapeLayer = [self createShapeLayer:gesture.view];
        origin = [gesture locationInView:gesture.view];

        NSLog(@"cgpoint %f and y %f",origin.x,origin.y);


    }
    else if (gesture.state == UIGestureRecognizerStateChanged)
    {
        UIBezierPath *path1 = [UIBezierPath bezierPath];
        [path1 moveToPoint:origin];
        CGPoint location = [gesture locationInView:gesture.view];
        [path1 addLineToPoint:location];
        shapeLayer.path = path1.CGPath;
    }
    else if (gesture.state == UIGestureRecognizerStateEnded ||
             gesture.state == UIGestureRecognizerStateFailed ||
             gesture.state == UIGestureRecognizerStateCancelled)
    {
        shapeLayer = nil;
    }
}
4

1 回答 1

0

我有一个关于如何检测你触摸的文本的想法,它确实有效,这是我做的:

您将其添加到标签中:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    [textLabel addGestureRecognizer:tap];
     textLabel.userInteractionEnabled = YES;
    [tap release];

执行:

- (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 < 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 > 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;
}

它将记录触摸的单词。

我没有考虑到 y,但这不应该阻止任何人。

于 2013-07-16T09:32:54.930 回答