天天看点

iOS中文本属性Attributes的用法

iOS中用到文本属性Attributes的地方还是很多的,这里对文本属性做一些归纳,以免记不住用到的时候到处找资料:

如下是所有的文本属性:

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

下面是详细用法

-1. NSFontAttributeName //设置字体大小

UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];
NSString *myString = @"这是一串测试用的字符串xxx";
NSDictionary *myDic = @{
                        NSFontAttributeName:[UIFont boldSystemFontOfSize:20],
                        };
lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];
[self addSubview:lable];
           

显示效果:

iOS中文本属性Attributes的用法

-2. NSParagraphStyleAttributeName //设置段落格式

UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
lable.numberOfLines = 0;
lable.backgroundColor = [UIColor lightGrayColor];
NSString *myString = @"这是一串测试用的字符串xxx";
//段落样式
NSMutableParagraphStyle *myStyle = [[NSMutableParagraphStyle alloc]init];
//行间距
myStyle.lineSpacing = 10;
//段落间距
myStyle.paragraphSpacing = 20;
//对齐方式
myStyle.alignment = NSTextAlignmentLeft;
//指定段落开始的缩进像素
myStyle.firstLineHeadIndent = 20;
//调整全部文字的缩进像素
myStyle.headIndent = 20;
NSDictionary *myDic = @{
                        NSParagraphStyleAttributeName:myStyle,
                        };
lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];
[self addSubview:lable];
           

显示效果:

iOS中文本属性Attributes的用法

-3.NSForegroundColorAttributeName //设置字体的颜色

UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];
lable.numberOfLines = 0;
lable.backgroundColor = [UIColor lightGrayColor];
NSString *myString = @"这是一串测试用的字符串xxx";
NSDictionary *myDic = @{
                        NSForegroundColorAttributeName:[UIColor redColor],
                        };
lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];
[self addSubview:lable];
           

显示效果:

iOS中文本属性Attributes的用法

-4.NSBackgroundColorAttributeName //设置背景的颜色

UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];
lable.numberOfLines = 0;
lable.backgroundColor = [UIColor lightGrayColor];
NSString *myString = @"这是一串测试用的字符串xxx";
NSDictionary *myDic = @{
                        NSBackgroundColorAttributeName:[UIColor redColor],
                        };
lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];
[self addSubview:lable];
           

显示效果:

iOS中文本属性Attributes的用法

-5.NSLigatureAttributeName //设置连体字符

UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];
lable.numberOfLines = 0;
lable.backgroundColor = [UIColor lightGrayColor];
NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";
//0 表示没有连体字符。1 表示使用默认的连体字符
//这个设置了感觉没有什么变化,如果设置字体为futura后可以看到连体的效果,但这是设置字体后改变的
NSDictionary *myDic = @{
                        NSLigatureAttributeName:@1,
//                            NSFontAttributeName: [UIFont fontWithName: @"futura" size: 18],
                        };
lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];
[self addSubview:lable];
           

显示效果:

iOS中文本属性Attributes的用法

-6.NSKernAttributeName //设置文字之间的距离

UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];
lable.numberOfLines = 0;
lable.backgroundColor = [UIColor lightGrayColor];
NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";
NSDictionary *myDic = @{
                        NSKernAttributeName:@20,
                        };
lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];
[self addSubview:lable];
           

显示效果:

iOS中文本属性Attributes的用法

-7.NSStrikethroughStyleAttributeName //设置删除线的样式

UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];
lable.backgroundColor = [UIColor lightGrayColor];
NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";
//在枚举NSUnderlineStyle中取值
NSDictionary *myDic = @{
                        NSStrikethroughStyleAttributeName:@(NSUnderlineStyleDouble),
                        };
lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];
[self addSubview:lable];
           

显示效果:

iOS中文本属性Attributes的用法

-8.NSUnderlineStyleAttributeName //设置下划线的格式

UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];
lable.backgroundColor = [UIColor lightGrayColor];
NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";
//在枚举NSUnderlineStyle中取值
NSDictionary *myDic = @{
                        NSUnderlineStyleAttributeName:@(NSUnderlineStyleDouble),
                        };
lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];
[self addSubview:lable];
           

显示效果:

iOS中文本属性Attributes的用法

-9.NSStrikethroughColorAttributeName //设置删除线的颜色

UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];
lable.backgroundColor = [UIColor lightGrayColor];
NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";
//在枚举NSUnderlineStyle中取值
NSDictionary *myDic = @{
                        NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle),
                        NSStrikethroughColorAttributeName:[UIColor redColor],
                        };
lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];
[self addSubview:lable];
           

显示效果:

iOS中文本属性Attributes的用法

-10.NSStrokeWidthAttributeName //设置中空效果以及效果的宽度

UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];
lable.backgroundColor = [UIColor lightGrayColor];
NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";
NSDictionary *myDic = @{
                        NSStrokeWidthAttributeName:@2,
                        };
lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];
[self addSubview:lable];
           

显示效果:

iOS中文本属性Attributes的用法

-11.NSStrokeColorAttributeName //设置中空效果的填充颜色

UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];
lable.backgroundColor = [UIColor lightGrayColor];
NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";
NSDictionary *myDic = @{
                        NSStrokeWidthAttributeName:@2,
                        NSStrokeColorAttributeName:[UIColor redColor],
                        };
lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];
[self addSubview:lable];
           

显示效果:

iOS中文本属性Attributes的用法

-12.NSShadowAttributeName //设置阴影效果

UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];
lable.backgroundColor = [UIColor lightGrayColor];
NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";
NSShadow *shadow = [[NSShadow alloc]init];
shadow.shadowColor = [UIColor redColor];
shadow.shadowBlurRadius = 1.0f;
shadow.shadowOffset = CGSizeMake(1, 1);
NSDictionary *myDic = @{
                        NSShadowAttributeName:shadow,
                        };
lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];
[self addSubview:lable];
           

显示效果:

iOS中文本属性Attributes的用法

-13.NSTextEffectAttributeName //设置文本的特殊效果

UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];
lable.backgroundColor = [UIColor lightGrayColor];
NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";
//只有一种印刷效果
NSDictionary *myDic = @{
                        NSTextEffectAttributeName:NSTextEffectLetterpressStyle,
                        };
lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];
[self addSubview:lable];
           

显示效果:

iOS中文本属性Attributes的用法

-14.NSAttachmentAttributeName //设置文本附件

UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];
lable.backgroundColor = [UIColor lightGrayColor];
NSMutableAttributedString *myString = [[NSMutableAttributedString alloc] initWithString:@"这是一串测试用的字符串fhfkfbfjflfi"];
NSTextAttachment *textAttachment = [[NSTextAttachment alloc]init];
textAttachment.image = [UIImage imageNamed:@"dog"];
textAttachment.bounds = CGRectMake(0, 0, 30, 30);
NSAttributedString * imageStr = [NSAttributedString attributedStringWithAttachment:textAttachment];
[myString appendAttributedString:imageStr];
    lable.attributedText = myString;
[self addSubview:lable];
           

显示效果:

iOS中文本属性Attributes的用法

-15.NSLinkAttributeName //设置超链接

//在 UILabel 和 UITextField 中是无法使用超链接的
UITextView *myTextView = [[UITextView alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];
myTextView.delegate = self;
myTextView.editable = NO;   //不设置是不能点击的

myTextView.backgroundColor = [UIColor lightGrayColor];
NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";
NSDictionary *myDic = @{
                        NSLinkAttributeName:[NSURL URLWithString:@"https://www.baidu.com"],
                        };
myTextView.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];
[self addSubview:myTextView];
           

记得遵循协议

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction {
    NSLog(@"点击了");
    return YES;
}
           

显示效果:

iOS中文本属性Attributes的用法
iOS中文本属性Attributes的用法

-16.NSBaselineOffsetAttributeName //设置基线偏移值

UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];
lable.backgroundColor = [UIColor lightGrayColor];
NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";
//正数上偏,负数下偏
NSDictionary *myDic = @{
                        NSBaselineOffsetAttributeName:@10,
                        };
lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];
[self addSubview:lable];
           

显示效果:

iOS中文本属性Attributes的用法

-17.NSUnderlineColorAttributeName //设置下划线的颜色

UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];
lable.backgroundColor = [UIColor lightGrayColor];
NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";
NSDictionary *myDic = @{
                        NSUnderlineStyleAttributeName:@(NSUnderlineStyleDouble),
                        NSUnderlineColorAttributeName:[UIColor redColor],
                        };
lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];
[self addSubview:lable];
           

显示效果:

iOS中文本属性Attributes的用法

-18.NSObliquenessAttributeName //设置字体倾斜

UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];
lable.backgroundColor = [UIColor lightGrayColor];
NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";
NSDictionary *myDic = @{
                        NSObliquenessAttributeName:@.5f,
                        };
lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];
[self addSubview:lable];
           

显示效果:

iOS中文本属性Attributes的用法

-19.NSExpansionAttributeName //设置文本扁平化(横向拉伸)

UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];
lable.backgroundColor = [UIColor lightGrayColor];
NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";
NSDictionary *myDic = @{
                        NSExpansionAttributeName:@1,
                        };
lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];
[self addSubview:lable];
           

显示效果:

iOS中文本属性Attributes的用法

-20.NSWritingDirectionAttributeName //设置文字的书写方向

UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];
lable.backgroundColor = [UIColor lightGrayColor];
NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";
//NSWritingDirectionAttributeName 设置文字的书写方向,取值为以下组合
/*
 @[@(NSWritingDirectionLeftToRight | NSWritingDirectionEmbedding)]
 @[@(NSWritingDirectionLeftToRight | NSWritingDirectionOverride)]
 @[@(NSWritingDirectionRightToLeft | NSWritingDirectionEmbedding)]
 @[@(NSWritingDirectionRightToLeft | NSWritingDirectionOverride)]
 */
NSDictionary *myDic = @{
                        NSWritingDirectionAttributeName:@[@(NSWritingDirectionRightToLeft | NSWritingDirectionOverride)],
                        };
lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];
[self addSubview:lable];
           

显示效果:

iOS中文本属性Attributes的用法

-21.NSVerticalGlyphFormAttributeName //设置文字的排版方向

UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];
lable.backgroundColor = [UIColor lightGrayColor];
NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";
//0表示横排文本,1表示竖排文本  在iOS中只支持0
NSDictionary *myDic = @{
                        NSVerticalGlyphFormAttributeName:@1,
                        };
lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];
[self addSubview:lable];
           

最后这个没有什么特别效果就不贴图了