繁体   English   中英

UIImageView中的UILabel文本被切断

[英]UILabel in UIImageView text being cut off

我将UILabel放在自定义UIImageView ,并且文本被截断:

截图

码:

//self.frame.size.width and height are the same value, self is a square
self.displayNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, (self.frame.size.width - 12), (self.frame.size.height - 12))];
self.displayNameLabel.center = CGPointMake((self.frame.size.width / 2), (self.frame.size.height / 2));
self.displayNameLabel.backgroundColor = [UIColor whiteColor];
self.displayNameLabel.textAlignment = UITextAlignmentCenter;
self.displayNameLabel.font = [UIFont fontWithName:labelFont size:40];
self.displayNameLabel.adjustsFontSizeToFitWidth = YES;

我将字体设置为大号,然后使用adjustsFontSizeToFitWidth来确保其尽可能大。 它指出:

但是,如果将此属性设置为YES,并且text属性中的文本超出标签的边界矩形,则接收方将开始减小字体大小,直到字符串适合为止

如果我将label.frame设置为:

self.displayNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, (self.frame.size.width - 0), (self.frame.size.height - 0))];

然后按预期工作。

如何将框架设置为:

self.frame.size.width
self.frame.size.height 

代替:

self.frame.size.width - 14 
self.frame.size.height - 14

工作?

为什么它被夹在顶部?

您可能需要相应地设置标签的autoresizingMask属性,以便在调整图像大小时,可以正确调整标签的框架。 或者,在将图像添加到任何视图之后,应在标签上调用setNeedsDisplay

属性adjustsFontSizeToFitWidth将仅使标签调整字体大小,直到字符串的宽度适合框架。 如果标签的高度不足以容纳最终的字体大小,它将被裁剪。

暂无
暂无

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

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