我想在UIImage
中繪製NSString
變量的內容,但我絕對不知道如何執行此操作。我需要編寫一個方法,該方法將接收NSString
作爲參數,並返回一個UIImage
及其中的文本。如何使用NSString繪圖功能從文本創建UIImage
36
A
回答
88
結束它你可以試試這個:(更新爲iOS 4)
-(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;
}
要叫它:
UIImage *image = [self imageFromText:@"This is a text"];
2
開始的圖像內容與UIGraphicsBeginImageContext
,畫它,與UIGraphicsGetImageFromCurrentImageContext
得到的圖像,與UIGraphicsEndImageContext
13
添加斯威夫特版本,還爲您提供了更多的選擇:
class func sizeOfAttributeString(str: NSAttributedString, maxWidth: CGFloat) -> CGSize {
let size = str.boundingRectWithSize(CGSizeMake(maxWidth, 1000), options:(NSStringDrawingOptions.UsesLineFragmentOrigin), context:nil).size
return size
}
class func imageFromText(text:NSString, font:UIFont, maxWidth:CGFloat, color:UIColor) -> UIImage {
let paragraph = NSMutableParagraphStyle()
paragraph.lineBreakMode = NSLineBreakMode.ByWordWrapping
paragraph.alignment = .Center // potentially this can be an input param too, but i guess in most use cases we want center align
let attributedString = NSAttributedString(string: text, attributes: [NSFontAttributeName: font, NSForegroundColorAttributeName: color, NSParagraphStyleAttributeName:paragraph])
let size = sizeOfAttributeString(attributedString, maxWidth: maxWidth)
UIGraphicsBeginImageContextWithOptions(size, false , 0.0)
attributedString.drawInRect(CGRectMake(0, 0, size.width, size.height))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
0
雨燕3.1
func sizeOfAttributeString(str: NSAttributedString, maxWidth: CGFloat) -> CGSize {
let size = str.boundingRect(with: CGSize(width: maxWidth, height: 1000), options:(NSStringDrawingOptions.usesLineFragmentOrigin), context:nil).size
return size
}
func imageFromText(text:NSString, font:UIFont, maxWidth:CGFloat, color:UIColor) -> UIImage {
let paragraph = NSMutableParagraphStyle()
paragraph.lineBreakMode = NSLineBreakMode.byWordWrapping
paragraph.alignment = .center
let attributedString = NSAttributedString(string: text as String, attributes: [NSFontAttributeName: font, NSForegroundColorAttributeName: color, NSParagraphStyleAttributeName:paragraph])
let size = sizeOfAttributeString(str: attributedString, maxWidth: maxWidth)
UIGraphicsBeginImageContextWithOptions(size, false , 0.0)
attributedString.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
幸得鮑蕾爲美麗的答案。
相關問題
- 1. 如何使用NSString繪圖功能從文本創建UIImage並將此圖像與另一個UIImage結合
- 2. 從NSString創建一個UIImage
- 3. 創建從PNG的OpenGL繪圖的UIImage
- 4. 將NSString繪製爲UIImage
- 5. 如何在圖像中創建圓圈....不使用繪圖功能?
- 6. ASP.NET:如何使用文本框創建搜索功能?
- 7. 我如何繪製一個圖像到另一個UIImage來創建一個UIImage
- 8. 從XIB創建UIImage如何構建?
- 9. 如何使用iOS繪圖創建餅圖並從文本字段獲取繪圖值
- 10. 如何從當前圖形上下文創建UIImage?
- 11. 如何使用DPI設置創建UIImage?
- 12. 從UIImage創建CGPath
- 13. 從MKMapView創建UIImage
- 14. 從UILabel創建UIImage?
- 15. 從點創建UIImage
- 16. 從UIImage創建MSSticker
- 17. 使用圖像創建功能
- 18. iOS:在UIImage上繪製NSString和邊框
- 19. 從功能獲取UIImage
- 20. 如何從字節創建UIImage?
- 21. 從NSString創建NSDate。
- 22. 如何使用stapplingin功能創建功能?
- 23. 如何使用UI功能創建全局級功能
- 24. 如何從資產文件夾中的圖像創建繪圖?
- 25. 使用phpMyAdmin創建功能
- 26. 如何使用ViewPager功能創建水平滾動視圖?
- 27. 如何在使用imageWithData創建UIImage時緩存圖像
- 28. 如何創建'onclick'功能?
- 29. 如何從更大的功能創建更小的功能?
- 30. 創建從位圖繪製
對於iOS4 +,如果縮放比例不是1.0(視網膜顯示爲2.0),則應創建縮放比例爲0.0的上下文,以獲得主屏幕的縮放比例:\t UIGraphicsBeginImageContextWithOptions(size,NO,0.0); – valexa 2010-10-03 16:02:05
@rpmx轉義字符如\ n新行未被檢測到? – Yadnesh 2012-01-17 09:03:56
@Yadnesh:使用:[text drawInRect:(CGRect)rect withFont:(UIFont *)font]; – rpmx 2012-01-21 23:57:48