gsdios / SDAutoLayout

One line of code to implement automatic layout. 一行代码搞定自动布局!支持Cell和Tableview高度自适应,Label和ScrollView内容自适应,致力于做最简单易用的AutoLayout库。The most easy way for autoLayout. Based on runtime.
MIT License
5.9k stars 1.28k forks source link

引入第三方sdk后setSingleLineAutoResizeWithMaxWidth计算label宽度时异常 #310

Open dev-zuo opened 5 years ago

dev-zuo commented 5 years ago

使用SDAutoLayout对Label自动布局,之前Label显示都是正常的,引入了一个第三方Framework之后,就显示不全了。

引入第三方Framework之前显示效果 image 引入第三方Framework之后显示效果 image 代码如下:

    UILabel *autoWidthlabel = [UILabel new];
    autoWidthlabel.backgroundColor = [[UIColor orangeColor] colorWithAlphaComponent:0.5];
    _autoWidthLabel = autoWidthlabel;
    autoWidthlabel.font = [UIFont systemFontOfSize:14];
    autoWidthlabel.text = @"Hello, 2Locksmiths!";
    [self.view addSubview:autoWidthlabel];
    autoWidthlabel.sd_layout
    .rightSpaceToView(self.view, 10)
    .heightIs(20)
    .bottomSpaceToView(self.view, 50);
    // 用Label显示Hello, 2Locksmiths!的内容
    [autoWidthlabel setSingleLineAutoResizeWithMaxWidth:200];

想着应该是计算宽度时出来问题,于是在UIView+SDAutoLayout.m源码的- (void)layoutAutoWidthWidthView:(UIView )view model:(SDAutoLayoutModel )model{} 函数里打印了一些log

  // qqqrect.size.width: 126.362305, width: 200.000000
  label.width_sd = rect.size.width + 0.1; // 出现差异的位置
  NSLog(@"label.width_sd: %f", label.width_sd); 
  // label.width_sd: 126.000000  // 引入第三方组件后,这里是什么原因造成的呢?
  // label.width_sd: 126.462305   // 引入第三方组件前

引入第三方组件之前计算的值: image 引入第三方组件之后计算的值: image

这是什么原因导致的呢?

引入的第三方组件 image

liujunliuhong commented 5 years ago

加个1像素的宽度😝