如何在UILabel上添加一个三角形,使其看起来像工具提示?

时间:2015-02-09 20:49:02

标签: ios objective-c uilabel core-graphics

我想用一些自定义代码制作像UILabel这样的工具提示。我设法为UILabel添加了一个三角形,但因为它超出了UILabel本身的界限(或框架)。它没有画在屏幕上。

这是我的方法:

- (void) addTriangleTipToLayer: (CALayer *) targetLayer{
[targetLayer setBackgroundColor: [UIColor whiteColor].CGColor];

CGPoint startPoint = CGPointMake(0, targetLayer.frame.size.height);
CGPoint endPoint = CGPointMake(15, targetLayer.frame.size.height);
UIBezierPath *trianglePath = [UIBezierPath bezierPath];

CGFloat middleX = (startPoint.x + endPoint.x) / 2;
CGFloat middleY = 7.5 * tan(M_PI / 3);

CGPoint middlePoint = CGPointMake(middleX, -middleY);

[trianglePath moveToPoint:startPoint];
[trianglePath addLineToPoint:middlePoint];
[trianglePath addLineToPoint:endPoint];
[trianglePath closePath];
CGAffineTransform rotate = CGAffineTransformMakeRotation(180);
[trianglePath applyTransform:rotate];

CAShapeLayer *triangleLayer = [CAShapeLayer layer];
[triangleLayer setFillColor: [UIColor whiteColor].CGColor];
[triangleLayer setPath:trianglePath.CGPath];
[targetLayer addSublayer:triangleLayer];

}

我还将标签转换为图片内容,以用作Google地图标记图标。这是我对这个UILabel所做的其他事情。

UILabel *klabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
[klabel setFont: [UIFont fontWithName:@"Roboto-Light" size:9]];
[klabel setText: @"Tower"];
// here I apply the add triangle function to the uilabel.layer
[self addTriangleTipToLayer:klabel.layer];
//set the label to fit text content before rendered as a image
[klabel sizeToFit];
//convert the uilabel to image content
UIGraphicsBeginImageContextWithOptions(klabel.bounds.size, NO, [[UIScreen mainScreen] scale]);
[klabel.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * kicon = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// the rest of code is just use the image content as marker icon on google map.
kaliaMarker = [[GMSMarker alloc] init];
kaliaMarker.tappable = NO;
kaliaMarker.position = CLLocationCoordinate2DMake(21.283652, -157.836971);
kaliaMarker.infoWindowAnchor = CGPointMake(0.5, 0.5);
kaliaMarker.icon = kicon;
kaliaMarker.map = self.mapView;

targetLayermyLabel.layer,这是我添加三角形的地方。问题是计算中的middleY-12.4,我猜它已经超出了我的UILabel的可见边界/框架,你只是看不到它。

我想知道我应该做什么或者是什么方法来增加我的UILabel的可见部分以显示这个小三角形。

谢谢你们,'帮助!

P.S。它看起来应该是这样的。

enter image description here

但它看起来像这样。如果我将路径旋转90度,我可以看到半角三角形,当然它在那里。因此,对于180度,整个三角形是超出界限,无法看到.. enter image description here

3 个答案:

答案 0 :(得分:2)

实际上你的箭头在那里,你的背景是白色的,所以你看不到它。为图层设置填充颜色。

[triangleLayer setFillColor:[[UIColor redColor] CGColor]];

答案 1 :(得分:2)

这个稍微修改过的代码运行正常。我将方法的参数更改为UIView,以便我可以访问其背景颜色以使三角形具有相同的颜色,并使三角形的y值与视图的高度成比例。

- (void) addTriangleTipToLayer: (UIView *) view{
    CALayer *targetLayer = view.layer;
    [targetLayer setBackgroundColor: [UIColor whiteColor].CGColor];

    CGPoint startPoint = CGPointMake(0, targetLayer.frame.size.height);
    CGPoint endPoint = CGPointMake(15, targetLayer.frame.size.height);
    UIBezierPath *trianglePath = [UIBezierPath bezierPath];

    CGFloat middleX = (startPoint.x + endPoint.x) / 2;
    CGFloat middleY = targetLayer.frame.size.height * 1.8;

    CGPoint middlePoint = CGPointMake(middleX, middleY);

    [trianglePath moveToPoint:startPoint];
    [trianglePath addLineToPoint:middlePoint];
    [trianglePath addLineToPoint:endPoint];
    [trianglePath closePath];

    CAShapeLayer *triangleLayer = [CAShapeLayer layer];
    [triangleLayer setFillColor: view.backgroundColor.CGColor];
    [triangleLayer setPath:trianglePath.CGPath];
    [targetLayer addSublayer:triangleLayer];
}

答案 2 :(得分:0)

我在代码中发现了问题。

//convert the uilabel to image content
UIGraphicsBeginImageContextWithOptions(klabel.bounds.size, NO, [[UIScreen mainScreen] scale]);

没有足够的空间用于添加到uilabel的箭头图层。 我也以错误的方式添加三角形。正确的代码应该是:

[targetLayer setBackgroundColor: [UIColor whiteColor].CGColor];

CGFloat side = targetLayer.frame.size.height;

CGPoint startPoint = CGPointMake(targetLayer.frame.size.width / 2 - side / 2, side);
CGPoint endPoint = CGPointMake(targetLayer.frame.size.width / 2 + side / 2, side);
UIBezierPath *trianglePath = [UIBezierPath bezierPath];

CGFloat middleX = targetLayer.frame.size.width / 2;
CGFloat middleY = (side / 2) * tan(M_PI / 3) + side;
CGPoint middlePoint = CGPointMake(middleX, middleY);

[trianglePath moveToPoint:startPoint];
[trianglePath addLineToPoint:middlePoint];
[trianglePath addLineToPoint:endPoint];
[trianglePath closePath];

CAShapeLayer *triangleLayer = [CAShapeLayer layer];
[triangleLayer setFillColor: [UIColor whiteColor].CGColor];
[triangleLayer setPath:trianglePath.CGPath];
[targetLayer addSublayer:triangleLayer];

这将为标签本身添加等边三角形中心。那么绘制转换为图像的代码应该是:

    //convert the uilabel to image content
UIGraphicsBeginImageContextWithOptions(CGRectMake(self.klabel.bounds.size.width, 2 * self.klabel.bounds.size.height), NO, [[UIScreen mainScreen] scale]);

end result will look like this谢谢你们这些人'帮助!