我想在 TextView 中启用 ScrollView。当 TextView 高度增加时

Posted

技术标签:

【中文标题】我想在 TextView 中启用 ScrollView。当 TextView 高度增加时【英文标题】:I want to enable ScrollView in TextView.When TextView Height Is Increased 【发布时间】:2013-10-04 16:00:32 【问题描述】:

1)我想在TextView中启用ScrollView。当TextView高度增加时。

2) 当我按 Enter 后输入整行时,我正在输入的内容正在显示。但是当我在按 Enter 后仅输入 2 或 3 个字母时,我输入的内容没有显示。按 Enter 后显示

 -(void)viewDidLoad

  UITextView *txtView=[[UITextView alloc]init];
  txtView.frame = CGRectMake(60, 100, 350, 154);
  txtView.backgroundColor=[UIColor whiteColor];
  txtView.font=[UIFont fontWithName:Nil size:30];/*Font size*/
  txtView.delegate=self;
  txtView.scrollEnabled=YES;
  [self.view addSubview:txtView];
 
 -(void)textViewDidChange:(UITextView *)textView
  
 if(textView.contentSize.height>textView.frame.size.height)
 
 CGRect frame = txtView.frame;
 frame.size.height = textView.contentSize.height+90;
 textView.frame = frame;
 textView.scrollEnabled=YES;
 
 textView.scrollEnabled=YES;
 

【问题讨论】:

我不明白,为什么您要手动更改 textView 的 contentsize。 UITextView 自己处理所有事情,包括内容大小和滚动。除非您想将 contentsize 设置为超过其实际拥有的内容的高度,否则不应设置 contentsize 或 scrollEnabled。 另外,你给她做的是,改变textView框架的高度。如果您想拥有一个根据其内容更改高度的 TextView,请查看HPGrowingTextView txtView=[[UITextView alloc]init]; txtView.frame = CGRectMake(60, 100, 350, 154); txtView.backgroundColor=[UIColor whiteColor]; txtView.font=[UIFont fontWithName:Nil size:30]; 感谢您的回复。 我的问题是当我在达到第四行结束后在 TextView 中输入文本时,如果我按 enter 并输入文本它没有显示我正在输入的内容。按 enter 后显示文本。如果我到达第四行结束后没有按回车它会自动转到下一行并显示我正在输入的内容。在这种情况下没问题。 【参考方案1】:
if(textView.contentSize.height > textView.frame.size.height)

    CGRect frame = textView.frame;
    frame.size.height = textView.contentSize.height + 90;
    textView.frame = frame;
    textView.scrollEnabled = YES;

    [textView setContentSize:CGSizeMake(0, textView.frame.size.height + 1)];

【讨论】:

如果我尝试这样的滚动视图在 textView 中禁用并且我不想增加 textHeight。因为在 textView 下面我有一个提交按钮。 请清楚说明您希望从 textView 中获得什么行为。你想改变textView的框架高度,还是只启用滚动? 我需要 1 个东西。1) 用户输入文本时应显示文本。例如 textView 高度为 4 行。当用户到达第 4 行末尾时,它会自动来到下一行,它显示正在输入的内容。在我的情况下,我将在输入第 4 行字母后每行仅输入 1 个字母,然后按 Enter,然后光标转到下一行,我输入的内容未显示。按后显示文字 显示您创建的 UIButton 的代码。具体来说就是框架。 我认为你没有得到我的问题。下面是 textview 按钮。

以上是关于我想在 TextView 中启用 ScrollView。当 TextView 高度增加时的主要内容,如果未能解决你的问题,请参考以下文章

Android TextView 启用 LinkiFy 和 Href 标签

如何在 Android 的 TextView 中显示 Unicode 字符?

TextView 没有出现在画布上

ObjectAnimator 像素化 TextView

TabHost只能充满整个页面吗?我想在一个Activity中同时显示TabHost和其他控件。

在 TextView 中显示 App 版本