Iphone 如何从UILabel的中心绘制直线?

Iphone 如何从UILabel的中心绘制直线?,iphone,ios,objective-c,uikit,uilabel,Iphone,Ios,Objective C,Uikit,Uilabel,我正在开发iPhone应用程序,其中我想画一条线,如果起点是UILabel,我还想检测被触摸的文本。这怎么可能?请帮助我。提前谢谢,这是我画线的代码。使用这个代码我可以画线,但我想从标签上画线 - (CAShapeLayer *)createShapeLayer:(UIView *)view { CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init]; shapeLayer.fillColor = [UIColor cle

我正在开发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;
    }
}

我有一个关于如何检测你触摸的文本的想法,它实际上是有效的,以下是我编造的:

您可以将其添加到标签中:

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;
}
-(void)handleTap:(UITapGestureRecognitor*)轻触
{
CGPoint point=[tap locationInView:tap.view];
UILabel*标签=(UILabel*)点击.view;
cgsizeblanksize=[@“sizeWithFont:label.font];
cgsizetextsize=[label.text-sizeWithFont:label.font];
if(textSize.width-point.x<0)
{
//出界
}
其他的
{
NSArray*数组=[label.text Components由字符串分隔:@”“;
CGFloat值=0.0f;
for(数组中的NSString*str)
{
cgsizetextsize=[str sizeWithFont:label.font];
如果(textSize.width+值>point.x)
{
NSLog(@“触摸str%”,str);
打破
}
值+=textSize.width+blankSize.width;
}
}
//将\u标签\u的\u中心\u和\u其他\u对象\u或\u点之间的\u差异\u作为宽度放置
CGRect rect=CGRectMake(0,0,labe.frame.size.width,label.frame.size.height);
//我用接触点作为终点,你只需要找到正确的矩形就可以了
UIImage*img=[self drawLine:rect fromPoint:CGPointMake(0,label.center.y)toPoint:point with color:[UIColor blackColor]];
//确保不要一个接一个地添加imageview,以便可以标记它并使用指定的标记删除子视图
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发布];
}
-(UIImage*)绘制线:(CGRect)帧起点:(CGPoint)起点到终点:(CGPoint)端点带颜色:(UIColor*)颜色
{
UIGraphicsBeginImageContext(frame.size);
CGContextRef thisContext=UIGraphicsGetCurrentContext();
CGContextSetLineWidth(thisContext,5);
CGContextSetStrokeColorWithColor(thisContext,color.CGColor);
CGContextMoveToPoint(此上下文,
startPoint.x,
startPoint.y
);
CGContextAddLineToPoint(此上下文,
端点.x,
端点.y
);
CGContextStrokePath(thisContext);
UIImage*img=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
返回img;
}
它将记录“触摸”一词


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

请输入一些您尝试过的代码。第一步是编写一些代码。你试过什么了吗?只是一个警告:不要说“不”(不管你说“我不知道从哪里开始”),否则你会得到堆栈溢出的愤怒。我添加了一些用于绘制线的代码,但是如何从UILabel中心开始线只需获取UILabel的
边界的中心即可(您可以使用
CGRectGetMidX
CGRectGetMidY
。现在您正在使用起始触控的位置。我认为您应该尝试处理
-(无效)触控开始:(NSSet*)触控事件:(UIEvent*)事件
-(无效)触控移动:(NSSet*)触控事件:(UIEvent*))事件
etcGlad是我可以帮助的,检查哪个词被触动看起来很有趣。