绘制到图像时设置文本的笔触宽度

时间:2012-11-21 10:00:01

标签: ios image

我发现了一个从文本中创建图像的功能,包括文本颜色,笔触颜色和笔划宽度 我可以使用CGContextSetRGBFillColorCGContextSetRGBStrokeColor设置笔触的文本颜色和颜色,但我不知道如何设置文本笔划的宽度。请帮我解决这个问题。 以下是我的示例代码,感谢您的帮助。

-(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;

 }

1 个答案:

答案 0 :(得分:1)

这可能对你有帮助。

NSString *str= @"YOUR String";
UIFont *font = [UIFont systemFontOfSize:16.0];
CGSize stringSize = [textToDraw sizeWithFont:font constrainedToSize:CGSizeMake(301, 10000) lineBreakMode:UILineBreakModeWordWrap];
CGRect Rect = CGRectMake(0, 0, stringSize.width , stringSize.height);
[str drawInRect:Rect withFont:[UIFont systemFontOfSize:16] lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
相关问题