Вот простой пример, чтобы попробовать.Нажатие кнопки «Изменить текст» переключает между коротким, средним и длинным объемом текста в текстовом представлении, давая такой результат:
Вот весь необходимый код (все, что он делает, это изменяет текст - больше ничего):
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" width="375" height="667"/>
<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" width="295" height="567"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="oNQ-i1-b49" userLabel="Container">
<rect key="frame" x="0.0" y="0.0" width="295" height="675.5"/>
<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" width="295" height="240"/>
<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" width="192" height="26"/>
<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" width="153.5" height="20.5"/>
<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" width="255" height="33"/>
<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" width="255" height="300"/>
<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>