NSAttributedString慧起,NSMutableAttributedString是帶屬性的特殊字符串惩淳,NSMutableParagraphStyle是帶屬性的文本段落屬性疹吃,用于控制段落有關屬性(行間距蹦疑,文本縮進等等)。
1萨驶、NSAttributedString
不可變屬性字符串歉摧,創(chuàng)建出來之后不能修改其屬性(屬性都是只讀的),但是可以在創(chuàng)建的時候直接附加屬性設置(屬性是針對所有文本),其屬性介紹以及用法叁温,直接在代碼上標注如下:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,100, CGRectGetWidth(self.view.frame),300)];? ? label.numberOfLines =0;? ? [self.view addSubview:label];? ? label.backgroundColor = [UIColor lightGrayColor];? ? NSString *string =@"今日頭條是一款基于數(shù)據(jù)挖掘的推薦引擎產品再悼,它為用戶推薦有價值的、個性化的信息膝但,提供連接人與信息的新型服務冲九,是國內移動互聯(lián)網領域成長最快的產品服務之一。";? ? NSMutableDictionary *dict = [NSMutableDictionary dictionary];//? ? dict[NSForegroundColorAttributeName] = [UIColor redColor];? ? ? ? ? ? // UIColor锰镀,文本前景顏色娘侍,默認是blackColor//? ? dict[NSBackgroundColorAttributeName] = [UIColor yellowColor];? ? ? ? ? ? // UIColor,文本背景顏色(不是控件View的背景顏色)泳炉,默認nil//? ? dict[NSFontAttributeName] = [UIFont systemFontOfSize:20];? ? ? ? ? ? ? ? // UIFont憾筏,文本字體大小,默認 Helvetica(Neue) 12//? ? @property(null_resettable, copy, NS_NONATOMIC_IOSONLY) NSArray *tabStops NS_AVAILABLE(10_0, 7_0);/*? 文本段落樣式//? ? NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];//? ? paragraphStyle.lineSpacing = 10;? ? ? ? ? ? ? ? ? // 段落行距//? ? paragraphStyle.headIndent = 5;? ? ? ? ? ? ? ? ? ? // 非首行文本縮進//? ? paragraphStyle.tailIndent = -20;? ? ? ? ? ? ? ? ? // 文本縮進(右端)//? ? paragraphStyle.firstLineHeadIndent = 20;? ? ? ? ? // 首行文本縮進//? ? paragraphStyle.alignment = NSTextAlignmentRight;? // 文本對齊方式//? ? paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping; // 折行方式//? ? paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight; // 文本寫入方式//? ? paragraphStyle.lineHeightMultiple = 3.0;? ? ? ? ? // 文本行間距是默認行間距的多少倍//? ? paragraphStyle.maximumLineHeight = 50;? ? ? ? ? ? // 文本最大行距//? ? paragraphStyle.minimumLineHeight = 50;? ? ? ? ? ? // 文本最小行距//? ? paragraphStyle.allowsDefaultTighteningForTruncation = YES; // 目前還不知道有什么作用//? ? paragraphStyle.hyphenationFactor = 1.0; // 設置每行的最后單詞是否截斷花鹅,在0.0-1.0之間氧腰,默認為0.0,越接近1.0單詞被截斷的可能性越大刨肃,//? ? paragraphStyle.paragraphSpacing = 10;? ? ? ? ? ? // 段落后面的間距//? ? paragraphStyle.paragraphSpacingBefore = 20;? ? ? //設置段與段之間的距離*///? ? dict[NSParagraphStyleAttributeName] = paragraphStyle;? ? ? ? ? ? ? ? ? ? // NSParagraphStyle古拴,文本段落樣式//? ? dict[NSStrikethroughStyleAttributeName] = @(NSUnderlinePatternSolid | NSUnderlineStyleSingle);? ? ? // NSNumber,加刪除線真友,默認不加刪除線黄痪,其它的話是加不同風格的刪除線//? ? dict[NSStrikethroughColorAttributeName] = [UIColor yellowColor];? ? ? ? ? // UIColor,刪除線顏色,默認等于文本前景顏色,前提是需要加刪除線盔然,和NSStrikethroughStyleAttributeName有關//? ? dict[NSUnderlineStyleAttributeName] = @(NSUnderlineStyleDouble);? ? ? ? ? // NSNumber桅打,加下劃線,默認NSUnderlineStyleNone不加下劃線愈案,其它的話是加不同的下劃線//? ? dict[NSUnderlineColorAttributeName] = [UIColor yellowColor];? ? ? ? ? ? ? // UIColor,下劃線顏色挺尾,默認等于文本前景顏色,前提是需要加下劃線,和NSUnderlineStyleAttributeName有關//? ? dict[NSStrokeColorAttributeName] = [UIColor yellowColor];? ? ? ? ? ? ? ? // UIColor,默認等于文本前景顏色站绪,需要和NSStrokeWidthAttributeName一起使用//? ? dict[NSStrokeWidthAttributeName] = @5;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // NSNumber,使文本有一種中空的效果(有立體效果)數(shù)字越大遭铺,文本填充的越滿,數(shù)字越小恢准,文本顏色越淡魂挂,不需要和NSStrokeColorAttributeName一起使用/* 文本陰影? ? NSShadow *shadow = [[NSShadow alloc] init];? ? shadow.shadowOffset = CGSizeMake(2,3);// 陰影偏移量shadow.shadowColor = [UIColor yellowColor];// 陰影顏色shadow.shadowBlurRadius =1.0;// 陰影圓角*///? ? dict[NSShadowAttributeName] = shadow;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // NSShadow,默認沒有陰影,//? ? dict[NSKernAttributeName] = @10;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // NSNumber,文本字間距(字與字之間的距離)//? ? dict[NSLinkAttributeName] = [NSURL URLWithString:@"http:baidu.com"];? ? ? // NSURL或者是鏈接字符串,文本鏈接樣式馁筐,自帶下劃線涂召,文本顏色是藍色//? ? ? ? dict[NSLinkAttributeName] = @"http:baidu.com";? ? ? ? ? ? ? ? ? ? ? ? ? ? // NSURL或者是鏈接字符串,文本鏈接樣式,自帶下劃線眯漩,文本顏色是藍色//? ? dict[NSExpansionAttributeName] = @(-0.5);? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // NSNumber,本文的擴張倍數(shù),負數(shù)的話相當于縮小//? ? dict[NSObliquenessAttributeName] = @(0.7);? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // NSNumber赦抖,本文的斜體程度以及斜體方向舱卡,默認0不歪斜,負數(shù)相當于右斜队萤,正數(shù)相當于左斜轮锥,歪斜的程度由數(shù)字的大小決定//? ? dict[NSBaselineOffsetAttributeName] = @(15.0);? ? ? ? ? ? ? ? ? ? ? ? ? ? // NSNumber,行文本基線的偏移量,//? ? dict[NSWritingDirectionAttributeName] = @[@(NSWritingDirectionOverride),@(NSWritingDirectionRightToLeft)]; // 貌似是文本寫入方向//? ? dict[NSVerticalGlyphFormAttributeName] = @(1);? ? ? ? ? ? ? ? ? ? ? ? ? ? // 文本的垂直與水平要尔,目前在iOS,它總是水平舍杜,任何其他值的行為是未定義的//? ? NSTextAttachment *textAtt = [[NSTextAttachment alloc] init];//? ? textAtt.image = [UIImage imageNamed:@"cloud.png"];//? ? dict[NSAttachmentAttributeName] = textAtt; // 在文本中插入表情/* 顯示圖片附件? ? NSTextAttachment *textA = [[NSTextAttachment alloc] init];? ? textA.image = [UIImage imageNamed:@"lock"];? ? NSAttributedString *att = [NSAttributedString attributedStringWithAttachment:textA];? ? label.attributedText = att;*/? ? label.attributedText = [[NSAttributedString alloc] initWithString:string attributes:dict];
2、NSMutableAttributedString
可變屬性字符串赵辕,創(chuàng)建出來之后可修改其屬性既绩,也可以給文本在某一個范圍內添加單個屬性或者字典屬性(一次性添加很多屬性),比如一個屬性字符串不同范圍的顏色不一樣还惠,其屬性介紹以及用法饲握,直接在代碼上標注如下:
UILabel *label= [[UILabel alloc] initWithFrame:CGRectMake(0,100, CGRectGetWidth(self.view.frame),300)];label.numberOfLines =0;? ? [self.view addSubview:label];label.backgroundColor = [UIColor lightGrayColor];? ? NSString *string= @"今日頭條是一款基于數(shù)據(jù)挖掘的推薦引擎產品,它為用戶推薦有價值的蚕键、個性化的信息救欧,提供連接人與信息的新型服務,是國內移動互聯(lián)網領域成長最快的產品服務之一锣光。";? ? NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:string];? ? NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];? ? [paragraphStyle setLineSpacing:10];? ? [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStylerange:NSMakeRange(0,string.length)];//? ? [attributedString addAttributes:<#(nonnull NSDictionary *)#>range:<#(NSRange)#>]; // 添加字典屬性? ? [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor]range:NSMakeRange(0,10)];label.attributedText = attributedString;
3笆怠、NSMutableParagraphStyle
用于設定文本段落有關設置,比如行間距誊爹,文本縮進蹬刷,段間距等等,其用法如下:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,100, CGRectGetWidth(self.view.frame),300)];? ? label.numberOfLines =0;? ? [self.view addSubview:label];? ? label.backgroundColor = [UIColor lightGrayColor];? ? NSString *string =@"今日頭條是一款基于數(shù)據(jù)挖掘的推薦引擎產品替废,它為用戶推薦有價值的箍铭、個性化的信息,提供連接人與信息的新型服務椎镣,是國內移動互聯(lián)網領域成長最快的產品服務之一诈火。";? ? /*? 文本段落樣式//? ? NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];//? ? paragraphStyle.lineSpacing = 10;? ? ? ? ? ? ? ? ? // 段落行距//? ? paragraphStyle.headIndent = 5;? ? ? ? ? ? ? ? ? ? // 非首行文本縮進//? ? paragraphStyle.tailIndent = -20;? ? ? ? ? ? ? ? ? // 文本縮進(右端)//? ? paragraphStyle.firstLineHeadIndent = 20;? ? ? ? ? // 首行文本縮進//? ? paragraphStyle.alignment = NSTextAlignmentRight;? // 文本對齊方式//? ? paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping; // 折行方式//? ? paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight; // 文本寫入方式//? ? paragraphStyle.lineHeightMultiple = 3.0;? ? ? ? ? // 文本行間距是默認行間距的多少倍//? ? paragraphStyle.maximumLineHeight = 50;? ? ? ? ? ? // 文本最大行距//? ? paragraphStyle.minimumLineHeight = 50;? ? ? ? ? ? // 文本最小行距//? ? paragraphStyle.allowsDefaultTighteningForTruncation = YES; // 目前還不知道有什么作用//? ? paragraphStyle.hyphenationFactor = 1.0; // 設置每行的最后單詞是否截斷,在0.0-1.0之間状答,默認為0.0冷守,越接近1.0單詞被截斷的可能性越大,//? ? paragraphStyle.paragraphSpacing = 10;? ? ? ? ? ? // 段落后面的間距//? ? paragraphStyle.paragraphSpacingBefore = 20;? ? ? //設置段與段之間的距離*///? ? dict[NSParagraphStyleAttributeName] = paragraphStyle;? ? ? ? ? ? ? ? ? ? // NSParagraphStyle惊科,文本段落樣式label.attributedText = [[NSAttributedString alloc] initWithString:string attributes:dict];
屬性字符串基本上就那么多內容拍摇,只不過需要在應用中靈活運用而已。