当行数设置为 0 时 UITextView 内容消失
UITextView content disappears when number of lines set to 0
我在对长文本使用 UITextView
时遇到了一个奇怪的行为。特别是将其textContainer
的maximumNumberOfLines
设置为0时,内容完全消失
我创建了一个小型测试项目,以确保我的代码中没有任何异常导致它,您可以在屏幕截图中看到它。在我的项目中,我有一个 UIViewController
,其中包含一个 UIScrollView
,其中包含一个垂直 UIStackView
。堆栈视图包含一个 UITextView
(屏幕截图中的红色标题),另一个包含标签、文本视图、按钮和其他文本视图的堆栈视图。
单击按钮后,我将 maximumNumberOfLines
设置为 0(之前为 2),内容就消失了。我试过使用和不使用动画,结果都是一样的。
消失好像和最终文字的高度有关,好像我用小一点的字体,设置多了文字内容才会消失
知道为什么会发生这种情况吗?
为了完整起见,我使用 Xamarin.iOS 和 here
是我的 ViewController.
内容消失,因为您的文本太大,UIView
对象无法显示。根据这个 post 我们知道,实际上 UIView
的最大高度和宽度受它们消耗的内存量限制。
在我的测试中,如果我们不为 textView
设置太多字符(删除一些 textView.Text += textView.Text;
),内容将会显示。我还在 UILabel
上测试过它,它也是如此。因为他们都是继承自UIView
.
如果确实要显示这么多字符串,请尝试启用 textView 的 ScrollEnabled
。不要让 textView 的 Bounds 超过最大限制。当你想扩展textView时,你可以尝试添加高度和宽度约束:
var textViewContraints = new NSLayoutConstraint[]
{
NSLayoutConstraint.Create(textView, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1f, 700),
NSLayoutConstraint.Create(textView, NSLayoutAttribute.Width, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1f, 500)
};
UIView.AnimateNotify(1.2d, () =>
{
if (!buttonExpanded)
{
textView.ScrollEnabled = true;
textView.TextContainer.MaximumNumberOfLines = 0;
textView.TextContainer.LineBreakMode = UILineBreakMode.WordWrap;
textView.SizeToFit();
textView.AddConstraints(textViewContraints);
expandButton.Transform = CGAffineTransform.MakeRotation((nfloat)(Math.PI / 2.0f));
textView.Text = "r" + textStr;
textView.Text = textView.Text.Substring(1);
}
else
{
textView.ScrollEnabled = false;
foreach (NSLayoutConstraint constraint in textView.Constraints)
{
textView.RemoveConstraint(constraint);
}
textView.TextContainer.MaximumNumberOfLines = 3;
textView.TextContainer.LineBreakMode = UILineBreakMode.WordWrap;
expandButton.Transform = CGAffineTransform.MakeRotation(0f);
textView.Text = textStr;
}
scrollView.LayoutIfNeeded();
buttonExpanded = !buttonExpanded;
}, null);
我在对长文本使用 UITextView
时遇到了一个奇怪的行为。特别是将其textContainer
的maximumNumberOfLines
设置为0时,内容完全消失
我创建了一个小型测试项目,以确保我的代码中没有任何异常导致它,您可以在屏幕截图中看到它。在我的项目中,我有一个 UIViewController
,其中包含一个 UIScrollView
,其中包含一个垂直 UIStackView
。堆栈视图包含一个 UITextView
(屏幕截图中的红色标题),另一个包含标签、文本视图、按钮和其他文本视图的堆栈视图。
单击按钮后,我将 maximumNumberOfLines
设置为 0(之前为 2),内容就消失了。我试过使用和不使用动画,结果都是一样的。
消失好像和最终文字的高度有关,好像我用小一点的字体,设置多了文字内容才会消失
知道为什么会发生这种情况吗?
为了完整起见,我使用 Xamarin.iOS 和 here 是我的 ViewController.
内容消失,因为您的文本太大,UIView
对象无法显示。根据这个 post 我们知道,实际上 UIView
的最大高度和宽度受它们消耗的内存量限制。
在我的测试中,如果我们不为 textView
设置太多字符(删除一些 textView.Text += textView.Text;
),内容将会显示。我还在 UILabel
上测试过它,它也是如此。因为他们都是继承自UIView
.
如果确实要显示这么多字符串,请尝试启用 textView 的 ScrollEnabled
。不要让 textView 的 Bounds 超过最大限制。当你想扩展textView时,你可以尝试添加高度和宽度约束:
var textViewContraints = new NSLayoutConstraint[]
{
NSLayoutConstraint.Create(textView, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1f, 700),
NSLayoutConstraint.Create(textView, NSLayoutAttribute.Width, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1f, 500)
};
UIView.AnimateNotify(1.2d, () =>
{
if (!buttonExpanded)
{
textView.ScrollEnabled = true;
textView.TextContainer.MaximumNumberOfLines = 0;
textView.TextContainer.LineBreakMode = UILineBreakMode.WordWrap;
textView.SizeToFit();
textView.AddConstraints(textViewContraints);
expandButton.Transform = CGAffineTransform.MakeRotation((nfloat)(Math.PI / 2.0f));
textView.Text = "r" + textStr;
textView.Text = textView.Text.Substring(1);
}
else
{
textView.ScrollEnabled = false;
foreach (NSLayoutConstraint constraint in textView.Constraints)
{
textView.RemoveConstraint(constraint);
}
textView.TextContainer.MaximumNumberOfLines = 3;
textView.TextContainer.LineBreakMode = UILineBreakMode.WordWrap;
expandButton.Transform = CGAffineTransform.MakeRotation(0f);
textView.Text = textStr;
}
scrollView.LayoutIfNeeded();
buttonExpanded = !buttonExpanded;
}, null);