View 中有一个 UITextView, View Controller 以 Modal 样式显示。在我的 viewDidLoad 方法中,我设置了这个 UITextView 的文本,但是文本没有显示。下图显示错误。 文本颜色为黑色。
奇怪的是,当我在 TextView 中长按或在键盘中点击 [return] 时,文本变得可见。我注意到的一件事是,只有当我设置的文本长于 UITextView 框架宽度,并且最后一个单词没有被打断(例如长 url)时,才会发生此错误。
我认为问题是可能自动换行无法正常工作。
提前致谢。
代码如下:
UITextView *myTextView = [[UITextView alloc]initWithFrame:CGRectMake(10, 10, 520, 220)];
myTextView.textColor = [UIColor blackColor];
myTextView.font = [UIFont systemFontOfSize:20];
myTextView.layer.cornerRadius = 5;
myTextView.delegate = self;
myTextView.text = @"long text should some like http://stackoverflow.com/posts/11200726/edit";
[self.view addSubview:myTextView];
[myTextView release];
已解决。在 viewDidLoad 方法中,添加如下代码:
CGRect tempFrame = myTextView.frame;
[myTextView setFrame:CGRectZero];
[myTextView setFrame:tempFrame];
最佳答案
将 UITextView 子类化为您自己的类,比方说 MyUITextView:UITextView。
在屏幕外用
初始化它[[MyUITextView alloc] initWithFrame:CGRectZero]
在 MyUITextView 中覆盖方法
-(void)willMoveToWindow:(UIWindow *)newWindow
并在其中将 self.frame 设置为适当的值。
这对我有用!
关于ios - UITextView 中的文本显示不正确,不可见,但占用空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11200726/