[英] How to write text on image in Objective-C (iOS)?
我想以编程方式制作一个这样的图像:
我有上面的图片和文字.我应该在图像上写文字吗?
我想让它完整.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));
我使用文本大小来计算纯色矩形的原点,但是您可以用任何数字替换它.
无法结合 UIImageView 旋转动画和 tableView 部分重新加载
如何在不同设备的segment控制中管理文本大小(text size)?
使用 RxDatasources 作为数据源时如何填充自定义页眉/页脚视图
Xcode 12.3:为 iOS 模拟器构建,但链接和嵌入式框架是为 iOS + iOS 模拟器构建的
SwiftUI 无法在 HStack 的 Spacer 中点击
Xcode 缺少支持文件 iOS 12.2 (16E227)
使用 Swift 4.0 编译的模块无法在 Swift 4.0.1 中导入