Auto-Resizing UITextView in UIScrollView

uitextview inside uiscrollview
resize uitextview height to fit text
uitextview not scrolling
uiscrollview autolayout
uitextview scroll to bottom
uitextview auto scroll to last line
uitextview accessibility
uitextview multiline

I'm setting up a scroll view that displays some content. I'm trying to figure out how to change the scroll view size according to the amount of text within the text view (and of course change the text view height, but keep the width the same). I am using a Storyboard for the scroll view. Inside the view controller is the scroll view, which contains a container view (this is where the text view is kept). The text view I am talking about is under where it says content description. I have tried a bunch of different approaches but haven't found one that works. Any help would be extremely appreciated. Thanks! (it's also worth noting that I am presenting this view controller using the DeckTransition library)

My Xcode storyboard https://imgur.com/OFrKRSk How it looks so far in the simulator https://imgur.com/iq73Jn0

If you set scrollEnabled to false, text view will fit its content. If it does not work, you should check your constraints. e.g. content hugging priority, compression resistance priority. To make sure the text view to shrink smaller than the intrinsic content size.

Auto resize UITextView in UIScrollView, Without auto layout it is much easier. Just get the contentSize property of the UITextView: float textViewHeight = textView.contentSize.height; And then set scroll  Autoresizing UITextView February 28, 2017 By Jeff Johnson, Developer of Underpass. My recent blog posts have been more business-y than programmer-y, so today we're going to bring back that codin' feelin'. With a twist, since I'm going to talk about iOS, which is blasphemy in these parts, but don't shout.

Here is a basic example to try. Tapping the "Change Text" button will switch between short, medium and long amounts of text in the text view, giving this result:

Here is all the code needed (all it does is change the text - nothing else):

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]
    }

}

And here's the Storyboard:

<?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>

Resizing a UITextView in UIScrollView using Auto Layout issue, Resizing a UITextView in UIScrollView using Auto Layout issue - ios. I don't want the TextView to be scrollable in itself but I want all the subviews to move as​  The Android Platform Linux Kernel and Device Drivers. autoSizeTextType of uniform enables auto-resizing. left ├── text │ ╰── Sets the width of gtk_text_window_left or gtk_text_window_right, or the insert_at_cursor signal is a keybinding signal which gets emitted when the user initiates the insertion of a fixed string at the cursor.

Okay, so it turns out that I had some constraints that were not allowing the text view to resize properly. I disabled the scrolling on the text view, removed a height constraint that was on the scroll view and the content view, and now everything seems to be working. I appreciate all the advice given! You guys are super helpful!

iOS, For this tutorial, I will use the UITextView which is tricky when used with UIScrollView. So let's add a UITextView under the new UIView. Resize  Today's video will teach you how to implement a very easy trick to get your UITextView components to automatically size itself when the text changes. Its reliable in most situations and isn't too

How to resize a textView to fit its content (re-sizing textview based on , There was a straight-forward answer on stackoverflow, which basically uses “​sizeThatFits” method. This method asks the UIView to calculate a  This method is called after inserting the typed character. Determining the height is than as easy as: textView.contentSize.height (UITextView extends UIScrollView) Determining the height of, for example, one line is done by a hidden UITextView, this is needed for the properties of minNumberOfLines and maxNumberOfLines. Removing the bottom margin

How to resize a UITextView dynamically with Auto Layout, Resizing a UITextView (or a UIScrollView) dynamically on the fly isn't easy – or so it seems at first. Should we move or squish the UIView? UITextView Change Height dynamically UITextView Change Height dynamically dynamically expand the height of UITextView. - (void) textViewDidEndEditing:(UITextView *)textView { CGRect frame = mTextView.frame; frame.size.height

How to use Auto Layout with UIScrollView for iOS, How to use Auto Layout with UIScrollView for iOS be used as a parent object to other UIKit items such as UIView and UITextView. Manually resize the View element with your cursor so that it fills the width of the screen. Summary: public This diff adds support for auto-resizing multiline text fields. This has been a long-requested feature, with several native solutions having been proposed (see #1229 and D2846915). Rather than making this a feature of the native component, this diff simply exposes some extra information in the `onChange` event that makes it easy

Comments
  • 0 Refer to the link below:-- Setting Dynamic Height to UIScrollView Swift developer.apple.com/library/archive/documentation/…
  • @MayankWadhwa that link does not help me. I tried doing what they said. Could you please explain further?
  • If you disable scrolling on your text view, it will auto-size itself just like a label with number of lines set to zero.
  • @DonMag when I disable the scrolling it does not resize itself. It just stays the same height as before but now the text is cut off
  • Do you have a height constraint set on it? If so, delete that.