iOS 中的 富文本文字

轉(zhuǎn)載:http://blog.csdn.net/u010330109/article/details/51882122

iOS富文本字符串AttributedString詳解: ?http://www.lxweimin.com/p/9ffcdc0003e0

iOS 富文本可點(diǎn)擊: ?http://www.lxweimin.com/p/e5fcf8f74997

首先先了解一下NSMutableAttributedString:

初始化方法:

-(instancetype)initWithString:(NSString*)str;-(instancetype)initWithString:(NSString*)str attributes:(nullableNSDictionary *)attrs;

包含的幾個(gè)基本方法:

- (void)addAttribute:(NSString*)name value:(id)value range:(NSRange)range;//為某一范圍內(nèi)文字添加某個(gè)屬性

- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;//為某一范圍內(nèi)文字設(shè)置多個(gè)屬性

- (void)removeAttribute:(NSString*)name range:(NSRange)range;//移除某范圍內(nèi)的某個(gè)屬性

初始化的方法和NSMutableString一樣。

NSFontAttributeName(字體)

該屬性所對應(yīng)的值是一個(gè) UIFont 對象。該屬性用于改變一段文本的字體。如果不指定該屬性,則默認(rèn)為12-point Helvetica(Neue)。

NSParagraphStyleAttributeName(段落)

該屬性所對應(yīng)的值是一個(gè) NSParagraphStyle 對象。該屬性在一段文本上應(yīng)用多個(gè)屬性。如果不指定該屬性,則默認(rèn)為 NSParagraphStyle 的defaultParagraphStyle 方法返回的默認(rèn)段落屬性。

NSMutableParagraphStyle*paragraph = [[NSMutableParagraphStylealloc] init];paragraph.alignment =NSTextAlignmentCenter;

NSForegroundColorAttributeName(字體顏色)

該屬性所對應(yīng)的值是一個(gè) UIColor 對象。該屬性用于指定一段文本的字體顏色。如果不指定該屬性,則默認(rèn)為黑色。

// NSForegroundColorAttributeName
NSDictionary*attrDict1 = @{NSForegroundColorAttributeName: [UIColorredColor] };

NSDictionary*attrDict2 = @{NSForegroundColorAttributeName: [UIColorblueColor] };

NSDictionary*attrDict3 = @{NSForegroundColorAttributeName: [UIColororangeColor] };

_label01.attributedText = [[NSAttributedStringalloc] initWithString: originStr attributes: attrDict1];

_label02.attributedText = [[NSAttributedStringalloc] initWithString: originStr attributes: attrDict2];

_label03.attributedText = [[NSAttributedStringalloc] initWithString: originStr attributes: attrDict3];

注意:

NSForegroundColorAttributeName 設(shè)置的顏色與 UILabel 的 textColor 屬性設(shè)置的顏色在地位上是相等的,與 NSBackgroundColorAttributeName 地位上也相等,誰最后賦值,最終顯示的就是誰的顏色,但是textColor屬性可以與 NSBackgroundColorAttributeName 屬性可疊加。

NSBackgroundColorAttributeName(字體背景色)

該屬性所對應(yīng)的值是一個(gè) UIColor 對象。該屬性用于指定一段文本的背景顏色。如果不指定該屬性,則默認(rèn)無背景色。

NSLigatureAttributeName(連字符)

該屬性所對應(yīng)的值是一個(gè) NSNumber 對象(整數(shù))。連體字符是指某些連在一起的字符,它們采用單個(gè)的圖元符號。0 表示沒有連體字符。1 表示使用默認(rèn)的連體字符。2表示使用所有連體符號。默認(rèn)值為 1(注意,iOS 不支持值為 2)。

NSString *ligatureStr = @"flush";

NSDictionary *attrDict1 = @{NSLigatureAttributeName:[NSNumbernumberWithInt:0],NSFontAttributeName:[UIFontfontWithName:@"futura"size:30] };

_label01.attributedText = [[NSAttributedString alloc]initWithString:ligatureStrattributes:attrDict1];

NSDictionary *attrDict2 = @{NSLigatureAttributeName:@(1),NSFontAttributeName:[UIFontfontWithName:@"futura"size:30]? ? ? ? ? ? ? ? ? ? ? ? ? ? ? };

_label02.attributedText = [[NSAttributedString alloc]initWithString:ligatureStrattributes:attrDict2];

由于要展示連體字符,所以將前面使用的帶有中文的字符串換成 flush

NSLigatureAttributeName的取值為NSNumber對象,所以不能直接將一個(gè)整數(shù)值賦給它,創(chuàng)建 NSNumber 對象的方法有很多,或者可以簡寫成 @(int)

注意觀察字母f和l之間的變化。

感覺連寫就是一個(gè)藝術(shù)字功能,當(dāng)字符f和l組合使用組合符號(所謂的字形(glyph))繪制時(shí),看起來確實(shí)更加美觀。但是并非所有的字符之間都有組合符號,事實(shí)上,只有某些字體中得某些字符的組合(如字符f和l,字符f和i等)才具有美觀的組合符號。

NSKernAttributeName(字間距)

NSKernAttributeName 設(shè)定字符間距,取值為 NSNumber 對象(整數(shù)),正值間距加寬,負(fù)值間距變窄

NSDictionary*attrDict1 = @{NSKernAttributeName: @(-3),NSFontAttributeName: [UIFontsystemFontOfSize:20]? ? ? ? ? ? ? ? ? ? ? ? ? ? ? };?

?_label01.attributedText = [[NSAttributedStringalloc] initWithString: originStr attributes: attrDict1];

NSDictionary*attrDict2 = @{NSKernAttributeName: @(0),NSFontAttributeName: [UIFontsystemFontOfSize:20]? ? ? ? ? ? ? ? ? ? ? ? ? ? ? };

_label02.attributedText = [[NSAttributedStringalloc] initWithString: originStr attributes: attrDict2];

NSDictionary*attrDict3 = @{NSKernAttributeName: @(10),NSFontAttributeName: [UIFontsystemFontOfSize:20]? ? ? ? ? ? ? ? ? ? ? ? ? ? ? };

_label03.attributedText = [[NSAttributedStringalloc] initWithString: originStr attributes: attrDict3];

NSStrikethroughStyleAttributeName(刪除線)

NSStrikethroughStyleAttributeName 設(shè)置刪除線,取值為 NSNumber 對象(整數(shù)),枚舉常量 NSUnderlineStyle中的值:

NSUnderlineStyleNone 不設(shè)置刪除線

NSUnderlineStyleSingle 設(shè)置刪除線為細(xì)單實(shí)線

NSUnderlineStyleThick 設(shè)置刪除線為粗單實(shí)線

NSUnderlineStyleDouble 設(shè)置刪除線為細(xì)雙實(shí)線

默認(rèn)值是NSUnderlineStyleNone。

NSDictionary*attrDict1 = @{NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle),NSFontAttributeName: [UIFontsystemFontOfSize:20] };

_label01.attributedText = [[NSAttributedStringalloc] initWithString: originStr attributes: attrDict1];

NSDictionary*attrDict2 = @{NSStrikethroughStyleAttributeName: @(NSUnderlineStyleThick),NSFontAttributeName: [UIFontsystemFontOfSize:20] };

_label02.attributedText = [[NSAttributedStringalloc] initWithString: originStr attributes: attrDict2];

NSDictionary*attrDict3 = @{NSStrikethroughStyleAttributeName: @(NSUnderlineStyleDouble),NSFontAttributeName: [UIFontsystemFontOfSize:20] };

_label03.attributedText = [[NSAttributedStringalloc] initWithString: originStr attributes: attrDict3];

注意:

雖然使用了枚舉常量,但是枚舉常量的本質(zhì)仍為整數(shù),所以同樣必須先轉(zhuǎn)化為 NSNumber 才能使用

刪除線和下劃線使用相同的枚舉常量作為其屬性值

目前iOS中只有上面列出的4中效果,雖然我們能夠在頭文件中發(fā)現(xiàn)其他更多的取值,但是使用后沒有任何效果

可以看出,中文和英文刪除線的位置有所不同

另外,刪除線屬性取值除了上面的4種外,其實(shí)還可以取其他整數(shù)值,有興趣的可以自行試驗(yàn),取值為 0 - 7時(shí),效果為單實(shí)線,隨著值得增加,單實(shí)線逐漸變粗,取值為 9 - 15時(shí),效果為雙實(shí)線,取值越大,雙實(shí)線越粗。

NSDictionary*attrDict1 = @{NSStrikethroughStyleAttributeName: @(1),NSFontAttributeName: [UIFontsystemFontOfSize:20] };

_label01.attributedText = [[NSAttributedStringalloc] initWithString: originStr attributes: attrDict1];

NSDictionary*attrDict2 = @{NSStrikethroughStyleAttributeName: @(3),NSFontAttributeName: [UIFontsystemFontOfSize:20] };

_label02.attributedText = [[NSAttributedStringalloc] initWithString: originStr attributes: attrDict2];

NSDictionary*attrDict3 = @{NSStrikethroughStyleAttributeName: @(7),NSFontAttributeName: [UIFontsystemFontOfSize:20] };

_label03.attributedText = [[NSAttributedStringalloc] initWithString: originStr attributes: attrDict3];

NSStrikethroughColorAttributeName

NSStrikethroughColorAttributeName 設(shè)置刪除線顏色,取值為 UIColor 對象,默認(rèn)值為黑色

NSDictionary*attrDict1 = @{NSStrikethroughColorAttributeName: [UIColorblueColor],NSStrikethroughStyleAttributeName: @(1),NSFontAttributeName: [UIFontsystemFontOfSize:20] };

_label01.attributedText = [[NSAttributedStringalloc] initWithString: originStr attributes: attrDict1];

NSDictionary*attrDict2 = @{NSStrikethroughColorAttributeName: [UIColororangeColor],NSStrikethroughStyleAttributeName: @(3),NSFontAttributeName: [UIFontsystemFontOfSize:20] };

_label02.attributedText = [[NSAttributedStringalloc] initWithString: originStr attributes: attrDict2];

NSDictionary*attrDict3 = @{NSStrikethroughColorAttributeName: [UIColorgreenColor],NSStrikethroughStyleAttributeName: @(7),NSFontAttributeName: [UIFontsystemFontOfSize:20] };

_label03.attributedText = [[NSAttributedStringalloc] initWithString: originStr attributes: attrDict3];

NSUnderlineStyleAttributeName(下劃線)

該屬性所對應(yīng)的值是一個(gè) NSNumber 對象(整數(shù))。該值指定是否在文字上加上下劃線,該值參考“Underline Style Attributes”。默認(rèn)值是NSUnderlineStyleNone。

下劃線除了線條位置和刪除線不同外,其他的都可以完全參照刪除線設(shè)置。

NSUnderlineColorAttributeName

NSUnderlineColorAttributeName 設(shè)置下劃線顏色,取值為 UIColor 對象,默認(rèn)值為黑色

NSDictionary*attrDict1 = @{NSUnderlineColorAttributeName: [UIColorblueColor],NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),NSFontAttributeName: [UIFontsystemFontOfSize:20] };

_label01.attributedText = [[NSAttributedStringalloc] initWithString: originStr attributes: attrDict1];

NSDictionary*attrDict2 = @{NSUnderlineColorAttributeName: [UIColororangeColor],NSUnderlineStyleAttributeName: @(NSUnderlineStyleThick),NSFontAttributeName: [UIFontsystemFontOfSize:20] };

_label02.attributedText = [[NSAttributedStringalloc] initWithString: originStr attributes: attrDict2];

NSDictionary*attrDict3 = @{NSUnderlineColorAttributeName: [UIColorgreenColor],NSUnderlineStyleAttributeName: @(NSUnderlineStyleDouble),NSFontAttributeName: [UIFontsystemFontOfSize:20] };

_label03.attributedText = [[NSAttributedStringalloc] initWithString: originStr attributes: attrDict3];

NSStrokeColorAttributeName(邊線顏色) 和 NSStrokeWidthAttributeName(邊線寬度)

設(shè)置文字描邊顏色,需要和NSStrokeWidthAttributeName設(shè)置描邊寬度,這樣就能使文字空心.

NSStrokeWidthAttributeName 這個(gè)屬性所對應(yīng)的值是一個(gè) NSNumber 對象(小數(shù))。該值改變筆畫寬度(相對于字體 size 的百分比),負(fù)值填充效果,正值中空效果,默認(rèn)為 0,即不改變。正數(shù)只改變描邊寬度。負(fù)數(shù)同時(shí)改變文字的描邊和填充寬度。例如,對于常見的空心字,這個(gè)值通常為 3.0。

同時(shí)設(shè)置了空心的兩個(gè)屬性,并且 NSStrokeWidthAttributeName 屬性設(shè)置為整數(shù),文字前景色就無效果了

NSStrokeColorAttributeName 填充部分顏色,不是字體顏色,取值為 UIColor 對象

NSDictionary*attrDict1 = @{NSStrokeWidthAttributeName: @(-3),NSFontAttributeName: [UIFontsystemFontOfSize:30] };

_label01.attributedText = [[NSAttributedStringalloc] initWithString: originStr attributes: attrDict1];

NSDictionary*attrDict2 = @{NSStrokeWidthAttributeName: @(0),NSFontAttributeName: [UIFontsystemFontOfSize:30] };

_label02.attributedText = [[NSAttributedStringalloc] initWithString: originStr attributes: attrDict2];

NSDictionary*attrDict3 = @{NSStrokeWidthAttributeName: @(3),NSFontAttributeName: [UIFontsystemFontOfSize:30] };

_label03.attributedText = [[NSAttributedStringalloc] initWithString: originStr attributes: attrDict3];

NSDictionary*attrDict1 = @{NSStrokeWidthAttributeName: @(-3),NSStrokeColorAttributeName: [UIColororangeColor],NSFontAttributeName: [UIFontsystemFontOfSize:30] };

_label01.attributedText = [[NSAttributedStringalloc] initWithString: originStr attributes: attrDict1];

NSDictionary*attrDict2 = @{NSStrokeWidthAttributeName: @(0),NSStrokeColorAttributeName: [UIColorblueColor],NSFontAttributeName: [UIFontsystemFontOfSize:30] };

_label02.attributedText = [[NSAttributedStringalloc] initWithString: originStr attributes: attrDict2];

NSDictionary*attrDict3 = @{NSStrokeWidthAttributeName: @(3),NSStrokeColorAttributeName: [UIColorgreenColor],NSFontAttributeName: [UIFontsystemFontOfSize:30] };

_label03.attributedText = [[NSAttributedStringalloc] initWithString: originStr attributes: attrDict3];

NSShadowAttributeName(陰影)

該屬性所對應(yīng)的值是一個(gè) NSShadow 對象。默認(rèn)為 nil。單獨(dú)設(shè)置不好使,和這三個(gè)任一個(gè)都好使,NSVerticalGlyphFormAttributeName,NSObliquenessAttributeName,NSExpansionAttributeName

NSVerticalGlyphFormAttributeName(橫豎排版)

該屬性所對應(yīng)的值是一個(gè) NSNumber 對象(整數(shù))。0 表示橫排文本。1 表示豎排文本。在 iOS 中,總是使用橫排文本,0 以外的值都未定義。

NSObliquenessAttributeName(字體傾斜)

NSExpansionAttributeName (文本扁平化)

iOS開發(fā)中,常常會有一段文字顯示不同的顏色和字體,或者給某幾個(gè)文字加刪除線或下劃線的需求。之前在網(wǎng)上找了一些資料,有的是重繪UILabel的textLayer,有的是用HTML5實(shí)現(xiàn)的,都比較麻煩,而且很多UILabel的屬性也不起作用了,效果都不理想。后來了解到NSMuttableAttstring(帶屬性的字符串),上面的一些需求都可以很簡便的實(shí)現(xiàn)。

1.實(shí)例化方法和使用方法

實(shí)例化方法:

使用字符串初始化

- (id)initWithString:(NSString*)str;

例:

NSMutableAttributedString*AttributedStr = [[NSMutableAttributedStringalloc]initWithString:@"今天天氣不錯(cuò)呀"];

- (id)initWithString:(NSString*)str attributes:(NSDictionary*)attrs;

字典中存放一些屬性名和屬性值,如:

NSDictionary*attributeDict = [NSDictionarydictionaryWithObjectsAndKeys:

[UIFontsystemFontOfSize:15.0],NSFontAttributeName,

[UIColorredColor],NSForegroundColorAttributeName,

NSUnderlineStyleAttributeName,NSUnderlineStyleSingle,nil];

NSMutableAttributedString*AttributedStr = [[NSMutableAttributedStringalloc]initWithString:@"今天天氣不錯(cuò)呀"attributes:attributeDict];

- (id)initWithAttributedString:(NSAttributedString*)attester;

使用NSAttributedString初始化,跟NSMutableString,NSString類似

使用方法:

為某一范圍內(nèi)文字設(shè)置多個(gè)屬性

- (void)setAttributes:(NSDictionary*)attrs range:(NSRange)range;

為某一范圍內(nèi)文字添加某個(gè)屬性

- (void)addAttribute:(NSString*)name value:(id)value range:(NSRange)range;

為某一范圍內(nèi)文字添加多個(gè)屬性

- (void)addAttributes:(NSDictionary*)attrs range:(NSRange)range;

移除某范圍內(nèi)的某個(gè)屬性

- (void)removeAttribute:(NSString*)name range:(NSRange)range;

2.常見的屬性及說明

NSFontAttributeName字體

NSParagraphStyleAttributeName段落格式

NSForegroundColorAttributeName字體顏色

NSBackgroundColorAttributeName背景顏色

NSStrikethroughStyleAttributeName刪除線格式

NSUnderlineStyleAttributeName下劃線格式

NSStrokeColorAttributeName刪除線顏色

NSStrokeWidthAttributeName刪除線寬度

NSShadowAttributeName陰影

更多方法和屬性說明詳見蘋果官方說明文檔:

https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableAttributedString_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40003689

3.使用實(shí)例

UILabel*testLabel=[[UILabelalloc]initWithFrame:CGRectMake(0,100,320,30)];

testLabel.backgroundColor=[UIColorlightGrayColor];

testLabel.textAlignment=NSTextAlignmentCenter;

NSMutableAttributedString*AttributedStr=[[NSMutableAttributedStringalloc]initWithString:@"今天天氣不錯(cuò)呀"];

[AttributedStraddAttribute:NSFontAttributeName

value:[UIFontsystemFontOfSize:16.0]

range:NSMakeRange(2,2)];

[AttributedStraddAttribute:NSForegroundColorAttributeName

value:[UIColorredColor]

range:NSMakeRange(2,2)];

testLabel.attributedText=AttributedStr;

[self.viewaddSubview:testLabel];

運(yùn)行效果:

另外,其他可以設(shè)置text的控件(如UIButton,UITextField)也都有該屬性,該文章不夠詳細(xì),只是簡單介紹,其他效果的實(shí)現(xiàn)參考API中更多的屬性及使用方法。




///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

項(xiàng)目上要加載html格式的文本,學(xué)習(xí)一下富文本相關(guān)內(nèi)容。

1.加載HTML標(biāo)簽文本

因?yàn)榻馕龅臄?shù)據(jù)里面有html標(biāo)簽,就使用下面的代碼把字符串轉(zhuǎn)換成data,初始化時(shí)再用HTML類型,轉(zhuǎn)換為富文本。

NSMutableAttributedString* attrStr = [[NSMutableAttributedStringalloc]initWithData:[strdataUsingEncoding:NSUnicodeStringEncoding]options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType}documentAttributes:nilerror:nil];

參數(shù)options里面的字典有三對key value

文檔類型NSDocumentTypeDocumentAttribute

NSPlainTextDocumentType// 普通文本NSRTFTextDocumentType//? 富文本NSRTFDTextDocumentType// 帶附件的富文本NSHTMLTextDocumentType// 這個(gè)可以加載HTML格式的文本

編碼格式NSCharacterEncodingDocumentAttribute

[NSNumber numberWithInt:NSUTF8StringEncoding]; // 不再一一列舉

默認(rèn)NSDefaultAttributesDocumentAttribute

這個(gè)不知道對應(yīng)的value是什么

2.富文本總結(jié)

這是富文本的所有屬性

屬性Name干啥的類型

NSFontAttributeName字號UIFont 默認(rèn)12

NSParagraphStyleAttributeName段落樣式NSParagraphStyle

NSForegroundColorAttributeName前景色UIColor

NSBackgroundColorAttributeName背景色UIColor

NSObliquenessAttributeName字體傾斜NSNumber

NSExpansionAttributeName字體加粗NSNumber 比例 0就是不變 1增加一倍

NSKernAttributeName字間距CGFloat

NSUnderlineStyleAttributeName下劃線1或0

NSUnderlineColorAttributeName下劃線顏色UIColor

NSStrikethroughStyleAttributeName刪除線1或0

NSStrikethroughColorAttributeName刪除線顏色UIColor

NSStrokeColorAttributeNamesame as ForegroundColorUIColor

NSStrokeWidthAttributeName字體描邊CGFloat

NSLigatureAttributeName連筆字 沒看出效果1或0

NSShadowAttributeName陰影NSShawdow

NSTextEffectAttributeName設(shè)置文本特殊效果,目前只有圖版印刷效果可用NSString

NSAttachmentAttributeName設(shè)置文本附件,常用插入圖片NSTextAttachment

NSLinkAttributeName鏈接NSURL (preferred) or NSString

NSBaselineOffsetAttributeName基準(zhǔn)線偏移NSNumber

NSWritingDirectionAttributeName文字方向 分別代表不同的文字出現(xiàn)方向等等,我想你一定用不到它 - -@[@(1),@(2)]

NSVerticalGlyphFormAttributeName水平或者豎直文本 在iOS沒卵用,不支持豎版1豎直 0水平

解釋一下其中的三個(gè)類型

段落樣式

段落樣式主要改行距、段距、首行縮進(jìn)、最大最小行高、多倍行距等十幾個(gè)屬性,把這些總結(jié)了你就比我更全..

NSMutableParagraphStyle *muParagraph = [[NSMutableParagraphStyle alloc]init];? ? muParagraph.lineSpacing =10;// 行距

muParagraph.paragraphSpacing =20;// 段距

muParagraph.firstLineHeadIndent =30;// 首行縮進(jìn)

陰影

就三屬性,不用解釋了

NSShadow *shadow= [[NSShadow alloc]init];shadow.shadowOffset = CGSizeMake(2,2);

shadow.shadowColor = [UIColor orangeColor];shadow.shadowBlurRadius =1;

這個(gè)我的也沒顯示出來,想鉆研的看這里http://www.lxweimin.com/p/5babe8b7983e

NSTextAttachment *attachment=[[NSTextAttachment alloc] initWithData:nilofType:nil];UIImage*img=[UIImageimageNamed:@"test.png"];? ?

?attachment.image=img;? ?

?attachment.bounds=CGRectMake(0,0,20,20);

下面是我的富文本代碼,可以copy過去直接試

- (void)viewDidLoad {??

? [super viewDidLoad];? ??

self.view.backgroundColor = [UIColor grayColor];? ??

[self.view addSubview:self.attTV];

//NSFontAttributeName? 字號 UIFont 默認(rèn)12

//NSParagraphStyleAttributeName? ? 段落樣式? NSParagraphStyle

//NSForegroundColorAttributeName? ? 前景色? UIColor

//NSBackgroundColorAttributeName? ? 背景色? UIColor//NSObliquenessAttributeName? ? ? ? 字體傾斜? ? NSNumber//NSExpansionAttributeName? ? ? ? ? 字體加粗? ? NSNumber? 比例 0就是不變 1增加一倍//NSKernAttributeName? ? ? ? ? ? ? 字間距? CGFloat

//NSUnderlineStyleAttributeName? ? 下劃線? ? 1或0

//NSUnderlineColorAttributeName? ? 下劃線顏色

//NSStrikethroughStyleAttributeName 刪除線? 1或0

//NSStrikethroughColorAttributeName 某種顏色

//NSStrokeColorAttributeName? ? ? ? same as ForegroundColor

//NSStrokeWidthAttributeName? ? ? ? CGFloat

//NSLigatureAttributeName? ? ? ? ? 連筆字? 1或0? 沒看出效果

//NSShadowAttributeName? ? ? ? ? ? 陰影? ? NSShawdow

//NSTextEffectAttributeName? ? ? ? ? 設(shè)置文本特殊效果,取值為 NSString 對象,目前只有圖版印刷效果可用:

//NSAttachmentAttributeName? ? ? ? NSTextAttachment? 設(shè)置文本附件,常用插入圖片

//NSLinkAttributeName? ? ? ? ? ? ? 鏈接? NSURL (preferred) or NSString

//NSBaselineOffsetAttributeName? ? 基準(zhǔn)線偏移? NSNumber

//NSWritingDirectionAttributeName? 文字方向? ? @[@(1),@(2)]? 分別代表不同的文字出現(xiàn)方向等等,我想你一定用不到它 - -

//NSVerticalGlyphFormAttributeName? 水平或者豎直文本? 1豎直 0水平 在iOS沒卵用,不支持豎版NSParagraphStyle *paragraph = [[NSParagraphStyle alloc]init];? ? NSMutableParagraphStyle *muParagraph = [[NSMutableParagraphStyle alloc]init];? ??

muParagraph.lineSpacing =10;// 行距

muParagraph.paragraphSpacing =20;// 段距

muParagraph.firstLineHeadIndent =30;// 首行縮進(jìn)

NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[@"asdasdflhjlfsaiollzislooa"dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nilerror:nil];? ??

NSRangerange= NSMakeRange(0, attrStr.length);

// 設(shè)置字體大小

[attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30]range:range];

//字間距

[attrStr addAttribute:NSKernAttributeName value:@(2)range:range];

// 字體傾斜

[attrStr addAttribute:NSObliquenessAttributeName value:@(1)range:range];

// 字體加粗

[attrStr addAttribute:NSExpansionAttributeName value:@(0.5)range:range];

// 下劃線

[attrStr addAttribute:NSUnderlineStyleAttributeName value:@(1)range:range];? ? [attrStr addAttribute:NSUnderlineColorAttributeName value:[UIColor blueColor]range:range];

// 刪除線

[attrStr addAttribute:NSStrikethroughStyleAttributeName value:@(1)range:range];? ? [attrStr addAttribute:NSStrikethroughColorAttributeName value:[UIColor greenColor]range:range];

// 連體字

[attrStr addAttribute:NSLigatureAttributeName value:@(1)range:range];

// 設(shè)置顏色

[attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0.942green:0.611blue:0.771alpha:1.000]range:range];

// 背景色

[attrStr addAttribute:NSBackgroundColorAttributeName value:[UIColor colorWithRed:0.475green:0.482blue:0.942alpha:1.000]range:range];

// stroke

[attrStr addAttribute:NSStrokeColorAttributeName value:[UIColor blueColor]range:range];? ? [attrStr addAttribute:NSStrokeWidthAttributeName value:@(2)range:range];

// 設(shè)置段落樣式

[attrStr addAttribute:NSParagraphStyleAttributeName value:muParagraphrange:range];

// 文本方向

[attrStr addAttribute:NSVerticalGlyphFormAttributeName value:@(1)range:range];? ?

?[attrStr addAttribute:NSWritingDirectionAttributeName value:@[@(2),@(3)]range:range];

// 陰影

NSShadow *shadow = [[NSShadow alloc]init];? ??

shadow.shadowOffset = CGSizeMake(2,2);? ??

shadow.shadowColor = [UIColor orangeColor];? ??

shadow.shadowBlurRadius =1;? ?

?[attrStr addAttribute:NSShadowAttributeName value:shadowrange:range];

// 鏈接

[attrStr addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"http://www.lxweimin.com/p/8f49c9c99b21"]range:range];

// 文字中加圖片

NSTextAttachment *attachment=[[NSTextAttachment alloc] initWithData:nilofType:nil];

? ? UIImage *img=[UIImage imageNamed:@"test.png"];??

? attachment.image=img;? ??

attachment.bounds=CGRectMake(0,0,20,20);? ??

[attrStr addAttribute:NSAttachmentAttributeName value:attachmentrange:range];

// 基準(zhǔn)線偏移

[attrStr addAttribute:NSBaselineOffsetAttributeName value:@(50)range:range];? ? self.attTV.attributedText = attrStr;}

我的效果圖很磕饞

3.TextView行距字間距的問題

同時(shí)遇到一個(gè)需求,TextView輸入的內(nèi)容的行距字間距進(jìn)行調(diào)整,這樣輸入一大段字會清楚一點(diǎn)。

網(wǎng)上的方法是在- (void)textViewDidChange:(UITextView *)textView代理方法中根據(jù)text生成對應(yīng)格式的attributedText

- (void)textViewDidChange:(UITextView*)textView{? ??

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];? ? paragraphStyle.lineSpacing=16;

// 字體的行間距NSDictionary*attributes = @{? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName:[UIFontsystemFontOfSize:17],? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSParagraphStyleAttributeName:paragraphStyle,? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSKernAttributeName : @(1.4f)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? };? ??

textView.attributedText= [[NSAttributedString alloc] initWithString:textView.textattributes:attributes];}

但是中文輸入法的時(shí)候就會懵逼,會同時(shí)打出英文

最后沒辦法,改成可視化的textView,調(diào)整行距的屬性,字間距就沒辦法了,誰有好辦法可以教教我

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 與NSString類似,在iOS中AttributedString也分為NSAttributedString和 N...
    錢十六閱讀 794評論 0 0
  • 一、關(guān)于AttributedString 在iOS開發(fā)過程中系統(tǒng)簡單的封裝工具已經(jīng)不夠我們使用,為了更好的開發(fā),蘋...
    iceMaple閱讀 7,024評論 2 31
  • 字體屬性設(shè)置示例:if(color ==nil) {color = [NSColor redColor];}NSF...
    袏扌戒指閱讀 8,457評論 0 2
  • by 北國的魚 (25) 童真的回憶,有如潺潺流動的溪流,將一個(gè)人的過往匯進(jìn)江河湖泊,分不清哪點(diǎn)來自童年,卻又不得...
    北國de魚閱讀 568評論 7 10
  • 慚蹈何械慚蹈何械q.q.1.2.3.3.4.5.6.7.87
    慚蹈何械3閱讀 135評論 0 1