为什么“sizeToFit”是这段代码中需要调用吗?
任何人都能够强调为什么下面的代码中需要“[aLabel sizeToFit]”行,摘自 此处。
也就是说,创建的 CGRect 已经根据文本字体大小调整了大小,那么为什么还需要 sizeToFit 呢?或者是因为虽然他们计算了正确的 CGRect 大小,但他们实际上还没有将预先商定的字体(系统字体)设置为 UILabel? (因此有效地调用 sizeToFit 而不是设置字体)
- (CGRect)RAD_frameForCellLabelWithSystemFontOfSize:(CGFloat)size {
CGFloat width = [UIScreen mainScreen].bounds.size.width - 50;
CGFloat height = [self RAD_textHeightForSystemFontOfSize:size] + 10.0;
return CGRectMake(10.0f, 10.0f, width, height);
}
- (void)RAD_resizeLabel:(UILabel *)aLabel WithSystemFontOfSize:(CGFloat)size {
aLabel.frame = [self RAD_frameForCellLabelWithSystemFontOfSize:size];
aLabel.text = self;
[aLabel sizeToFit]; // WHY IS THIS REQUIRED
}
Anyone able to highlight why the "[aLabel sizeToFit]" line is required in this code below, taken from here.
That is, the CGRect created has already been sized based on the text font size, so why would the sizeToFit be required? Or is it because whilst they calculated the correct CGRect size they actually didn't set the pre-agreed font (system font) to the UILabel yet? (and therefore effectively called sizeToFit instead of setting the font)
- (CGRect)RAD_frameForCellLabelWithSystemFontOfSize:(CGFloat)size {
CGFloat width = [UIScreen mainScreen].bounds.size.width - 50;
CGFloat height = [self RAD_textHeightForSystemFontOfSize:size] + 10.0;
return CGRectMake(10.0f, 10.0f, width, height);
}
- (void)RAD_resizeLabel:(UILabel *)aLabel WithSystemFontOfSize:(CGFloat)size {
aLabel.frame = [self RAD_frameForCellLabelWithSystemFontOfSize:size];
aLabel.text = self;
[aLabel sizeToFit]; // WHY IS THIS REQUIRED
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自文档:
选择适合您情况的正确选项。
From docs:
Choose the right one, that is appropriate for your case.