像UIView一样转向CGRect的角落

时间:2014-01-14 03:12:54

标签: uibezierpath cgrect uigraphicscontext

我有这个功能,基本上在给定图像下面添加一个给定的文本,我想让textRect的角落圆,你能帮我理解如何在这段代码中使用UIBezierPath

-(UIImage*) overlapText:(NSString*) p_text inImage:(UIImage*) p_image atPoint:(CGPoint) p_point
{
    p_point.y += p_image.size.height-5;

    UIFont *font = [UIFont boldSystemFontOfSize:11];
    UIGraphicsBeginImageContext(CGSizeMake(p_image.size.width+15,p_image.size.height+15));
    CGFloat imageX = (p_image.size.width+10)/2 - (p_image.size.width/2);
    [p_image drawInRect:CGRectMake(imageX,0,p_image.size.width,p_image.size.height)];

    CGRect textRect = CGRectMake(0, p_point.y, p_image.size.width+15, p_image.size.height+10);

    [[UIColor colorWithRed:(70/255.0) green:(70/255.0) blue:(70/255.0) alpha:1] set];
    CGContextFillRect(UIGraphicsGetCurrentContext(), textRect);


    [[UIColor whiteColor] set];
    [p_text drawInRect:textRect withFont:font lineBreakMode:NSLineBreakByTruncatingTail alignment:NSTextAlignmentCenter];


    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

1 个答案:

答案 0 :(得分:0)

您的图片上下文是当前上下文,因此只需使用bezierPathWithRoundedRect:cornerRadius:构建圆角矩形,设置填充颜色,并告知fill的bezier路径。这是一行代码(如果计算填充颜色,则为两行):

[[UIColor colorWithRed:(70/255.0) green:(70/255.0) blue:(70/255.0) alpha:1] set];
[[UIBezierPath bezierPathWithRoundedRect:textRect cornerRadius:5] fill];

随意玩这里的数字;例如,您可能喜欢不同的角半径。实验!