创建带样式的字符串AttributedString(富文本)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
+ (id)stringWithDefaultStyle:(NSString *)string kern:(CGFloat)kernValue
{
    NSNumber *kern = [NSNumber numberWithFloat:kernValue];
    NSNumber *underline = [NSNumber numberWithInt:NSUnderlineStyleSingle];
    UIFont   *font = [UIFont boldSystemFontOfSize:17];
    UIColor  *color = kOrange(1.0);
 
    NSDictionary *attrsDic = @{
                               NSKernAttributeName: kern,                   // 字间距
                               NSUnderlineStyleAttributeName: underline,    // 下划线
                               NSForegroundColorAttributeName: color,       // 字体颜色
                               NSFontAttributeName: font
                               };
 
    return [[NSAttributedString alloc] initWithString:string attributes:attrsDic];
}

Leave a Reply