2011-11-15 9 views
6

iphoneアプリケーションをいくつかのグリーティングカードアプリケーションと似て、事前準備された背景画像(カード)にテキストを書くことができます。Xcodeの画像にテキストを追加する

  • このテキストを書くにはどうすればよいですか?
  • 背景画像とテキストを1つの画像ファイルに保存するにはどうすればよいですか?

ありがとうございます。

+1

コアグラフィックスが正しい方向かもしれないスーパー直感的です。 – dasdom

+1

ステータスバーを非表示にして空のコントローラを作成し、編集が完了したらスクリーンショットを作成することができます。私はUIImagePNGRepresentationがあなたに必要なものだと思います。 –

+0

あなたの答えをありがとう。はい、私はあなたのソリューションは、コアグラフィックスを使用するよりはるかに簡単だと思う、私はそれに焦点を当てます。私が理解すれば、あなたはuiviewの背景イメージを準備し、その上に透明なBachgroundを持つuiTextfieldを追加し、ユーザーにテキストを入力させてからscereenshotをさせることを意味します。そうかもしれない? – hafedh

答えて

10

文字列を画像に書き込む方法です。フォントサイズやその他のパラメータを微調整して、好きなように設定することができます。

/* Creates an image with a home-grown graphics context, burns the supplied string into it. */ 
- (UIImage *)burnTextIntoImage:(NSString *)text :(UIImage *)img { 

UIGraphicsBeginImageContext(img.size); 

CGRect aRectangle = CGRectMake(0,0, img.size.width, img.size.height); 
[img drawInRect:aRectangle]; 

[[UIColor redColor] set];   // set text color 
NSInteger fontSize = 14; 
if ([text length] > 200) { 
    fontSize = 10; 
} 
UIFont *font = [UIFont boldSystemFontOfSize: fontSize];  // set text font 

[ text drawInRect : aRectangle      // render the text 
     withFont : font 
    lineBreakMode : UILineBreakModeTailTruncation // clip overflow from end of last line 
     alignment : UITextAlignmentCenter ]; 

UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext(); // extract the image 
UIGraphicsEndImageContext();  // clean up the context. 
return theImage; 
} 
+0

ありがとうRayfleck、素晴らしいよ! – hafedh

+0

@Rayfleckこれは古い回答であることは分かっていますが、今日はそれを見つけただけで大変効果的です。ありがとうございます!私はちょうど1つの質問があります。このコードを変更して文字列を垂直方向に描画する方法はありますか?(テキストは反時計回りに90度回転します) – Ryan

+0

@Ryan - 最初にイメージをCLOCKwiseに回転させ、テキストを焼き付けてから戻します。回転を行う便利なUIImageの拡張機能については、http://www.catamount.com/forums/viewtopic.php?f=21&t=967をご覧ください。 – Rayfleck

5

ありがとうRayfleck!出来た。

は網膜ディスプレイとオプションの互換性を追加するには(画像の '@ 2xの' バージョン間に修正途切れ途切れの文字をスケールアップ):

置き換える:条件付き

UIGraphicsBeginImageContext(img.size); 

if (UIGraphicsBeginImageContextWithOptions != NULL) 
    UIGraphicsBeginImageContextWithOptions(img.size,NO,0.0); 
else 
    UIGraphicsBeginImageContext(img.size); 
+0

おかげでRob、これはうまく動作します。それは正確に何をしていますか? – Ryan

+0

喜んで助けてください! IF文は、UIGraphicsBeginImageContextWithOptionsが利用可能かどうかを調べるための簡単なチェックです(ios4以降)。もしそうでなければ、それは網膜ではないと考えることができます。そうであれば、オプションを渡します。1 - イメージのサイズ(テキスト領域)。 2 - 透明な背景(不透明= NO)。デバイスが独自のスケールを使用するように指示する3 - '0.0'(網膜ディスプレイはデフォルトで2.0(?)になります) – Rob

0

iOS7用の更新...

/* Creates an image with a home-grown graphics context, burns the supplied string into it. */ 
    - (UIImage *)burnTextIntoImage:(NSString *)text :(UIImage *)img { 

     UIGraphicsBeginImageContext(img.size); 

     CGRect aRectangle = CGRectMake(0,0, img.size.width, img.size.height); 
     [img drawInRect:aRectangle]; 

     [[UIColor redColor] set];   // set text color 
     NSInteger fontSize = 14; 
     if ([text length] > 200) { 
      fontSize = 10; 
     } 

     UIFont *font = [UIFont fontWithName:@"Courier" size:fontSize]; 
     NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; 
     paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail; 


     paragraphStyle.alignment = NSTextAlignmentRight; 
     NSDictionary *attributes = @{ NSFontAttributeName: font, 
             NSParagraphStyleAttributeName: paragraphStyle, 
             NSForegroundColorAttributeName: [UIColor whiteColor]}; 
     [text drawInRect:aRectangle withAttributes:attributes]; 

     UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext(); // extract the image 
     UIGraphicsEndImageContext();  // clean up the context. 
     return theImage; 
    } 
0

このアプローチは、アカウントに、画面のスケールを取り、それが

UIImage * img = ... 
    UIImageView * iV = [[UIImageView alloc] initWithImage:img]; 
    UILabel * l = [[UILabel alloc] initWithFrame:iV.bounds]; 
    l.textAlignment = ...; 
    l.adjustsFontSizeToFitWidth = YES; 
    l.textColor = ...; 
    l.font = ...; 
    l.text = ...; 

    [iV addSubview:l]; 

    UIGraphicsBeginImageContextWithOptions(iV.bounds.size, NO, 0); 
    [iV.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage * finalImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return finalImage 
関連する問題