文字处理之Attribute全解

Attribute由dic和range组成

NSMutableAttributedString *AttributedStr= [[NSMutableAttributedString alloc] init];

NSDictionary *DicAttribute1 = @{NSFontAttributeName:[UIFont systemFontOfSize:15.0f],NSForegroundColorAttributeName:[UIColor whiteColor]};

NSDictionary *DicAttribute2 = @{NSFontAttributeName:[UIFont systemFontOfSize:20.0f],NSForegroundColorAttributeName:[UIColor blackColor]};

[AttributedStr addAttributes:DicAttribute1range:NSMakeRange(0, 3)];

[AttributedStr addAttributes:DicAttribute2range:NSMakeRange(3, AttributedStr.length - 3)];

self.testLabel.attributedText = AttributedStr;

各属性字典设置

字体

NSDictionary *textFontAttribute = @{NSFontAttributeName:[UIFont systemFontOfSize:15.0f]};

颜色

NSDictionary *textColorAttribute = @{NSForegroundColorAttributeName:[UIColor blackColor]]};

行间距

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

[paragraphStyle setLineSpacing:2];

NSDictionary *widthDicAttribute = @{NSParagraphStyleAttributeName:paragraphStyle};

列间距

NSDictionary *segDicAttribute = @{NSKernAttributeName:[NSNumber numberWithInteger:0.7]};

下划线

NSDictionary *underLineDictAttribute = @{NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle],NSUnderlineColorAttributeName:[UIColor whiteColor]};

用于文字适配

NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:14],NSParagraphStyleAttributeName:paragraphStyle,NSKernAttributeName:[NSNumber numberWithInteger:0.7]};

CGRect rect = [str boundingRectWithSize:CGSizeMake(text_w, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];

你可能感兴趣的:(文字处理之Attribute全解)