UITextKit框架的学习(二)

来源:互联网 发布:一元云购正版源码 编辑:程序博客网 时间:2024/06/05 15:14
// NSTextContainer描述了文本在屏幕上显示时的几何区域,每个text container与一个具体的UITextView相关联。如果你需要定义一个很复杂形状的区域来显示文本,你可能需要创建NSTextContainer子类。CGRect textViewRect = CGRectInset(self.view.bounds, 10.0, 20.0);NSTextContainer *container = [[NSTextContainer alloc] initWithSize:CGSizeMake(textViewRect.size.width, CGFLOAT_MAX)]; container.widthTracksTextView = YES; // NSLayoutManager作为文本控件中的排版引擎接收保存的文本并在屏幕上渲染出来。NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init]; [layoutManager addTextContainer:container];// NSTextStorage本身继承与NSMutableAttributedString,它是以attributed string的形式保存需要渲染的文本,并在文本内容改变的时候通知到对应的layout manager对象。通常你需要创建NSTextStorage的子类来在文本改变时进行文本显示样式的更新。self.textStorage = [[TextStorage alloc] init];[self.textStorage addLayoutManager:layoutManager];
更新样式
//NSTextStorage对样式的更新需要放在beginEditingendEditing方法之间[self.textStorage beginEditing];    [_storingText replaceCharactersInRange:range withString:str];[self.textStorage endEditing];
添加textView
UITextView *newTextView = [[UITextView alloc] initWithFrame:textViewRect textContainer:container];[self.view addSubview:self.textView];
0 0