前言:
? ? ? ? 在iOS中,顯示一段字符串通常會使用UILabel先誉,但是它對文字的展現(xiàn)方式比較單一湿刽,有的時候需要展示一些特殊的形式,此時NSMutableAttributedString可以很好地滿足使用要求褐耳。它一共提供了21方法可以選擇使用诈闺,下面我將一一介紹。
一:屬性介紹
1:NSParagraphStyleAttributeName 段落排版
? ? ? ?設置文本段落排版格式铃芦,取值為 NSParagraphStyle 對象雅镊,該屬性在一段文本上應用多個屬性。如果不指定該屬性刃滓,則默認為 NSParagraphStyle 的defaultParagraphStyle 方法返回的默認段落屬性仁烹。
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.firstLineHeadIndent = 20;?
style.lineSpacing = 20;
style.alignment = NSTextAlignmentCenter;
2:NSFontAttributeName 字體屬性
? ? ? 該屬性所對應的值是一個 UIFont 對象。該屬性用于改變一段文本的字體咧虎。如果不指定該屬性卓缰,則默認為12-point Helvetica(Neue)。
3:NSForegroundColorAttributeNam? 字體顏色
? ? ? ?該屬性所對應的值是一個 UIColor 對象老客。該屬性用于指定一段文本的字體顏色僚饭。如果不指定該屬性,則默認為黑色胧砰。
? ? ? ?NSForegroundColorAttributeName 設置的顏色與 UILabel 的 textColor 屬性設置的顏色在地位上是相等的,與 NSBackgroundColorAttributeName 地位上也相等苇瓣,誰最后賦值尉间,最終顯示的就是誰的顏色,但是textColor屬性可以與 NSBackgroundColorAttributeName 屬性可疊加。
4:NSBackgroundColorAttributeName? 字體所在區(qū)域背景顏色
? ? ? 該屬性所對應的值是一個 UIColor 對象哲嘲。該屬性用于指定一段文本的背景顏色贪薪。如果不指定該屬性, 默認值為nil, 透明色
5:NSLigatureAttributeName? 連體屬性
? ? ? ? 該屬性所對應的值是一個 NSNumber 對象(整數(shù))眠副。連體字符是指某些連在一起的字符画切,它們采用單個的圖元符號。0 表示沒有連體字符囱怕。1 表示使用默認的連體字符霍弹。2表示使用所有連體符號。默認值為 1(注意娃弓,iOS 不支持值為 2)典格。一般中文用不到,在英文中可能出現(xiàn)相鄰字母連筆的情況
6:NSKernAttributeName? 字符間距
取值為 NSNumber 對象(整數(shù))台丛,正值間距加寬耍缴,負值間距變窄,值為浮點數(shù)挽霉,字距屬性防嗡,默認值為0
7:NSStrikethroughStyleAttributeName? 刪除線
設置刪除線,取值為 NSNumber 對象(整數(shù))侠坎,枚舉默認值是NSUnderlineStyleNone蚁趁。
NSUnderlineStyleNone 不設置刪除線
NSUnderlineStyleSingle 設置刪除線為細單實線
NSUnderlineStyleThick 設置刪除線為粗單實線
NSUnderlineStyleDouble 設置刪除線為細雙實線
8:NSStrikethroughColorAttributeName? 刪除線顏色
取值為 UIColor 對象,默認值為黑色硅蹦,
9:NSUnderlineStyleAttributeName 下劃線
取值為 NSNumber 對象(整數(shù))荣德,枚舉常量 NSUnderlineStyle中的值,與刪除線類似童芹,該值指定是否在文字上加上下劃線涮瞻,下劃線除了線條位置和刪除線不同外,其他的都可以完全參照刪除線設置
10:NSUnderlineColorAttributeName 下劃線顏色
設置下劃線顏色假褪,取值為 UIColor 對象署咽,默認值為黑色
11:NSStrokeWidthAttributeName? 筆畫寬度
? ? ? 值為浮點數(shù)NSNumber,該值改變筆畫寬度(相對于字體 size 的百分比)設置筆畫的粗細生音,負值填充效果宁否,正值中空效果,正數(shù)只改變描邊寬度缀遍,負數(shù)同時改變文字的描邊和填充寬度慕匠,默認為 0,即不改變域醇。
12:NSStrokeColorAttributeName 填充部分顏色
填充部分顏色台谊,不是字體顏色蓉媳,取值為 UIColor 對象,
13:NSShadowAttributeName? 陰影屬性
取值為 NSShadow 對象锅铅,默認為 nil酪呻。
NSShadow *shadow=[[NSShadow alloc] init];
shadow.shadowBlurRadius=5;//模糊度
shadow.shadowColor=[UIColor yellowColor];
shadow.shadowOffset=CGSizeMake(1, 3);
14:NSObliquenessAttributeName? 字形傾斜度
取值為 NSNumber (float),正值右傾,負值左傾盐须,默認值為0玩荠,表示沒有傾斜,
15:NSExpansionAttributeName? 橫向拉伸屬性,
取值為 NSNumber (float),正值橫向拉伸文本,負值橫向壓縮文本
16:NSVerticalGlyphFormAttributeName 文字排版方向
取值為 NSNumber 對象(整數(shù))贼邓,0 表示橫排文本阶冈,1 表示豎排文本,在iOS中, 總是以橫向排版,0 以外的值都未定義立帖。
17:NSTextEffectAttributeName 文本特殊效果
這個屬性的值是一個NSString對象眼溶。使用此屬性指定的文字效果,如NSTextEffectLetterpressStyle晓勇。此屬性的默認值為nil堂飞,表示沒有文本效應。
18:NSBaselineOffsetAttributeName? 基線偏移值
取值為 NSNumber (float),表示的字符從基線偏移的NSNumber對象绑咱,默認值是0绰筛,正值上偏,負值下偏描融。
19:NSWritingDirectionAttributeName? 文字書寫方向
? ? ? ? 取值為包含NSNumber對象的數(shù)組. 從左向右書寫或者從右向左書寫铝噩。
The values of the NSNumber objects should be 0, 1, 2, or 3, for LRE, RLE, LRO, or RLO respectively, and combinations of NSWritingDirectionLeftToRight and NSWritingDirectionRightToLeft with NSTextWritingDirectionEmbedding or NSTextWritingDirectionOverride, as shown in Values of NSWritingDirectionAttributeName and equivalent markup.
[attributedString addAttribute:NSWritingDirectionAttributeName value:@[@3]? range:NSMakeRange(40, 4)];
20:NSLinkAttributeName ?鏈接屬性
點擊后調(diào)用瀏覽器打開指定URL地址,此屬性的值是NSURL對象(首選)或一個NSString對象窿克,此屬性的默認值為nil骏庸,表示沒有鏈接,UILabel無法使用該屬性, 可以使用UITextView 控件年叮。
21:NSAttachmentAttributeName? 文本附件
? ? ? 取值為NSTextAttachment對象,常用于文字圖片混排具被,此屬性的默認值為nil,表示無附件只损。
使用:
//創(chuàng)建NSTextAttachment的對象一姿,用來裝載圖片
NSTextAttachment *attch = [[NSTextAttachment alloc] init];
//將NSTextAttachment對象的image屬性設置為想要使用的圖片
attch.image = [UIImage imageNamed:@"lock_wallpaper.jpeg"];
//設置NSTextAttachment對象bounds大小,也就是要顯示的圖片的大小
attch.bounds = CGRectMake(0, 0, 50, 50);
// 創(chuàng)建帶有圖片的富文本
NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
//插入文本指定位置
[attributedString insertAttributedString:string atIndex:7];
//插入文本末尾
[attributedString appendAttributedString:string];
二:效果展示
? ? ? 了解完上面的屬性介紹跃惫,看一下實際的效果
三:實現(xiàn)代碼:
- (void)viewDidLoad {
[super viewDidLoad];
self.lable.numberOfLines = 0; ? ? ? //自動換行
//創(chuàng)建一個NSMutableAttributedString富文本對象
NSMutableAttributedString *attributedString=[[NSMutableAttributedString alloc] initWithString:@"愿你有好運氣叮叹,如果沒有,愿你在不幸中學會慈悲爆存。愿你被很多人愛蛉顽,如果沒有,愿你在寂寞中學會寬容先较。愿你一生一世每天都可以睡到自然醒蜂林。"];
[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:40.0] range:NSMakeRange(0, 2)]; ? ?//設置字體屬性 大小
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(2, 2)]; ? ? //設置字體顏色
[attributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(4, 2)]; ? //設置字體所在區(qū)域背景顏色
[attributedString addAttribute: NSLigatureAttributeName value:@(1) range:NSMakeRange(7, 2)];? ? //設置連體屬性
[attributedString addAttribute: NSKernAttributeName? value:@(10) range:NSMakeRange(7, 2)];? //設定字符間距
[attributedString addAttribute:NSStrikethroughStyleAttributeName? value:@(NSUnderlineStyleThick) range:NSMakeRange(9, 2)];? //設置刪除線
[attributedString addAttribute: NSStrikethroughColorAttributeName? value:[UIColor redColor] range:NSMakeRange(9, 2)];? //設置刪除線顏色
[attributedString addAttribute: NSUnderlineStyleAttributeName? value:@(NSUnderlineStyleThick) range:NSMakeRange(12, 2)];? //設置下劃線
[attributedString addAttribute: NSUnderlineColorAttributeName? value:[UIColor redColor] range:NSMakeRange(12, 2)];? //設置下劃線顏色
[attributedString addAttribute:NSStrokeWidthAttributeName value:@(-5) range:NSMakeRange(14, 2)];? //設置筆畫寬度? //填充字
[attributedString addAttribute:NSStrokeWidthAttributeName value:@(2) range:NSMakeRange(16, 2)];//設置筆畫寬度? //空心字
[attributedString addAttribute:NSStrokeColorAttributeName value:[UIColor greenColor] range:NSMakeRange(14, 4)];? //填充部分顏色
//設置陰影屬性
NSShadow *shadow=[[NSShadow alloc] init];
shadow.shadowBlurRadius=5;//模糊度
shadow.shadowColor=[UIColor yellowColor];
shadow.shadowOffset=CGSizeMake(1, 3);
[attributedString addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(18, 4)];
[attributedString addAttribute:NSObliquenessAttributeName value:@(1) range:NSMakeRange(23, 2)];? ? //設置字形傾斜度遥诉,
[attributedString addAttribute:NSExpansionAttributeName value:@(0.5) range:NSMakeRange(25, 4)];? ? //設置文本橫向拉伸屬性,
[attributedString addAttribute:NSVerticalGlyphFormAttributeName value:@(1) range:NSMakeRange(18, 4)];? //設置文字排版方向拇泣,
[attributedString addAttribute:NSTextEffectAttributeName value:NSTextEffectLetterpressStyle range:NSMakeRange(31, 4)];? //設置文本特殊效果,目前只有圖版印刷效果可用:
[attributedString addAttribute:NSBaselineOffsetAttributeName value:@(-6) range:NSMakeRange(36, 4)]; //設置基線偏移值噪叙,
[attributedString addAttribute:NSWritingDirectionAttributeName value:@[@3]? range:NSMakeRange(40, 4)];? //設置文字書寫方向,
//設置鏈接屬性霉翔, UILabel無法使用該屬性, 可以使用UITextView 控件.
NSURL *url=[NSURL URLWithString:@"www.baidu.com"];
[attributedString addAttribute: NSLinkAttributeName? value:url? range:NSMakeRange(44, 2)];
//NSAttachmentAttributeName 設置文本附件,常用于文字圖片混排
NSTextAttachment *attch = [[NSTextAttachment alloc] init];? //創(chuàng)建NSTextAttachment的對象睁蕾,用來裝載圖片
attch.image = [UIImage imageNamed:@"lock_wallpaper.jpeg"];? //將NSTextAttachment對象的image屬性設置為想要使用的圖片
attch.bounds = CGRectMake(0, 0, 50, 50);? //設置NSTextAttachment對象bounds大小,也就是要顯示的圖片的大小
NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];? // 創(chuàng)建帶有圖片的富文本
[attributedString insertAttributedString:string atIndex:7]; ? //圖片插入指定位置
[attributedString appendAttributedString:string];? ? //插入末尾
//設置文本段落排版格式债朵,
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.firstLineHeadIndent = 20;
style.lineSpacing = 20;
style.alignment = NSTextAlignmentCenter;
[attributedString addAttribute:NSParagraphStyleAttributeName value:style? range:NSMakeRange(0, attributedString.length)];
self.lable.attributedText=attributedString;//設置控件的富文本對象
}