iOS CGContextRef在UIImage上绘制描边颜色和边框文本

时间:2015-05-18 10:49:00

标签: ios objective-c iphone uiimage cgcontextref

我正在尝试在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;

enter image description here

0 个答案:

没有答案