2011-10-18 97 views
3

我想繪製一個NSString和一個邊框到我已經擁有的UIImage上。我找到了一個將NSString作爲UIImage繪製的方法,但我需要它繪製我提供的圖像。iOS:在UIImage上繪製NSString和邊框

-(UIImage *)imageFromText:(NSString *)text 
{ 
    // set the font type and size 
    UIFont *font = [UIFont systemFontOfSize:20.0]; 
    CGSize size = [text sizeWithFont:font]; 

    // check if UIGraphicsBeginImageContextWithOptions is available (iOS is 4.0+) 
    if (UIGraphicsBeginImageContextWithOptions != NULL) 
     UIGraphicsBeginImageContextWithOptions(size,NO,0.0); 
    else 
     // iOS is < 4.0 
     UIGraphicsBeginImageContext(size); 

    // optional: add a shadow, to avoid clipping the shadow you should make the context size bigger 
    // 
    // CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    // CGContextSetShadowWithColor(ctx, CGSizeMake(1.0, 1.0), 5.0, [[UIColor grayColor] CGColor]); 

    // draw in context, you can use also drawInRect:withFont: 
    [text drawAtPoint:CGPointMake(0.0, 0.0) withFont:font]; 

    // transfer image 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext();  

    return image; 
} 

我該如何修改此方法來提供我自己的背景圖像,以及添加邊框?

回答

5

如果你是在一個UIImageView顯示的UIImage可以設置UIImageView.layer.delegate和使用這樣的:

- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx { 
    CGContextSetFillColorWithColor(ctx, [[UIColor darkTextColor] CGColor]); 

    UIGraphicsPushContext(ctx); 

    [word drawAtPoint:CGPointMake(30.0f, 30.0f) 
      forWidth:200.0f 
      withFont:[UIFont boldSystemFontOfSize:32] 
     lineBreakMode:UILineBreakModeClip]; 

    UIGraphicsPopContext(); 
} 

代碼從Add text to CALayer

邊界很容易,只需使用CALayer屬性:

imageview.layer.borderColor = [UIColor blackColor].CGColor; 
imageview.sublayer.borderWidth = 2.0; 
+0

很大,但最後我用UIButton的更換UIImage的,它有一個背景圖片,也可以在其上添加Label.text。 – meadlai

1

使用此功能繪製的NSString和邊界上的UIImage
邊境檢查CGContextSetRGBStrokeColor

-(UIImage *)imageFromText:(NSString *)text 
{ 
// set the font type and size 
UIFont *font = [UIFont systemFontOfSize:20.0]; 
CGSize size = [text sizeWithFont:font]; 

// check if UIGraphicsBeginImageContextWithOptions is available (iOS is 4.0+) 
if (UIGraphicsBeginImageContextWithOptions != NULL) 
    UIGraphicsBeginImageContextWithOptions(size,NO,0.0); 
else 
    // iOS is < 4.0 
    UIGraphicsBeginImageContext(size); 

// optional: add a shadow, to avoid clipping the shadow you should make the context size bigger 
// 
CGContextRef ctx = UIGraphicsGetCurrentContext(); 
CGContextSetShadowWithColor(ctx, CGSizeMake(1.0, 1.0), 5.0, [[UIColor brownColor] CGColor]); 

// draw in context, you can use also drawInRect:withFont: 
[text drawAtPoint:CGPointMake(0.0, 0.0) withFont:font]; 

//CGImageRef cimg = UIGraphicsGetCurrentContext();  

// transfer image 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height); 
[image drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0]; 

//CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSetRGBStrokeColor(ctx, 2.0, 3.5, 5.0, 1.0); 
CGContextStrokeRect(ctx, rect); 
UIImage *testImg = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext();  

return testImg; 
} 

+0

請通過提出更好的問題來獲得更好的答案:http://stackoverflow.com/questions/how-to-ask –