【发布时间】:2013-06-14 00:17:56
【问题描述】:
我正在尝试实现一个受约束的UITableViewCell 子类,并且除了UILabel 之外,一切都运行良好。我设置的约束肯定会被强制执行,但是当约束发生冲突时,标签内的文本不会调整为较小的字体大小。相反,UILabel 的高度被截断,字体保持不变,这意味着字母在顶部和底部被截断。
我必须调用一些方法才能使其正常工作吗?我认为 Auto Layout 足够聪明,可以自动调整字体大小,所以我有点不知道为什么会这样。
相关代码:
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];
基本上,这些约束旨在强制一个单元格,其中otherView 在左侧,aTextField 在同一 y 级别上 otherView 的右侧,并且标签低于 aTextField 并otherView的底部右侧。
像往常一样,感谢您对此提供的任何帮助。
【问题讨论】:
-
添加这些约束时,您是否在控制台中收到异常或其他日志消息?
-
不,不过,我最终摆脱了使用此约束的部分。
标签: ios uilabel autolayout nslayoutconstraint