OC UILabel总结

2021-04-26  本文已影响0人  Look2021

改变label部分文字的font

NSString *actualPrice = [NSString stringWithFormat:@"¥%ld", model.actual_price.integerValue]];
NSMutableAttributedString *actualAttributedString = [[NSMutableAttributedString alloc] initWithString:actualPrice];
[actualAttributedString addAttributes:@{NSFontAttributeName: kAdaptedFontSize(28)} range:NSMakeRange(1, actualAttributedString.length - 1)];
cell.actualPriceLabel.attributedText = actualAttributedString;

中划线

NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"¥%ld", model.show_price.integerValue] attributes:attribtDic];
动态改变label的宽度,改变label的部分字符
_groupName.text = [NSString stringWithFormat:@"%@ (%li)",_ref.groupName,_ref.count];
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:_groupName.text];
    // 需要改变的第一个文字的位置
    NSUInteger firstLoc = [[noteStr string] rangeOfString:@"("].location;
    // 需要改变的最后一个文字的位置
    NSUInteger secondLoc = [[noteStr string] rangeOfString:@")"].location + 1;
    // 需要改变的区间
    NSRange range = NSMakeRange(firstLoc, secondLoc - firstLoc);
    // 改变颜色
    [noteStr addAttribute:NSForegroundColorAttributeName value:S04_App_SubCharColor range:range];
    // 改变字体大小及类型
    [noteStr addAttribute:NSFontAttributeName value:P26_TagAssistFont range:range];
    // 为label添加Attributed
    [_groupName setAttributedText:noteStr];
上一篇 下一篇

猜你喜欢

热点阅读