iOS NSAttributedString和NSMutableAttributedString、NSMutableParagraphStyle的詳細(xì)用法

NSAttributedString,NSMutableAttributedString是帶屬性的特殊字符串,NSMutableParagraphStyle是帶屬性的文本段落屬性,用于控制段落有關(guān)屬性(行間距,文本縮進(jìn)等等)。

1、NSAttributedString

不可變屬性字符串,創(chuàng)建出來(lái)之后不能修改其屬性(屬性都是只讀的),但是可以在創(chuàng)建的時(shí)候直接附加屬性設(shè)置(屬性是針對(duì)所有文本),其屬性介紹以及用法,直接在代碼上標(biāo)注如下:

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ù)挖掘的推薦引擎產(chǎn)品,它為用戶推薦有價(jià)值的、個(gè)性化的信息,提供連接人與信息的新型服務(wù),是國(guó)內(nèi)移動(dòng)互聯(lián)網(wǎng)領(lǐng)域成長(zhǎng)最快的產(chǎn)品服務(wù)之一。";? ? NSMutableDictionary *dict = [NSMutableDictionary dictionary];//? ? dict[NSForegroundColorAttributeName] = [UIColor redColor];? ? ? ? ? ? // UIColor,文本前景顏色,默認(rèn)是blackColor//? ? dict[NSBackgroundColorAttributeName] = [UIColor yellowColor];? ? ? ? ? ? // UIColor,文本背景顏色(不是控件View的背景顏色),默認(rèn)nil//? ? dict[NSFontAttributeName] = [UIFont systemFontOfSize:20];? ? ? ? ? ? ? ? // UIFont,文本字體大小,默認(rèn) 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;? ? ? ? ? ? ? ? ? ? // 非首行文本縮進(jìn)//? ? paragraphStyle.tailIndent = -20;? ? ? ? ? ? ? ? ? // 文本縮進(jìn)(右端)//? ? paragraphStyle.firstLineHeadIndent = 20;? ? ? ? ? // 首行文本縮進(jìn)//? ? paragraphStyle.alignment = NSTextAlignmentRight;? // 文本對(duì)齊方式//? ? paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping; // 折行方式//? ? paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight; // 文本寫(xiě)入方式//? ? paragraphStyle.lineHeightMultiple = 3.0;? ? ? ? ? // 文本行間距是默認(rèn)行間距的多少倍//? ? paragraphStyle.maximumLineHeight = 50;? ? ? ? ? ? // 文本最大行距//? ? paragraphStyle.minimumLineHeight = 50;? ? ? ? ? ? // 文本最小行距//? ? paragraphStyle.allowsDefaultTighteningForTruncation = YES; // 目前還不知道有什么作用//? ? paragraphStyle.hyphenationFactor = 1.0; // 設(shè)置每行的最后單詞是否截?cái)?,?.0-1.0之間,默認(rèn)為0.0,越接近1.0單詞被截?cái)嗟目赡苄栽酱螅?/? ? paragraphStyle.paragraphSpacing = 10;? ? ? ? ? ? // 段落后面的間距//? ? paragraphStyle.paragraphSpacingBefore = 20;? ? ? //設(shè)置段與段之間的距離*///? ? dict[NSParagraphStyleAttributeName] = paragraphStyle;? ? ? ? ? ? ? ? ? ? // NSParagraphStyle,文本段落樣式//? ? dict[NSStrikethroughStyleAttributeName] = @(NSUnderlinePatternSolid | NSUnderlineStyleSingle);? ? ? // NSNumber,加刪除線,默認(rèn)不加刪除線,其它的話是加不同風(fēng)格的刪除線//? ? dict[NSStrikethroughColorAttributeName] = [UIColor yellowColor];? ? ? ? ? // UIColor,刪除線顏色,默認(rèn)等于文本前景顏色,前提是需要加刪除線,和NSStrikethroughStyleAttributeName有關(guān)//? ? dict[NSUnderlineStyleAttributeName] = @(NSUnderlineStyleDouble);? ? ? ? ? // NSNumber,加下劃線,默認(rèn)NSUnderlineStyleNone不加下劃線,其它的話是加不同的下劃線//? ? dict[NSUnderlineColorAttributeName] = [UIColor yellowColor];? ? ? ? ? ? ? // UIColor,下劃線顏色,默認(rèn)等于文本前景顏色,前提是需要加下劃線,和NSUnderlineStyleAttributeName有關(guān)//? ? dict[NSStrokeColorAttributeName] = [UIColor yellowColor];? ? ? ? ? ? ? ? // UIColor,默認(rèn)等于文本前景顏色,需要和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,默認(rèn)沒(méi)有陰影,//? ? dict[NSKernAttributeName] = @10;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // NSNumber,文本字間距(字與字之間的距離)//? ? dict[NSLinkAttributeName] = [NSURL URLWithString:@"http:baidu.com"];? ? ? // NSURL或者是鏈接字符串,文本鏈接樣式,自帶下劃線,文本顏色是藍(lán)色//? ? ? ? dict[NSLinkAttributeName] = @"http:baidu.com";? ? ? ? ? ? ? ? ? ? ? ? ? ? // NSURL或者是鏈接字符串,文本鏈接樣式,自帶下劃線,文本顏色是藍(lán)色//? ? dict[NSExpansionAttributeName] = @(-0.5);? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // NSNumber,本文的擴(kuò)張倍數(shù),負(fù)數(shù)的話相當(dāng)于縮小//? ? dict[NSObliquenessAttributeName] = @(0.7);? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // NSNumber,本文的斜體程度以及斜體方向,默認(rèn)0不歪斜,負(fù)數(shù)相當(dāng)于右斜,正數(shù)相當(dāng)于左斜,歪斜的程度由數(shù)字的大小決定//? ? dict[NSBaselineOffsetAttributeName] = @(15.0);? ? ? ? ? ? ? ? ? ? ? ? ? ? // NSNumber,行文本基線的偏移量,//? ? dict[NSWritingDirectionAttributeName] = @[@(NSWritingDirectionOverride),@(NSWritingDirectionRightToLeft)]; // 貌似是文本寫(xiě)入方向//? ? 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)建出來(lái)之后可修改其屬性,也可以給文本在某一個(gè)范圍內(nèi)添加單個(gè)屬性或者字典屬性(一次性添加很多屬性),比如一個(gè)屬性字符串不同范圍的顏色不一樣,其屬性介紹以及用法,直接在代碼上標(biāo)注如下:

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ù)挖掘的推薦引擎產(chǎn)品,它為用戶推薦有價(jià)值的、個(gè)性化的信息,提供連接人與信息的新型服務(wù),是國(guó)內(nèi)移動(dòng)互聯(lián)網(wǎng)領(lǐng)域成長(zhǎng)最快的產(chǎn)品服務(wù)之一。";? ? 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

用于設(shè)定文本段落有關(guān)設(shè)置,比如行間距,文本縮進(jìn),段間距等等,其用法如下:

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ù)挖掘的推薦引擎產(chǎn)品,它為用戶推薦有價(jià)值的、個(gè)性化的信息,提供連接人與信息的新型服務(wù),是國(guó)內(nèi)移動(dòng)互聯(lián)網(wǎng)領(lǐng)域成長(zhǎng)最快的產(chǎn)品服務(wù)之一。";? ? /*? 文本段落樣式//? ? NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];//? ? paragraphStyle.lineSpacing = 10;? ? ? ? ? ? ? ? ? // 段落行距//? ? paragraphStyle.headIndent = 5;? ? ? ? ? ? ? ? ? ? // 非首行文本縮進(jìn)//? ? paragraphStyle.tailIndent = -20;? ? ? ? ? ? ? ? ? // 文本縮進(jìn)(右端)//? ? paragraphStyle.firstLineHeadIndent = 20;? ? ? ? ? // 首行文本縮進(jìn)//? ? paragraphStyle.alignment = NSTextAlignmentRight;? // 文本對(duì)齊方式//? ? paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping; // 折行方式//? ? paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight; // 文本寫(xiě)入方式//? ? paragraphStyle.lineHeightMultiple = 3.0;? ? ? ? ? // 文本行間距是默認(rèn)行間距的多少倍//? ? paragraphStyle.maximumLineHeight = 50;? ? ? ? ? ? // 文本最大行距//? ? paragraphStyle.minimumLineHeight = 50;? ? ? ? ? ? // 文本最小行距//? ? paragraphStyle.allowsDefaultTighteningForTruncation = YES; // 目前還不知道有什么作用//? ? paragraphStyle.hyphenationFactor = 1.0; // 設(shè)置每行的最后單詞是否截?cái)啵?.0-1.0之間,默認(rèn)為0.0,越接近1.0單詞被截?cái)嗟目赡苄栽酱螅?/? ? paragraphStyle.paragraphSpacing = 10;? ? ? ? ? ? // 段落后面的間距//? ? paragraphStyle.paragraphSpacingBefore = 20;? ? ? //設(shè)置段與段之間的距離*///? ? dict[NSParagraphStyleAttributeName] = paragraphStyle;? ? ? ? ? ? ? ? ? ? // NSParagraphStyle,文本段落樣式label.attributedText = [[NSAttributedString alloc] initWithString:string attributes:dict];

屬性字符串基本上就那么多內(nèi)容,只不過(guò)需要在應(yīng)用中靈活運(yùn)用而已。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容