在 UIScrollView 中自动调整 UITextView 的大小
Posted
技术标签:
【中文标题】在 UIScrollView 中自动调整 UITextView 的大小【英文标题】:Auto-Resizing UITextView in UIScrollView 【发布时间】:2019-06-25 05:35:42 【问题描述】:我正在设置一个显示一些内容的滚动视图。我试图弄清楚如何根据文本视图中的文本量来改变滚动视图的大小(当然改变文本视图的高度,但保持宽度不变)。我正在使用 Storyboard 作为滚动视图。视图控制器内部是滚动视图,它包含一个容器视图(这是保存文本视图的地方)。我正在谈论的文本视图位于内容描述的下方。我尝试了很多不同的方法,但没有找到一种有效的方法。任何帮助将不胜感激。谢谢! (同样值得注意的是,我正在使用 DeckTransition 库展示这个视图控制器)
我的 Xcode 故事板https://imgur.com/OFrKRSk 到目前为止在模拟器中的样子https://imgur.com/iq73Jn0
【问题讨论】:
0 参考以下链接:-- 设置动态高度为 UIScrollView Swift developer.apple.com/library/archive/documentation/… @MayankWadhwa 该链接对我没有帮助。我试着按照他们说的去做。能否进一步解释一下? 如果您在文本视图上禁用滚动,它将自动调整大小,就像行数设置为零的标签一样。 @DonMag 当我禁用滚动时,它不会自行调整大小。它只是保持和以前一样的高度,但现在文本被切断了 你设置了高度限制吗?如果是这样,请删除它。 【参考方案1】:如果您将scrollEnabled
设置为false,文本视图将适合其内容。如果它不起作用,您应该检查您的约束。例如内容拥抱优先,抗压优先。确保文本视图缩小到小于固有内容大小。
【讨论】:
【参考方案2】:这是一个基本的例子。点击“更改文本”按钮将在文本视图中的短文本、中文本和长文本之间切换,结果如下:
这是所需的所有代码(它所做的只是更改文本 - 没有别的):
import UIKit
class TextViewinScrollViewController: UIViewController
@IBOutlet var theTextView: UITextView!
var i = 0
var strings = [
"This is a Short amount of text.",
"This is a Medium amount of text. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.",
"This is a Long amount of text. Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.",
]
override func viewDidLoad()
super.viewDidLoad()
theTextView.text = strings[i]
@IBAction func didTap(_ sender: Any)
i += 1
if i >= strings.count
i = 0
theTextView.text = strings[i]
这是故事板:
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="ios.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="HAr-gQ-Ow4">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Text Viewin Scroll View Controller-->
<scene sceneID="0zs-mA-Zcw">
<objects>
<viewController id="HAr-gQ-Ow4" customClass="TextViewinScrollViewController" customModule="XC10SWScratch" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="oNR-og-qiK">
<rect key="frame" x="0.0" y="0.0" />
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="XBn-Je-V51">
<rect key="frame" x="40" y="60" />
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="oNQ-i1-b49" userLabel="Container">
<rect key="frame" x="0.0" y="0.0" />
<subviews>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="VAf-T0-cKT">
<rect key="frame" x="0.0" y="0.0" />
<color key="backgroundColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="height" constant="240" id="NzL-9L-Ybs"/>
</constraints>
</imageView>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="K3u-yi-BbX">
<rect key="frame" x="51.5" y="252" />
<color key="backgroundColor" red="0.92143100499999997" green="0.92145264149999995" blue="0.92144101860000005" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<inset key="contentEdgeInsets" minX="10" minY="4" maxX="10" maxY="4"/>
<state key="normal" title="Change Text in Text View"/>
<connections>
<action selector="didTap:" destination="HAr-gQ-Ow4" eventType="touchUpInside" id="dnw-ff-qw3"/>
</connections>
</button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Content Description" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="gXD-0q-Avm">
<rect key="frame" x="8" y="290" />
<color key="backgroundColor" red="0.92143100499999997" green="0.92145264149999995" blue="0.92144101860000005" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" scrollEnabled="NO" editable="NO" text="This is the Text View" textAlignment="natural" translatesAutoresizingMaskIntoConstraints="NO" id="Mwg-70-9L2">
<rect key="frame" x="20" y="322.5" />
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
</textView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="1ox-xZ-Teo" userLabel="Example View">
<rect key="frame" x="20" y="367.5" />
<color key="backgroundColor" red="0.92143100499999997" green="0.92145264149999995" blue="0.92144101860000005" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="height" constant="300" id="hXk-ck-CvF"/>
</constraints>
<string key="text">Example View
(300-pts Height)</string>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" red="1" green="0.83234566450000003" blue="0.47320586440000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="gXD-0q-Avm" firstAttribute="top" secondItem="K3u-yi-BbX" secondAttribute="bottom" constant="12" id="7gX-pC-zmh"/>
<constraint firstItem="Mwg-70-9L2" firstAttribute="width" secondItem="oNQ-i1-b49" secondAttribute="width" constant="-40" id="9pZ-ja-g7R"/>
<constraint firstItem="Mwg-70-9L2" firstAttribute="centerX" secondItem="oNQ-i1-b49" secondAttribute="centerX" id="Byo-5N-WXf"/>
<constraint firstAttribute="trailing" secondItem="VAf-T0-cKT" secondAttribute="trailing" id="FyF-by-edM"/>
<constraint firstItem="1ox-xZ-Teo" firstAttribute="width" secondItem="Mwg-70-9L2" secondAttribute="width" id="Lic-3Q-vWZ"/>
<constraint firstAttribute="bottom" secondItem="1ox-xZ-Teo" secondAttribute="bottom" constant="8" id="MQK-MS-dCA"/>
<constraint firstItem="K3u-yi-BbX" firstAttribute="centerX" secondItem="oNQ-i1-b49" secondAttribute="centerX" id="UPN-FD-gxb"/>
<constraint firstItem="K3u-yi-BbX" firstAttribute="top" secondItem="VAf-T0-cKT" secondAttribute="bottom" constant="12" id="WMi-Vl-xBu"/>
<constraint firstItem="1ox-xZ-Teo" firstAttribute="centerX" secondItem="oNQ-i1-b49" secondAttribute="centerX" id="dno-nR-zvQ"/>
<constraint firstItem="Mwg-70-9L2" firstAttribute="top" secondItem="gXD-0q-Avm" secondAttribute="bottom" constant="12" id="fDe-GP-gs4"/>
<constraint firstItem="VAf-T0-cKT" firstAttribute="top" secondItem="oNQ-i1-b49" secondAttribute="top" id="hRj-XY-fnb"/>
<constraint firstItem="gXD-0q-Avm" firstAttribute="leading" secondItem="oNQ-i1-b49" secondAttribute="leading" constant="8" id="ks5-IH-yrt"/>
<constraint firstItem="1ox-xZ-Teo" firstAttribute="top" secondItem="Mwg-70-9L2" secondAttribute="bottom" constant="12" id="lYY-L9-pb6"/>
<constraint firstItem="VAf-T0-cKT" firstAttribute="leading" secondItem="oNQ-i1-b49" secondAttribute="leading" id="urO-i9-GQ7"/>
</constraints>
</view>
</subviews>
<color key="backgroundColor" red="0.46202266219999999" green="0.83828371759999998" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="oNQ-i1-b49" firstAttribute="top" secondItem="XBn-Je-V51" secondAttribute="top" id="FGL-wN-OHg"/>
<constraint firstItem="oNQ-i1-b49" firstAttribute="leading" secondItem="XBn-Je-V51" secondAttribute="leading" id="f4T-Mh-ZdB"/>
<constraint firstAttribute="bottom" secondItem="oNQ-i1-b49" secondAttribute="bottom" id="qwX-hf-UtO"/>
<constraint firstAttribute="trailing" secondItem="oNQ-i1-b49" secondAttribute="trailing" id="tdW-x1-DtX"/>
<constraint firstItem="oNQ-i1-b49" firstAttribute="width" secondItem="XBn-Je-V51" secondAttribute="width" id="urs-jZ-3Hi"/>
</constraints>
</scrollView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstItem="VsF-QR-ZgP" firstAttribute="trailing" secondItem="XBn-Je-V51" secondAttribute="trailing" constant="40" id="bww-Xy-uZ6"/>
<constraint firstItem="XBn-Je-V51" firstAttribute="top" secondItem="VsF-QR-ZgP" secondAttribute="top" constant="40" id="dP4-Ax-cgr"/>
<constraint firstItem="XBn-Je-V51" firstAttribute="leading" secondItem="VsF-QR-ZgP" secondAttribute="leading" constant="40" id="eof-fi-9Ml"/>
<constraint firstItem="VsF-QR-ZgP" firstAttribute="bottom" secondItem="XBn-Je-V51" secondAttribute="bottom" constant="40" id="euP-M7-rBT"/>
</constraints>
<viewLayoutGuide key="safeArea" id="VsF-QR-ZgP"/>
</view>
<connections>
<outlet property="theTextView" destination="Mwg-70-9L2" id="IWV-hn-TRM"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="q25-D5-Cge" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-114.40000000000001" y="58.920539730134934"/>
</scene>
</scenes>
</document>
【讨论】:
【参考方案3】:好的,事实证明我有一些约束不允许文本视图正确调整大小。我禁用了文本视图的滚动,删除了滚动视图和内容视图的高度约束,现在一切似乎都正常了。我感谢所有的建议!你们太有帮助了!
【讨论】:
以上是关于在 UIScrollView 中自动调整 UITextView 的大小的主要内容,如果未能解决你的问题,请参考以下文章
使用自动布局问题在 UIScrollView 中调整 UITextView 的大小
将 UIImageView 动态添加到 UIScrollview 中,但它不能自动调整大小