我正在使用以下函数将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的自动换行创建图像?
你需要在NSString上调用drawInRect:withAttributes:
方法。