我将 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
以确保它尽可能大。它指出:
If this property is set to YES, however, and the text in the text property exceeds the label’s bounding rectangle, the receiver starts reducing the font size until the string fits
如果我将 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
工作?
为什么会被夹在上面?
adjustsFontSizeToFitWidth
属性只会导致标签调整字体大小,直到字符串的宽度适合框架。如果标签的高度不足以容纳最终的字体大小,它将被剪裁。
关于objective-c - UIImageView 文本中的 UILabel 被截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12710980/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |