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,612评论 25 708
  • iOS 7 引入了一个非常有用的新功能TextKit,使开发者可以通过方便的接口去修改文字的样式和排版,而不需要直...
    星___尘阅读 7,706评论 4 75
  • 一、什么是 Canvas? HTML5 的 canvas 元素使用 JavaScript 在网页上绘制图像 画布是...
    EndEvent阅读 721评论 0 1