iOS lable添加下划线,斜线

2017-12-25  本文已影响255人  YannChee

如果设置lable一部分有下滑划线、斜线时一部分没有时,需要注意iOS10.3系统以后,必须设置NSBaselineOffsetAttributeName属性,否则文字上下不对齐

        NSString *str = [NSString stringWithFormat:@"%zd~%zd  %zd~%.f",surveyModel.point_s,surveyModel.point_c, surveyModel.point_s,surveyModel.point_c * 1.1];

        NSRange range = [str rangeOfString:@" "];
        NSMutableAttributedString *attriStr = [[NSMutableAttributedString alloc] initWithString:str];

        //
        [attriStr addAttribute:NSStrikethroughStyleAttributeName
                         value:[NSNumber numberWithInteger:NSUnderlineStyleSingle]
                         range:NSMakeRange(0,range.location)];
        // iOS10.3系统以后 ,设置该属性才显示
        [attriStr addAttribute:NSBaselineOffsetAttributeName
                         value:@(NSUnderlineStyleSingle)
                         range:NSMakeRange(0,attriStr.length)];
        //
        [attriStr addAttribute:NSForegroundColorAttributeName
                        value:[UIColor darkGrayColor]
                        range:NSMakeRange(0,range.location)];
        
        [self.integralLabel setAttributedText:attriStr];

如果整个lable都有下换线或斜线时,不需要设置上述属性

// 整个都有下划线
     NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:@"修改收货地址" attributes:@{NSUnderlineStyleAttributeName : [NSNumber numberWithInteger:NSUnderlineStyleSingle]}];
        self.modifyAddressL.attributedText = attStr;
上一篇 下一篇

猜你喜欢

热点阅读