【发布时间】:2012-02-19 09:53:07
【问题描述】:
我正在使用以下函数将 NSString 转换为图像。
-(UIImage *)imageFromText:(NSString *)text FontName:(UIFont *)font
{
// set the font type and size
//UIFont *font = [UIFont systemFontOfSize:20.0];
CGSize size = [text sizeWithFont:font];
UIGraphicsBeginImageContext(size);
[text drawAtPoint:CGPointMake(0.0, 0.0) withFont:font];
// transfer image
CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
效果很好。问题是当字符串包含长文本时,它会创建一个宽度太大的图像。如果文本超出范围,我想应用自动换行功能。
那么如何创建带有 NSString 自动换行的图像?
【问题讨论】:
标签: iphone uiimage nsstring word-wrap