天天看點

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

最後這個沒有什麼特别效果就不貼圖了