1) 在iOS 7之前也有一種用於文字排版和渲染的技術——Core Text,而引入Text Kit的目的並非要取代Core Text。 Core Text是面層的文字排版和渲染技術,如果我們需要將文本內容接渲染到圖形上下文時,從性能角度考慮 ,最佳方案是使用Core Text。是從易用性角度考慮,使用Text Kit是最好的選擇,因為能直接使用UIKit 提供的一些文本控件,例如:UITextView、UILabel和UITextField,對文字進行排版。
Text Kit具有很多優點:文本控件UITextView、UITextField和UILabel是構建於Text Kit之上的。Text Kit完全 掌控着文字的排版和渲染:可以調整字距、行距、文字大小,指定定的字體,對文字進行分頁或分欄,支持文 本編輯、自定義文字截斷,支持文字的換行、折疊和着色等處理,支持凸版印刷效果。
2) 我們在使用Text Kit時,會涉及如下核心類。
-
NSTextContainer。定義了文本可以排版的區域。默認情況下是矩形區域,如果是其他形狀的區域,需要通過子類化NSTextContainer來創建。
-
NSLayoutManager。該類責對文字進行編輯排版處理,將存儲在NSTextStorage中的數據轉換為可以在視圖控件中顯示的文本內容,並把字字符編碼映射到對應的字形上,然后將字形排版到NSTextContainer定的區域中。
-
NSTextStorage。主要用來存儲文本的字和相關屬性,是NSMutableAttributedString的子類。此外,NSTextStorage中的字符或屬性發生改變時,會通知NSLayoutManager,進而做到文本內容的顯示更新。
-
NSAttributedString。支持渲染不同風格的文本。
-
NSMutableAttributedString。可變類型的NSAttributedString,是NSAttributedString的子類
3)讀者喜歡閱讀圖文並茂的文章,因此在應用界面中,有時不僅僅需要有文字,還要有圖片,這就涉及文字和圖 片的混排了。在圖文混排過程中必然會涉及文字圖片的情況,很多文字處理件(如Word、WPS、Open Office 等)有這種功能。Text Kit通過(exclusion paths)將文字按照指定的路徑在圖片等視圖對象的周圍。
圖文混排的介紹基本就是這樣了,下面就直接上方式方法吧!
*.h文件
@property (nonatomic,strong) NSTextContainer *textContainer; @property (strong, nonatomic) IBOutlet UITextView *textView; @property (weak, nonatomic) IBOutlet UIImageView *imageView; /** *查找關鍵字修改顏色和樣式 * @param word 關鍵字 * @param textStorage NSTextStorage對象 */ -(void)markWord:(NSString *)word inTextStorage:(NSTextStorage *)textStorage;
*.m文件
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. //創建一個矩形區域 CGRect textViewRect=CGRectInset(self.view.bounds, 10.0, 20.0); //創建NSTextStorage對象,它需要一個字符串作為構造方法的參數,這里我們是從TextView 控件取出來付值給它的 NSTextStorage *textStorage=[[NSTextStorage alloc] initWithString:self.textView.text]; NSLayoutManager *layoutManager=[[NSLayoutManager alloc] init]; //將剛創建的nstextStorage和NSLayoutManager對象關聯起來 [textStorage addLayoutManager:layoutManager]; self.textContainer =[[NSTextContainer alloc] initWithSize:textViewRect.size]; //將NSLayoutManager和NSTextContainer關聯起來 [layoutManager addTextContainer:self.textContainer]; /** *重新構建原來的TextView控件,並且重新添加到視圖上。這主要是因為只有重新創建代碼 *才能通過Text Kit中NSLayoutManager來管理,而原來在Interface Builder中創建的TextView控件不*能使用了 */ [self.textView removeFromSuperview]; self.textView=[[UITextView alloc] initWithFrame:textViewRect textContainer:_textContainer]; // [self.view addSubview:self.textView]; [self.textView setFont:[UIFont systemFontOfSize:20.0f]]; //添加的textView在ImageView之下 [self.view insertSubview:self.textView belowSubview:self.imageView]; //設置凸版印刷效果 [textStorage beginEditing]; /** *聲明一個字典對象,其中包括@{NSTextEffectAttribute-*Name:NSTextEffectLetterpressStyle},NSTextEffectAttributeName是文本效果建,而*NSTextEffect- LetterpressStyle是文本效果值,這里面它們都是常量 */ NSDictionary *attrsDic = @{NSTextEffectAttributeName: NSTextEffectLetterpressStyle}; NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:_textView.text attributes:attrsDic]; [textStorage setAttributedString:attrStr]; [self markWord:@"我" inTextStorage:textStorage]; [self markWord:@"N" inTextStorage:textStorage]; [textStorage endEditing]; self.textView.textContainer.exclusionPaths=@[[self translatedBezierPath]];//設置translatedBezierPath方法 } //改變textview和imageView的坐標 - (UIBezierPath *)translatedBezierPath { CGRect imageRect = [self.textView convertRect:_imageView.frame fromView:self.view]; UIBezierPath *newPath = [UIBezierPath bezierPathWithRect:imageRect]; return newPath; } //根據指定的文本設置樣式風格 -(void)markWord:(NSString *)word inTextStorage:(NSTextStorage *)textStorage{ // NSRegularExpression *regex=[NSRegularExpression regularExpressionWithPattern:word options:0 error:nil]; //通過正則表達式NSRegularExpression對象對TextView中的文本內 容進行掃描,結果放到數組中 NSArray *matches=[regex matchesInString:self.textView.text options:0 range:NSMakeRange(0, [self.textView.text length])]; //為找到的文本設置顏色 for (NSTextCheckingResult *match in matches) { NSRange matchRange=[match range]; [textStorage addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:matchRange]; } }
那么圖文混排的效果由上面的代碼就能完成 。
4)以前的iOS用戶會抱怨,為什么不能設置自定義字體呢?在iOS 7系統之后蘋果對於字體在顯示上做了一些優 化,讓不同大小的字體在屏幕上都能清晰地顯示。通常用戶設置了自己偏好的字體了,用戶可以(設置-》通用-》輔助功能)設置粗體文字的過程。用戶還可以在下圖所示的(設置-》通用-》文字大小) 是設置文字大小的過程。
但是並不是在設置中進行設置就萬事大吉了,我們還要在應用代碼中進行編程,以應對這些變化。我們需要 在應用中給文本控件設置為用戶設置的字體,而不是在代碼中編碼字體及大小。iOS 7中可以通過UIFont中新增的preferredFontForTextStyle:方法來取用戶設置的字體。
iOS 7中提供了6種字體樣式供選擇。
UIFontTextStyleHeadline。標題字體,例如:報紙的標題。
UIFontTextStyleSubheadline。子標題字體。
UIFontTextStyleBody。正文字體。
UIFontTextStyleFootnote。腳注字體。
UIFontTextStyleCaption1。標題字體,一般用於照片或者字幕。
UIFontTextStyleCaption2。另一個可選Caption字體
//監聽系統設置 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(preferredContentSizeChanged:) name:UIContentSizeCategoryDidChangeNotification object:nil];
//設置字體大小 -(void)preferredContentSizeChanged:(NSNotification *)notification{ self.textView.font=[UIFont preferredFontForTextStyle:UIFontTextStyleBody]; }
經過這兩步就能設置動態字體了!!!!!