繁体   English   中英

如何在矩形中绘制和缩放文本

[英]how to draw and scale text in rectangle

我的问题很简单。 我有一个矩形,例如(0.0,0.0,300,45),需要在这个矩形的中心画一个字符串,例如“Text”。 “文本”的高度必须符合矩形的高度。

困难的部分是:矩形可以缩放。 “文本”的大小必须缩放为矩形的大小。

我可以在矩形的中心绘制字符串作为下面的编码,但难点是我无法管理“文本”字体大小更改为矩形大小。

[@“Text”drawInRect:textRect withFont:font];

我想在矩形中绘制文本,而不是标签,用户可以用手指缩放矩形大小,最后,我将在图像上按比例绘制文本,我认为标签不适用于这些功能。

谁有好的解决方案?

谢谢!

更新:

实际上,我需要在大尺寸图像上绘制文字,而不仅仅是在iPhone屏幕上显示,请看下面的代码:

UIImageVIew *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320, 320)];

imageView.image = [UIImage imageNamed:@"LargeSizeImage"]; // Image size = 2048 *2048

[self.view addSubview:imageView];

UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 45)];

textLabel.text = @"Text";

[self.view addSubviews:textLabel];

// Draw text on image

UIGraphicsBeginImageCurrentContext(imageView.image.size);(2048 * 2048)

[imageView.image drawInRect:CGRectMake(0, 0, 2048, 2048)];

CGRect scaleRect = CGRectMake(10 * scaleFactor, 10 *scaleFactor, textLabel.bounds.size.width * scaleFactor, text.Label.bounds.size.height *scaleFactor);

UIFont *font = [UIFont systemOfSize:**?**];

[textLabel.text drawInRect:scaleRect withFont:font];

........... ........... ...........

我的问题是如何确定字体大小?

UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 300, 45)];
text.backgroundColor = [UIColor clearColor];
text.font = [UIFont systemFontOfSize:45];
text.textAlignment =  UITextAlignmentCenter;
text.text = @"TEXT";
[self.view addSubview:text];

可能会得到你感兴趣的效果。

编辑

实际上,如果调整方块大小,您可以动态调整文本大小。 在您执行调整大小的方法中,只需为UILabel添加这些代码行以触发调整大小。

text.frame = CGRectMake(0.0, 0.0, rect.frame.size.width, rect.frame.size.height);
text.font = [UIFont systemFontOfSize:rect.frame.size.height];

rect是我用来描述你正在调整大小的矩形。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM