文本属性Attributes的使用

本文详细介绍了NSAttributedString的各种属性,包括字体大小、段落格式、字体颜色等,以及如何使用这些属性来定制文本显示效果。通过具体代码示例,展示了如何设置字体、段落样式、字间距、文本颜色等,为开发者提供了丰富的文本格式化技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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];



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值