ios - UITextView 中的文本显示不正确,不可见,但占用空间

标签 ios uikit uitextview word-wrap

View 中有一个 UITextView, View Controller 以 Modal 样式显示。在我的 viewDidLoad 方法中,我设置了这个 UITextView 的文本,但是文本没有显示。下图显示错误。 文本颜色为黑色

奇怪的是,当我在 TextView 中长按或在键盘中点击 [return] 时,文本变得可见。我注意到的一件事是,只有当我设置的文本长于 UITextView 框架宽度,并且最后一个单词没有被打断(例如长 url)时,才会发生此错误

我认为问题是可能自动换行无法正常工作

提前致谢。

enter image description here

代码如下:

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/

相关文章:

ios - 在 iOS 上执行代码,使用背景音频

ios - 估计信标无法按预期在 iPhone 上工作

ios - Swift - 从 AppDelegate 添加 subview 到 View Controller

iphone - DetailViewController 中的 subview ?

ios - iOS 9 中的 UITextView 高度错误

swift - UITextView 使用 swift 突出显示所有匹配项

ios - 使用 UICollectionViews 创建动态和多特征

ios - 通过phonegap获取ios设备真实udid

swift - iOS 使用 NSAttributedString 时更改 uilabel 链接的颜色

iphone - 在 iOS 中,如何将图像嵌入到文本中(在 UITextView 中)?