2011-10-18 4 views
3

私は既に持っているUIImageにNSStringとボーダーを描画したいと思います。 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を設定して、のようなものを使用することができますAdd text to CALayer

国境から

- (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(); 
} 

コードを簡単ですが、CALayerプロパティを使用してください:

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

偉大をチェックし、境界線のUIImage
にNSStringのとの境界線を描画する際に、しかし最終的に私はUIImageを置き換えるためにUIButtonを使用し、それ背景Imageを持ち、Label.textを追加することもできます。 – meadlai

1

あなたはCALayersを探しています。

ここでは非常に良いチュートリアルを作成して使用する方法を説明します。

基本的には、画像をバックグラウンドとして新しいCALayerを追加し、それにテキストを描画します。

http://www.raywenderlich.com/2502/introduction-to-calayers-tutorial

1

この機能を使用して、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 –

関連する問題