CGLTextViewContainer是Jared Sinclair的关于UITextView的著名更新问题的优秀解决方案的轻量级版本。
基本上就是整章复制,但是有一些不同:它足够信任客户端,赋予他们内部文本视图的访问权限,从而减少了大约一半的代码量。是具有的(Has-a)与是(is-a)或假装是(pretends-to-be-a)。它允许配置文本容器的高度。并且它还考虑了文本视图文本容器缩进量的度量。
原始源码在这里:https://github.com/jaredsinclair/JTSTextView
希望iOS 9能继续iOS中长久的文本改进链,我们不再需要这些麻烦事。在此之前,这似乎是一个好的解决方案。
有一个基本示例项目,但基本上
a CGLTextViewContainer是UIScrollView,它作为子视图包含一个真的、真的非常高文本视图。
文本视图有多长取决于您的初始化。Sinclair先生选择了100,000 pts。这似乎是一个合理的默认值。使用initWithFrame: height:
来设置自定义值,或者仅使用initWithFrame:
来获取默认值。
如果您想使用所有这些漂亮的修复,您必须不要修改容器对象的文本视图的代理。在这里,我们把您当作成年人对待,人们。赢得这份信任。有提供代理转接。
欢迎更新和改进,但我非常希望在几个月后这段代码就过时了。一旦iOS 9发布官方UIKit组件,就尝试使用它们,并为他们祈祷。
// set up your text view, with a height (optionally) that you want to set your
// internal text view to be.
self.textViewContainer = [[CGLTextViewContainer alloc] initWithFrame:self.view.bounds height:200000];
// optionally configure the underlying text view how you'd like it.
self.textViewContainer.textView.typingAttributes = @{
NSFontAttributeName : [UIFont fontWithName:@"Menlo-BoldItalic" size:18.0]
};
self.textViewContainer.textView.textContainerInset = UIEdgeInsetsMake(40.0,
20.0,
40.0,
20.0);
// and: don't you dare alter the delegate of the text view container's underlying text view. Either use the pass-through delegate or, as here, subscribe to notifications, being sure to the text container's text view as the object.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextViewChanged:) name:UITextViewTextDidChangeNotification object:self.textViewContainer.textView];
CGLTextViewContainer通过CocoaPods提供。要安装它,只需将以下行添加到您的Podfile中
pod "CGLTextViewContainer"
Chris Ladd, [email protected]
CGLTextViewContainer在MIT许可协议下提供。有关更多信息,请参阅LICENSE文件。