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

你可能感兴趣的:(TextKit学习笔记)