使用TextKit

轉載: 經典必看總結 ?http://www.itnose.net/detail/6177538.html

Text Kit學習(入門和進階):http://www.cocoachina.com/industry/20131028/7250.html

iOS文本布局探討之一——文本布局框架TextKit淺析:https://yq.aliyun.com/articles/60173


TextKit是在iOS7中新出的,實現了對CoreText的封裝,使用起來更加方便.

雖然是新出的,但也不代表立馬就能上手-_-!!,TextKit可以實現圖文混排效果,很好用.

1. 使用TextKit加載基本的文本

- (void)viewDidLoad

{

[super viewDidLoad];

//裝載內容的容器

NSTextStorage *storage = [NSTextStoragenew];

[storage replaceCharactersInRange:NSMakeRange(0,0)

withString:@"未選擇的路-弗羅斯特\n\n黃色的樹林里分出兩條路,\n可惜我不能同時去涉足,\n我在那路口久久佇立,\n我向著一條路極目望去,\n直到它消失在叢林深處。\n但我卻選了另外一條路,\n它荒草萋萋,十分幽寂,\n顯得更誘人、更美麗,\n雖然在這兩條小路上,\n都很少留下旅人的足跡,\n雖然那天清晨落葉滿地,\n兩條路都未經腳印污染。\n啊,留下一條路等改日再見!\n但我知道路徑延綿無盡頭,\n恐怕我難以再回返。\n也許多少年后在某個地方,\n我將輕聲嘆息把往事回顧,\n一片樹林里分出兩條路,\n而我選了人跡更少的一條,\n從此決定了我一生的道路。"];

//給內容容器添加布局(可以添加多個)

NSLayoutManager *layoutManager = [NSLayoutManagernew];

[storage addLayoutManager:layoutManager];

//帶有內容和布局的容器NSTextContainer *textContainer = [NSTextContainernew];

[layoutManager addTextContainer:textContainer];

//給TextView添加帶有內容和布局的容器UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10,20,300,400)

textContainer:textContainer];

textView.layer.borderWidth=1;

textView.scrollEnabled=NO;

textView.editable=NO;

[self.view addSubview:textView];

}

實現的過程如下:

storage --> layoutManager --> textContainer --> textView

這.....顯示一串文本就要做這么多的事情.....

2. 高亮某些文本

- (void)viewDidLoad

{

[super viewDidLoad];

//裝載內容的容器

NSTextStorage *storage = [NSTextStoragenew];

[storage replaceCharactersInRange:NSMakeRange(0,0)

withString:@"未選擇的路-弗羅斯特\n\n黃色的樹林里分出兩條路,\n可惜我不能同時去涉足,\n我在那路口久久佇立,\n我向著一條路極目望去,\n直到它消失在叢林深處。\n但我卻選了另外一條路,\n它荒草萋萋,十分幽寂,\n顯得更誘人、更美麗,\n雖然在這兩條小路上,\n都很少留下旅人的足跡,\n雖然那天清晨落葉滿地,\n兩條路都未經腳印污染。\n啊,留下一條路等改日再見!\n但我知道路徑延綿無盡頭,\n恐怕我難以再回返。\n也許多少年后在某個地方,\n我將輕聲嘆息把往事回顧,\n一片樹林里分出兩條路,\n而我選了人跡更少的一條,\n從此決定了我一生的道路。"];

//高亮容器里面的某些內容

[storage addAttribute:NSForegroundColorAttributeName

value:[UIColor redColor]

range:NSMakeRange(0,5)];

[storage addAttribute:NSForegroundColorAttributeName

value:[UIColor greenColor]

range:NSMakeRange(6,4)];

//給內容容器添加布局(可以添加多個)

NSLayoutManager *layoutManager = [NSLayoutManagernew];

[storage addLayoutManager:layoutManager];

//帶有內容和布局的容器NSTextContainer *textContainer = [NSTextContainernew];

[layoutManager addTextContainer:textContainer];

//給TextView添加帶有內容和布局的容器

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10,20,300,400)

textContainer:textContainer];

textView.layer.borderWidth=1;

textView.scrollEnabled=NO;

textView.editable=NO;

[self.view addSubview:textView];

}

可以用來設置的屬性有這些,你懂得:)

/************************ Attributes ************************

//*Predefined character attributes for text. If the key is not in the dictionary, then use the default values as described below.*/

UIKIT_EXTERN NSString*constNSFontAttributeName NS_AVAILABLE_IOS(6_0);//UIFont, default Helvetica(Neue) 12

UIKIT_EXTERN NSString *constNSParagraphStyleAttributeName NS_AVAILABLE_IOS(6_0);//NSParagraphStyle, default defaultParagraphStyle

UIKIT_EXTERN NSString *constNSForegroundColorAttributeName NS_AVAILABLE_IOS(6_0);//UIColor, default blackColor

UIKIT_EXTERN NSString *constNSBackgroundColorAttributeName NS_AVAILABLE_IOS(6_0);//UIColor, default nil: no background

UIKIT_EXTERN NSString *constNSLigatureAttributeName NS_AVAILABLE_IOS(6_0);//NSNumber containing integer, default 1: default ligatures, 0: no ligatures

UIKIT_EXTERN NSString *constNSKernAttributeName NS_AVAILABLE_IOS(6_0);//NSNumber containing floating point value, in points; amount to modify default kerning. 0 means kerning is disabled. (note: values other than nil and 0 are unsupported on iOS)

UIKIT_EXTERN NSString *constNSStrikethroughStyleAttributeName NS_AVAILABLE_IOS(6_0);//NSNumber containing integer, default 0: no strikethrough

UIKIT_EXTERN NSString *constNSUnderlineStyleAttributeName NS_AVAILABLE_IOS(6_0);//NSNumber containing integer, default 0: no underline

UIKIT_EXTERN NSString *constNSStrokeColorAttributeName NS_AVAILABLE_IOS(6_0);//UIColor, default nil: same as foreground color

UIKIT_EXTERN NSString *constNSStrokeWidthAttributeName NS_AVAILABLE_IOS(6_0);//NSNumber containing floating point value, in percent of font point size, default 0: no stroke; positive for stroke alone, negative for stroke and fill (a typical value for outlined text would be 3.0)

UIKIT_EXTERN NSString *constNSShadowAttributeName NS_AVAILABLE_IOS(6_0);//NSShadow, default nil: no shadow

UIKIT_EXTERN NSString *constNSTextEffectAttributeName NS_AVAILABLE_IOS(7_0);//NSString, default nil: no text effect

UIKIT_EXTERN NSString*constNSAttachmentAttributeName NS_AVAILABLE_IOS(7_0);//NSTextAttachment, default nil

UIKIT_EXTERN NSString *constNSLinkAttributeName NS_AVAILABLE_IOS(7_0);//NSURL (preferred) or NSStringUIKIT_EXTERN NSString *constNSBaselineOffsetAttributeName NS_AVAILABLE_IOS(7_0);//NSNumber containing floating point value, in points; offset from baseline, default 0

UIKIT_EXTERN NSString *constNSUnderlineColorAttributeName NS_AVAILABLE_IOS(7_0);//UIColor, default nil: same as foreground color

UIKIT_EXTERN NSString *constNSStrikethroughColorAttributeName NS_AVAILABLE_IOS(7_0);//UIColor, default nil: same as foreground color

UIKIT_EXTERN NSString *constNSObliquenessAttributeName NS_AVAILABLE_IOS(7_0);//NSNumber containing floating point value; skew to be applied to glyphs, default 0: no skew

UIKIT_EXTERN NSString *constNSExpansionAttributeName NS_AVAILABLE_IOS(7_0);//NSNumber containing floating point value; log of expansion factor to be applied to glyphs, default 0: no expansion

UIKIT_EXTERN NSString*constNSWritingDirectionAttributeName NS_AVAILABLE_IOS(7_0);

//NSArray of NSNumbers representing the nested levels of writing direction overrides as defined by Unicode LRE, RLE, LRO, and RLO characters.? The control characters can be obtained by masking NSWritingDirection and NSTextWritingDirection values.? LRE: NSWritingDirectionLeftToRight|NSTextWritingDirectionEmbedding, RLE: NSWritingDirectionRightToLeft|NSTextWritingDirectionEmbedding, LRO: NSWritingDirectionLeftToRight|NSTextWritingDirectionOverride, RLO: NSWritingDirectionRightToLeft|NSTextWritingDirectionOverride,

UIKIT_EXTERN NSString*constNSVerticalGlyphFormAttributeName NS_AVAILABLE_IOS(6_0);

//An NSNumber containing an integer value.? 0 means horizontal text.? 1 indicates vertical text.? If not specified, it could follow higher-level vertical orientation settings.? Currently on iOS, it's always horizontal.? The behavior for any other value is undefined./*This defines currently supported values for NSUnderlineStyleAttributeName and NSStrikethroughStyleAttributeName.*/

3. 圖文混排

- (void)viewDidLoad

{

[super viewDidLoad];

//裝載內容的容器

NSTextStorage *storage = [NSTextStoragenew];

[storage replaceCharactersInRange:NSMakeRange(0,0)

withString:@"未選擇的路-弗羅斯特\n\n黃色的樹林里分出兩條路,\n可惜我不能同時去涉足,\n我在那路口久久佇立,\n我向著一條路極目望去,\n直到它消失在叢林深處。\n但我卻選了另外一條路,\n它荒草萋萋,十分幽寂,\n顯得更誘人、更美麗,\n雖然在這兩條小路上,\n都很少留下旅人的足跡,\n雖然那天清晨落葉滿地,\n兩條路都未經腳印污染。\n啊,留下一條路等改日再見!\n但我知道路徑延綿無盡頭,\n恐怕我難以再回返。\n也許多少年后在某個地方,\n我將輕聲嘆息把往事回顧,\n一片樹林里分出兩條路,\n而我選了人跡更少的一條,\n從此決定了我一生的道路。"];

//高亮容器里面的某些內容[storage addAttribute:NSForegroundColorAttributeName

value:[UIColor redColor]

range:NSMakeRange(0,5)];

[storage addAttribute:NSForegroundColorAttributeName

value:[UIColor greenColor]

range:NSMakeRange(6,4)];

//給內容容器添加布局(可以添加多個)NSLayoutManager *layoutManager = [NSLayoutManagernew];

[storage addLayoutManager:layoutManager];

//帶有內容和布局的容器NSTextContainer *textContainer = [NSTextContainernew];

[layoutManager addTextContainer:textContainer];

//設置textContainer中要排斥的路徑UIImage *image = [UIImage imageNamed:@"show"];

CGRect areaRect= CGRectMake(5,5, image.size.width, image.size.height);

UIBezierPath*ovalPath =\

[UIBezierPath bezierPathWithRect:areaRect];

textContainer.exclusionPaths=@[ovalPath];

//給TextView添加帶有內容和布局的容器

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10,20,300,400)

textContainer:textContainer];

textView.layer.borderWidth=1;

textView.scrollEnabled=YES;

textView.editable=YES;

[self.view addSubview:textView];

//要顯示的圖片UIImageView *showImageView =\

[[UIImageView alloc] initWithFrame:areaRect];

showImageView.image=image;

[textView addSubview:showImageView];

}

核心代碼:

這是初級的使用,強大的功能需要自己去挖掘了.....

使用段落樣式的代碼:

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

推薦閱讀更多精彩內容