2014-01-14 34 views
0

我有這個功能,基本上增加了一個給定的圖像低於給定的文字,我願textRect一輪的角落,你能幫助我瞭解如何在這段代碼轉向的CGRect的邊角圓像的UIView

使用 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; 
} 

回答

0

您的圖片背景是當前的背景下,所以只是構建圓角的矩形與bezierPathWithRoundedRect:cornerRadius:,設置您的填充顏色,並告訴貝塞爾路徑fill。這是一行代碼(以及兩個如果算上設置填充顏色):

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

隨意這裏的玩數字遊戲;例如,您可能會喜歡不同的圓角半徑。實驗!

+0

你能幫我一下代碼嗎?我刪除CGRect? –

+0

CGRect只是一個路徑。我在說,使用不同的路徑。你在說'CGContextFillRect'。這繪製了一個填充的矩形。我建議你畫一個填充的圓角矩形。 – matt