2011-11-15 21 views
6

圖像添加文本我想打一個iPhone應用程序類似,一些賀卡應用,在那裏我可以寫文字了一些預先準備好的背景圖片(卡)。超過上的Xcode

  • 怎麼能在我寫這篇文字?
  • 如何保存的背景圖像+上一個圖像文件中的文本?

感謝。

+1

核心圖形可能是正確的方向。 – dasdom

+1

你可以製作一個隱藏狀態欄的空白控制器,並在完成編輯後製作截圖。我認爲UIImagePNGRepresentation是你需要的。 –

+0

感謝您的回答。是的,我認爲你的解決方案比使用Core Graphics更容易,我會專注於此。如果我明白了,你的意思是我在一個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我知道這是一個古老的答案,但我今天才發現它,它工作的很棒,所以謝謝!我剛剛有一個問題。有沒有辦法修改這段代碼來垂直繪製字符串?(文本逆時針旋轉90度) – Ryan

+0

@Ryan - 首先按CLOCKwise旋轉圖像,然後在文本中刻錄,然後再旋轉它。看看這裏http://www.catamount.com/forums/viewtopic.php?f=21&t=967一些方便的UIImage擴展來做旋轉。 – Rayfleck

5

謝謝Rayfleck!有效。

要添加具有視網膜顯示器可選兼容性(在 '@ 2X' 版本圖像的修正波濤洶涌字母擴展):

取代:

UIGraphicsBeginImageContext(img.size); 

帶有條件:

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

感謝羅布,這工作很好。它到底在做什麼? – 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