1. 程式人生 >實用技巧 >其它 >3.iOS 自定義 UILable Rect時,有時會上邊或右邊會顯示一條細線的問題。

3.iOS 自定義 UILable Rect時,有時會上邊或右邊會顯示一條細線的問題。


技術標籤:iOS 經驗總結 -ios自定義UILabel細線自定義UILabel rect

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

在自定義UILlabel 邊距時,label上邊或左邊會顯示一條細線。

這是因為,UILable 的rect 寬高沒有取整。

3.iOS 自定義 UILable Rect時,有時會上邊或右邊會顯示一條細線的問題。

        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);

3.iOS 自定義 UILable Rect時,有時會上邊或右邊會顯示一條細線的問題。

* 附 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;