iOS-label出现未知边框线的bug

在前段时间碰到了一个问题  label上出现了一个位置的右边框  仔细查看代码发现没有指定边框 而且奇怪的是只显示右边框  其他边框没有显示  

需求效果图:

实际效果图:

结构图:

通过查看结构图  可以发现英雄联盟 label的右边框明显黑一点  

改正前代码:

    UILabel *tagLabel = [[UILabel alloc]init];
    tagLabel.backgroundColor = [UIColor whiteColor];
    tagLabel.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1.0];
    tagLabel.font = ([UIScreen mainScreen].bounds.size.width>=375?[UIFont systemFontOfSize:12]:[UIFont systemFontOfSize:10]);
    self.tagLabel = tagLabel;
    tagLabel.text = @"英雄联盟";
    [self addSubview:tagLabel];

改正后代码:
    UILabel *tagLabel = [[UILabel alloc]init];
//    tagLabel.backgroundColor = [UIColor whiteColor];
    //将白色背景色改为透明色后则无右边框
    tagLabel.backgroundColor = [UIColor clearColor];
    tagLabel.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1.0];
    tagLabel.font = ([UIScreen mainScreen].bounds.size.width>=375?[UIFont systemFontOfSize:12]:[UIFont systemFontOfSize:10]);
    self.tagLabel = tagLabel;
    tagLabel.text = @"英雄联盟";
    [self addSubview:tagLabel];
那个未知的右边框应该室友label的背景颜色导致的 通过更改label的背景颜色为透明色 可以隐藏此边框


另外,出现此问题的原因也有可能是因为label尺寸精度问题:
在计算每个label 的frame时因为是根据text的文字多少与字体大小有关,造成frame的size 中width 跟height 有小数部分,只要将其width与height改为int类型即可解决改问题。
  CGRect tempFrame = label.frame;
  tempFrame.size.width = (int)label.frame.size.width;
  tempFrame.size.height = (int)label.frame.size.height;
  label.frame = tempFrame;


 
原文地址:https://www.cnblogs.com/gaoxiaoniu/p/5332541.html