我找到了一个函数,可以使用文本颜色、笔划颜色和笔划宽度从文本创建图像我可以使用 和 设置文本颜色和笔划颜色CGContextSetRGBFillColor
,CGContextSetRGBStrokeColor
但我不知道如何设置笔划宽度的文本。请帮我解决这个问题。这是我的示例代码,感谢您的帮助。
-(UIImage *)imageFromText:(NSString *)text withFont: (NSString *)fontName{
// set the font type and size
UIFont *font = [UIFont fontWithName:fontName size:100];
CGSize size = [text sizeWithFont:font];
CGSize newSize = CGSizeMake(size.width+30, size.height+20);
// check if UIGraphicsBeginImageContextWithOptions is available (iOS is 4.0+)
if (UIGraphicsBeginImageContextWithOptions != NULL)
UIGraphicsBeginImageContextWithOptions(newSize,NO,0.0);
else
// iOS is < 4.0
UIGraphicsBeginImageContext(newSize);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetCharacterSpacing(ctx, 10);
CGContextSetTextDrawingMode (ctx, kCGTextFillStroke);
CGContextSetRGBFillColor (ctx, 0.1, 0.2, 0.3, 1); // 6
CGContextSetRGBStrokeColor (ctx, 0.2, 0.8, 0.6, 1);
// draw in context, you can use also drawInRect:withFont:
[text drawAtPoint:CGPointMake(15.0, 10.0) withFont:font];
// transfer image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}