SDAutoLayout icon indicating copy to clipboard operation
SDAutoLayout copied to clipboard

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

Open dev-zuo opened this issue 6 years ago • 1 comments

使用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

dev-zuo avatar Mar 06 '19 13:03 dev-zuo

加个1像素的宽度😝

liujunliuhong avatar Mar 11 '19 00:03 liujunliuhong