ios - 设置绘制到图像时文本的描边宽度

标签 ios image

我找到了一个从文本创建图像的函数,其中包含文本颜色、描边颜色和描边宽度 我可以使用 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;

 }

最佳答案

这可能对你有帮助。

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

关于ios - 设置绘制到图像时文本的描边宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13490656/

相关文章:

html - 使 HTML 正文背景图像透明

c++ - Objective-C 协议(protocol)委托(delegate)不工作

iphone - 自定义类 "unrecognised selector send to instance"

ios - ARC block ,弱和保留计数

iOS 如何摆脱多余的 View 填充?

c# - WPF 模糊图像

html - 我似乎无法正确对齐这些图像

python - 在 python,OpenCV 中使用 GrabCut 进行交互式前景提取

c# - 判断文件是否为图片

ios - 为什么不调用 deinit?