1

我正在尝试在 UIImage 上实现带有颜色和边框的绘制描边文本,但我无法获得解决方案。

我已经将此THLabel用于文本的笔画和边框,它工作正常,但只有当我将该代码复制到绘图图像中时,它才对我有用。

请参阅相同的屏幕截图。有什么建议么?

在 UIImage 方法上绘制文本:

- (UIImage *) drawText:(NSString*)text
          inImage:(UIImage*)image
          atPoint:(NSString *)location{

CGFloat fontSize = image.size.width*3/30.0;

NSString *fontName = @"Arial";

int w = image.size.width;
int h = image.size.height;
UIFont *font=[UIFont fontWithName:fontName size:fontSize];
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];

int textFrameY = 25;
int textFrameX = 5;
CGSize aSize = [text sizeWithFont:font];
CGRect rect = CGRectMake(textFrameX,textFrameY, 80, 30);

CGSize lSize = [location sizeWithFont:font];
CGRect rect1 = CGRectMake(image.size.width-lSize.width-fontSize-textFrameX, h-(fontSize*3)-textFrameY, w, h);

[[UIColor blackColor] set];
CGContextFillRect(UIGraphicsGetCurrentContext(), rect1);
CGContextFillRect(UIGraphicsGetCurrentContext(), rect);
[[UIColor whiteColor] set];

[text drawInRect:CGRectIntegral(rect) withFont:font];
[location drawInRect:CGRectIntegral(rect1) withFont:font];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;}

用于文本方法的笔触颜色和边框的 THLabel 代码:

// Demonstrate stroke.
self.label3.strokeColor = kStrokeColor;
self.label3.strokeSize = kStrokeSize;

在此处输入图像描述

4

0 回答 0