我试图实现一个受限制的UITableViewCell子类,一切都正常工作,除了UILabel.我设置的约束肯定是强制执行的,但是当限制冲突时,标签内的文本不会调整为较小的字体大小.相反,UILabel的高度被截断,并且字体保持相同的大小,这意味着字母在顶部和底部被切断.
有没有一些方法我必须调用,以使其工作?我认为自动布局会足够聪明,可以自动调整字体大小,所以我为什么会发生这样的事情.
相关代码:
self.label = [[UILabel alloc] initWithFrame:CGRectZero]; self.label.textColor = [UIColor whiteColor]; self.label.translatesAutoresizingMaskIntoConstraints = NO; self.label.textAlignment = NSTextAlignmentCenter; self.label.numberOfLines = 1; [self.contentView addSubview:self.label]; NSLayoutConstraint *otherViewToLabelHorizontalConstraint = // Make sure that the label is always to the right of the other view. [NSLayoutConstraint constraintWithItem:self.label attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:self.otherView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0]; NSLayoutConstraint *aTextFieldToLabelVerticalConstraint = [NSLayoutConstraint constraintWithItem:self.label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:self.aTextField attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0];
基本上,这些约束意在强制执行其他view在左侧的单元格,aTextField位于同一y级别的otherView右侧,标签位于aTextField下方,在右侧的otherView底部.
像往常一样,感谢任何帮助.
解决方法
你需要
myLabel.adjustsFontSizeToFitWidth = YES; myLabel.minimumScaleFactor = .5f;
然后标签字体大小将被自动调整.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。