TextKit中需要用到的几个基本类:
- NSTextStorage
- NSLayoutManager
- NSTextContainer
- 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是怎么运作的!