文本属性Attributes的使用

NSFontAttributeName                    //设置字体大小
NSParagraphStyleAttributeName          //设置段落格式
NSForegroundColorAttributeName         //设置字体的颜色
NSBackgroundColorAttributeName         //设置背景的颜色
NSLigatureAttributeName                //设置连体字符
NSKernAttributeName                    //设置文字之间的距离
NSStrikethroughStyleAttributeName      //设置删除线的样式
NSUnderlineStyleAttributeName          //设置下划线的格式
NSStrikethroughColorAttributeName      //设置删除线的颜色
NSStrokeColorAttributeName             //设置中空效果的填充颜色
NSStrokeWidthAttributeName             //设置中空效果的宽度
NSShadowAttributeName                  //设置阴影效果
NSTextEffectAttributeName              //设置文本的特殊效果
NSAttachmentAttributeName              //设置文本附件
NSLinkAttributeName                    //设置超链接
NSBaselineOffsetAttributeName          //设置基线偏移值
NSUnderlineColorAttributeName          //设置下划线的颜色
NSObliquenessAttributeName             //设置字体倾斜
NSExpansionAttributeName               //设置文本扁平化(横向拉伸)
NSWritingDirectionAttributeName        //设置文字的书写方向
NSVerticalGlyphFormAttributeName       //设置文字的排版方向

 

书写方式

    //有范围
    NSString * helloStr = @"hello word";
    NSMutableAttributedString * mString = [[NSMutableAttributedString alloc] initWithString: helloStr];
    
    //方式1
    NSDictionary * dic = @{NSFontAttributeName :[UIFont fontWithName:@"Helvetica-Bold" size:20],NSForegroundColorAttributeName:[UIColor redColor],};
    [mString addAttributes:dic range:NSMakeRange(0, 3)];
  
    //方式2
    [mString addAttribute: NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 4)];
    [mString addAttribute: NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:20] range:NSMakeRange(0, 7)];

    label.attributedText = mString;

    //无范围
    label.attributedText = [[NSAttributedString alloc]initWithString: helloStr attributes:dic];

字体段落

    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
    style.alignment = NSTextAlignmentLeft;
    style.lineSpacing = 5;
    //指定段落开始的缩进像素
    style.firstLineHeadIndent = 20;
    //调整全部文字的缩进像素
    style.headIndent = 2;
    [mString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, str.length)];
    //字间距
    [mString addAttribute:NSKernAttributeName value:@1.5 range:NSMakeRange(0, str.length)];

    //NSDictionary * dic = @{NSFontAttributeName :[UIFont fontWithName:@"Helvetica-Bold" size:20],NSForegroundColorAttributeName:[UIColor redColor],NSParagraphStyleAttributeName:style,NSKernAttributeName:@1.5f,};


 

字体相关

    //调整字间距:
    [mString addAttribute:NSKernAttributeName value:@10 range:range];
    //设置字体:
    [mString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:22] range:range];
    //设置文本颜色:
    [mString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
    //设置文本背景色:
    [mString addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:range];
    //描边颜色 NSStrokeWidthAttributeName 正值 字体空心 负值 字体 非空心 会使NSForegroundColorAttributeName 无效果
    [mString addAttribute:NSStrokeColorAttributeName value:[UIColor greenColor] range:range];
    [mString addAttribute:NSStrokeWidthAttributeName value:@3 range:range];
    //删除线
    [mString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleSingle) range:range];
    //下划线
    [mString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:range];
    //设置阴影
    NSShadow *shadow = [[NSShadow alloc]init];
    shadow.shadowBlurRadius = 5;//设置模糊度
    shadow.shadowColor = [UIColor blueColor];//设置阴影颜色
    shadow.shadowOffset = CGSizeMake(1, 3);//设置阴影的偏移量
    [mString addAttribute:NSShadowAttributeName value:shadow range:range];
    //文本排版方式
    [mString addAttribute:NSVerticalGlyphFormAttributeName value:@(0) range:range];
    //文本斜体
    [mString addAttribute:NSObliquenessAttributeName value:@(1) range:range];
    //扁平化
    [mString addAttribute:NSExpansionAttributeName value:@(1) range:range];



 

你可能感兴趣的:(iOS)