TextKit學習筆記

TextKit中需要用到的幾個基本類:

  1. NSTextStorage
  2. NSLayoutManager
  3. NSTextContainer
  4. UITextView


NSTextStorage *sharedStoage = self.originalTextView.textStorage;
    [sharedStoage replaceCharactersInRange:NSMakeRange(0, 0) withString:[NSString stringWithContentsOfURL:[NSBundle.mainBundle URLForResource:@"lorem" withExtension:@"txt"]usedEncoding:NULL error:NULL]];
    
    NSLayoutManager *otherLayoutManager = [NSLayoutManager new];
    [sharedStoage addLayoutManager:otherLayoutManager];
    
    NSTextContainer *otherContainer = [NSTextContainer new];
    [otherLayoutManager addTextContainer:otherContainer];
    
    UITextView *otherTextView = [[UITextView alloc] initWithFrame:self.otherContainerView.bounds textContainer:otherContainer];
    otherTextView.backgroundColor = self.otherContainerView.backgroundColor;
    otherTextView.translatesAutoresizingMaskIntoConstraints = YES;
    otherTextView.scrollEnabled = NO;
    
    [self.otherContainerView addSubview:otherTextView];
    self.otherTextView = otherTextView;
    
    
    NSTextContainer *thirdTextContainer = [NSTextContainer new];
    [otherLayoutManager addTextContainer:thirdTextContainer];
    
    UITextView *thirdTextView = [[UITextView alloc] initWithFrame:self.thirdContainerView.bounds textContainer:thirdTextContainer];
    thirdTextView.backgroundColor = self.thirdContainerView.backgroundColor;
    thirdTextView.translatesAutoresizingMaskIntoConstraints = NO;
    thirdTextView.scrollEnabled = YES;
    [self.thirdContainerView addSubview:thirdTextView];

一個簡單的demo,了解TextKit是怎么運作的!

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

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,306評論 25 708
  • iOS 7 引入了一個非常有用的新功能TextKit,使開發者可以通過方便的接口去修改文字的樣式和排版,而不需要直...
    星___塵閱讀 7,689評論 4 75
  • 一、什么是 Canvas? HTML5 的 canvas 元素使用 JavaScript 在網頁上繪制圖像 畫布是...
    EndEvent閱讀 721評論 0 1