区块链技术博客
www.b2bchain.cn

sizeToFit & sizeThatFits:求职学习资料

本文介绍了sizeToFit & sizeThatFits:求职学习资料,有助于帮助完成毕业设计以及求职,是一篇很好的资料。

对技术面试,学习经验等有一些体会,在此分享。

以前总觉的sizeToFit和sizeThatFits神乎其神的,看了源码发现原来很简单winObjc源码

源码

直接上源码:

//UIView.mm - (void)sizeToFit {     CGRect curSize;      //  Get current size     curSize = [self frame];      //  Get preferred size     curSize.size = [self sizeThatFits:curSize.size];     [self setFrame:curSize]; }
//UIView.mm - (CGSize)sizeThatFits:(CGSize)curSize {     CGRect curBounds;     curBounds = [self bounds];     return curBounds.size; }

可以重写自己一个UIView的sizeThatFits:,返回一个size,当UIView调用sizeToFit时,内部其实就是调用sizeThatFits:将自身的size设为返回的size

UILabel应用

UILabel在设置完text字符串后,可以调用sizeToFit根据内部字符串调整UILabel的大小,比如:

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 0, 0)];     label.backgroundColor = UIColor.redColor;     [self.view addSubview:label];     label.text = @"hello world!";     [label sizeToFit];

sizeToFit & sizeThatFits:

对于UILabel如何计算的,源码如下:

- (CGSize)sizeThatFits:(CGSize)curSize {     CGSize ret = CGSizeZero;      if ([_text length] != 0) {         if (self.numberOfLines != 0) {             CGFloat lineHeight = [_font ascender] - [_font descender];             curSize.height = lineHeight * self.numberOfLines;              if (self.numberOfLines == 1) {                 curSize.width = 0;             }         } else {             curSize.height = 0;         }          //  Calculate the size of the text set in our label         ret = [_text sizeWithFont:_font constrainedToSize:CGSizeMake(curSize.width, curSize.height) lineBreakMode:self.lineBreakMode];     }      return ret; }

以前总觉的sizeToFit和sizeThatFits神乎其神的,看了源码发现原来很简单winObjc源码

源码

直接上源码:

//UIView.mm - (void)sizeToFit {     CGRect curSize;      //  Get current size     curSize = [self frame];      //  Get preferred size     curSize.size = [self sizeThatFits:curSize.size];     [self setFrame:curSize]; }
//UIView.mm - (CGSize)sizeThatFits:(CGSize)curSize {     CGRect curBounds;     curBounds = [self bounds];     return curBounds.size; }

可以重写自己一个UIView的sizeThatFits:,返回一个size,当UIView调用sizeToFit时,内部其实就是调用sizeThatFits:将自身的size设为返回的size

UILabel应用

UILabel在设置完text字符串后,可以调用sizeToFit根据内部字符串调整UILabel的大小,比如:

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 0, 0)];     label.backgroundColor = UIColor.redColor;     [self.view addSubview:label];     label.text = @"hello world!";     [label sizeToFit];

sizeToFit & sizeThatFits:

对于UILabel如何计算的,源码如下:

- (CGSize)sizeThatFits:(CGSize)curSize {     CGSize ret = CGSizeZero;      if ([_text length] != 0) {         if (self.numberOfLines != 0) {             CGFloat lineHeight = [_font ascender] - [_font descender];             curSize.height = lineHeight * self.numberOfLines;              if (self.numberOfLines == 1) {                 curSize.width = 0;             }         } else {             curSize.height = 0;         }          //  Calculate the size of the text set in our label         ret = [_text sizeWithFont:_font constrainedToSize:CGSizeMake(curSize.width, curSize.height) lineBreakMode:self.lineBreakMode];     }      return ret; }

以前总觉的sizeToFit和sizeThatFits神乎其神的,看了源码发现原来很简单winObjc源码

源码

直接上源码:

//UIView.mm - (void)sizeToFit {     CGRect curSize;      //  Get current size     curSize = [self frame];      //  Get preferred size     curSize.size = [self sizeThatFits:curSize.size];     [self setFrame:curSize]; }
//UIView.mm - (CGSize)sizeThatFits:(CGSize)curSize {     CGRect curBounds;     curBounds = [self bounds];     return curBounds.size; }

可以重写自己一个UIView的sizeThatFits:,返回一个size,当UIView调用sizeToFit时,内部其实就是调用sizeThatFits:将自身的size设为返回的size

UILabel应用

UILabel在设置完text字符串后,可以调用sizeToFit根据内部字符串调整UILabel的大小,比如:

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 0, 0)];     label.backgroundColor = UIColor.redColor;     [self.view addSubview:label];     label.text = @"hello world!";     [label sizeToFit];

sizeToFit & sizeThatFits:

对于UILabel如何计算的,源码如下:

- (CGSize)sizeThatFits:(CGSize)curSize {     CGSize ret = CGSizeZero;      if ([_text length] != 0) {         if (self.numberOfLines != 0) {             CGFloat lineHeight = [_font ascender] - [_font descender];             curSize.height = lineHeight * self.numberOfLines;              if (self.numberOfLines == 1) {                 curSize.width = 0;             }         } else {             curSize.height = 0;         }          //  Calculate the size of the text set in our label         ret = [_text sizeWithFont:_font constrainedToSize:CGSizeMake(curSize.width, curSize.height) lineBreakMode:self.lineBreakMode];     }      return ret; }

部分转自互联网,侵权删除联系

赞(0) 打赏
部分文章转自网络,侵权联系删除b2bchain区块链学习技术社区 » sizeToFit & sizeThatFits:求职学习资料
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

b2b链

联系我们联系我们