【发布时间】:2012-09-20 11:25:24
【问题描述】:
我正在将文本写入从 iDevice 的相机拍摄或从照片库中选择的图像上,但我需要根据图像的宽度/高度缩放字体大小。这是我当前的代码: UIGraphicsBeginImageContext(img.size);
CGRect aRectangle = CGRectMake(0,0, img.size.width, img.size.height);
[img drawInRect:aRectangle];
[[UIColor whiteColor] set]; // set text color
NSInteger fontSize = 45;
UIFont *font = [UIFont systemFontOfSize:fontSize];// set text font
[ text drawInRect : aRectangle // render the text
withFont : font
lineBreakMode : UILineBreakModeTailTruncation // clip overflow from end of last line
alignment : UITextAlignmentCenter ];
UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext(); // extract the image
UIGraphicsEndImageContext(); // clean up the context.
return theImage;</i>
【问题讨论】:
-
您希望文本是单行还是多行?
-
加入 fontScaling 聊天室。
-
@Kent 我收到这条消息:您必须在 Stack Overflow 上拥有 20 名声望才能在此处交谈。请参阅常见问题解答。我还是个新成员。
-
啊。真可惜。所以更多的问题。您希望文本是图像的全高还是有其他限制?
-
@Kent 对于这部分文本,我希望它就像图像的中心标题一样。就像尺寸为 960x750 的图像一样,我会将字体大小设置为 45,所以它正好在中间并且足够大,可以看到,但问题是我不能让字体大小始终为 45,因为图像的大小变化。
标签: xcode nsstring uiimage font-size autoresize