iOS 自定义 UILable Rect时,有时会上边或右边会显示一条细线的问题

151 阅读1分钟

-(void)drawTextInRect:(CGRect)rect { [super drawTextInRect:UIEdgeInsetsInsetRect(rect, _edgeInsets)]; }

在自定义UILlabel 边距时,label上边或左边会显示一条细线。

这是因为,UILable 的 宽高没有取整。

20210204150717144.png

   `labelWidth = GetWidth6(15)*2 + charWidth + 2;
    labelHeight = GetWidth6(5)*2 + fontHeight + 2;
    [_labelBg addSubview:label];

    label.sd_layout
    .leftEqualToView(_labelBg)
    .topSpaceToView(_labelBg, GetWidth6(0))
    .widthIs(labelWidth)
    .heightIs(labelHeight);`

改为

labelWidth = ceil(GetWidth6(15)*2 + charWidth + 2); labelHeight = ceil(GetWidth6(5)*2 + fontHeight + 2); [_labelBg addSubview:label]; label.sd_layout .leftEqualToView(_labelBg) .topSpaceToView(_labelBg, GetWidth6(0)) .widthIs(labelWidth) .heightIs(labelHeight);

20210204151027254.png

  • 附 iOS取整方式

float floatVale1 = 2.8;

float floatVale2 = 2.2;

1.直接转化。

int value1 = (int) floatVale1;

int value2 = (int) floatVale2;

结果:

value1 = 2;

value2 = 2;

2.向下取整

int value1 = floor(floatValue1);

int value2 = floor(floatValue2);

结果:

value1 = 2;

value2 = 2;

3.向上取整

int value1 = ceil(floatValue1);

int value2 = ceil(floatValue2);

结果:

value1 = 3;

value2 = 3;

4.四舍五入

int value1 = round(floatValue1);

int value2 = round(floatValue2);

结果:

value1 = 2;

value2 = 3;