gpt4 book ai didi

iphone - 为什么此代码中需要 "sizeToFit"调用?

转载 作者:行者123 更新时间:2023-11-28 23:17:42 26 4
gpt4 key购买 nike

任何人都能够突出显示为什么在下面的代码中需要“[aLabel sizeToFit]”行,摘自 here .

也就是说,创建的 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
}

最佳答案

来自文档:

Call this method when you want to resize the current view so that it uses the most appropriate amount of space. Specific UIKit views resize themselves according to their own internal needs. In some cases, if a view does not have a superview, it may size itself to the screen bounds. Thus, if you want a given view to size itself to its parent view, you should add it to the parent view before calling this method.

选择适合您情况的正确选项。

关于iphone - 为什么此代码中需要 "sizeToFit"调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5254585/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com