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是怎么運作的!

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

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

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

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