我想以编程方式制作一个这样的图像:
我有上面的图片和文字.我应该在图像上写文字吗?
我想让它完整.png图像(图像+标签)并将其设置为按钮的背景.
我想以编程方式制作一个这样的图像:
我有上面的图片和文字.我应该在图像上写文字吗?
我想让它完整.png图像(图像+标签)并将其设置为按钮的背景.
在图像内绘制文本并返回结果图像:
+(UIImage*) drawText:(NSString*) text
inImage:(UIImage*) image
atPoint:(CGPoint) point
{
UIFont *font = [UIFont boldSystemFontOfSize:12];
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
CGRect rect = CGRectMake(point.x, point.y, image.size.width, image.size.height);
[[UIColor whiteColor] set];
[text drawInRect:CGRectIntegral(rect) withFont:font];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
用法:
// note: replace "ImageUtils" with the class where you pasted the method above
UIImage *img = [ImageUtils drawText:@"Some text"
inImage:img
atPoint:CGPointMake(0, 0)];
将图像中文本的原点从0,0更改为您喜欢的任意点.
要在文本后面绘制一个纯色矩形,请在第[[UIColor whiteColor] set];
行之前添加以下内容:
[[UIColor brownColor] set];
CGContextFillRect(UIGraphicsGetCurrentContext(),
CGRectMake(0, (image.size.height-[text sizeWithFont:font].height),
image.size.width, image.size.height));
我使用文本大小来计算纯色矩形的原点,但是您可以用任何数字替换它.