简体   繁体   English

UIScrollView中的UITextView不显示长文本

[英]UITextView inside UIScrollView does not display long text

I have a UITextView inside a UIScrollView. 我在UIScrollView中有一个UITextView。 if I try to display a long text with a certain font size, the text is not showing. 如果我尝试显示具有特定字体大小的长文本,则不会显示该文本。 With shorter text or decreasing the font size the text shows up again. 如果文本较短或字体减小,则文本会再次显示。 What am I doing wrong? 我究竟做错了什么?

Screenshot of the autolayout here (the item selected is the textview): 此处的自动布局的屏幕截图(所选项目为textview): 在此处输入图片说明

The UITextView is inherited from UIScrollView UITextView继承自UIScrollView

open class UITextView : UIScrollView, UITextInput, UIContentSizeCategoryAdjusting

So it is a bad practice to put ScrollView inside scrollView. 因此,将ScrollView放在scrollView中是一种不好的做法。 The reason why you don't see a long text in UITextView is that UITextView became scrollable. 在UITextView中看不到长文本的原因是UITextView可滚动。 Also you didn't specify your scrollView content width so the with is calculated is as one line of your long text. 同样,您也没有指定scrollView内容宽度,因此with的计算方式是长文本的一行。 But you can scroll it because you will scroll scrollView. 但是您可以滚动它,因为您将滚动scrollView。 Better way is to set label inside scrollview and set the label numberOfLines = 0. Make the label bottom padding to the bottom of your scrollView and top to some items in top of label and you will get a scrollable text. 更好的方法是将标签设置在scrollview中,并设置标签numberOfLines =0。使标签底部填充到scrollView的底部,顶部填充到标签顶部的某些项目,您将获得可滚动的文本。

I've just understood that you don't need a scrollView to place your long text. 我刚刚了解到,您不需要scrollView即可放置长文本。

You can use just a UITextView for your needs. 您可以只使用UITextView来满足您的需求。

  1. Remove your scrollView. 删除您的scrollView。
  2. Add a UITextView. 添加一个UITextView。
  3. Set constraints as described and math them with screenshot 按说明设置约束条件并使用屏幕截图对其进行数学处理
    • Top Space to ReadingTimeLabel ReadingTimeLabel的顶部空间
    • Align Bottom to Superview / Bottom Layout Guide 使底部与Superview对齐/底部布局指南
    • Leading space to Superview 通往Superview的领先空间
    • Trailing space to Superview 尾随空间到Superview

UITextViewConstraints

And you will get the result like on the screenshots 您将获得如屏幕截图所示的结果

Scroller top: 滚动条顶部:

在此处输入图片说明

Scrolled bottom: 滚动底部:

在此处输入图片说明

Note: You can disable UITextView editing in attribute inspector. 注意:您可以在属性检查器中禁用UITextView编辑。

But if you for some reason need a scroll view: Set constrains as described, also match them as shown on screenshots. 但是,如果由于某种原因需要滚动视图:按照说明设置约束,也请按照屏幕快照所示进行匹配。

  1. Add scrollView constraints (Superview is the view where the scrollView is placed in): -Top space to ReadingTimeLabel -Align bottom to Superview/Bottom Layout Guide -Align leading to Superview -Align trailing to Superview 添加scrollView约束(Superview是scrollView所在的视图):-顶部空间到ReadingTimeLabel-对齐底部到Superview /底部布局指南-对齐导致Superview-对齐尾部到Superview

  2. Please a UIView inside scrollView. 请在UIView里面滚动。 Use it as container. 用作容器。

  3. Add constraints for container. 为容器添加约束。 (Superview is scrollView) -Align Leading to Superview -Align Trailing to Superview -Top Space to SuperView -Bottom Space to SuperView -Equal Width to SuperView (Superview是scrollView)-Align导致Superview -Align尾随Superview-顶部空间SuperView-底部空间SuperView -Equal

  4. Add a UILabel inside container. 在容器内添加一个UILabel。 (Superview is container). (Superview是容器)。

  5. Add constraints for UILabel. 为UILabel添加约束。 -Align Leading to Superview -Align Trailing to Superview -Top Space to SuperView -Bottom Space to SuperView -Align导致Superview -Align尾随Superview-顶部空间SuperView-底部空间SuperView

  6. For the label set Lines = 0 in Attribute inspector or use the code 对于属性检查器中的标签集Lines = 0或使用代码

label.numberOfLines = 0 label.numberOfLines = 0

scrollView约束

容器约束

标签约束

And you will get the result like on screenshots 您将获得如屏幕截图所示的结果

Scrolled top 滚动顶部

结果1

Scroller bottom 滚动条底部

结果2

试试下面的代码:

textView.sizeToFit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM