I have a view in my storyboard with some items, 1 of which is a UITextView. Whenever I enable scrolling (either by inspector or via code), the UITextView disappears/becomes invisible/whatever. Is there a way to make this not happen?
Thanks in advance
The height of UITextView is probably not set. When scrolling is disabled, the UITextView's size is calculated based on it's content (because the content has to fit, because if it doesn't, user can't scroll to see it). On the other hand, when the scrolling is enabled, the system presumes, that even if the content doesn't fit, user can scoll down to see it all. And since the UITextView height is not set by you, and neither it is set "by the content", the UITextView height is 0.
Related
I have been going through apple's documentation and answers on SO and can't get my UITextView within a UIScrollView to expand vertically dynamically as text increases. Apple seems to say just set a low content hugging priority and that should be all you need. But not working for me. Thanks in advance for any suggestions.
I have pinned UITextview left and right and top. I also have a view below it pinned to its bottom. There is no height constraint. Thanks in advance for any suggestions. The textview I am trying to make behave in this way is BlockView. It is pinned above to contact image and below to notesview.
Make sure you set the text view's scrollEnabled property to false. By default it is enabled, which renders your text view with the height set in XIB (even if it doesn't have any height constraint).
I currently have an app which displays questions and answers as text in textview objects. These textviews are wrapped in stackviews along with navigation buttons.
If I set the textview to non-scrollable (by unchecking the scrollable button) the text displays, but long text runs off the screen and of course the user can't scroll down to read it. If I check the scrollable button, the text isn't displayed at all - just blank space.
Any idea how I can either: set the text to be scrollable and display or set the text to autoshrink (as it doesnt run over by much).
That much to say about textViews -> Replace the textViews with labels with 0 of lines -> This guarantees that the label will be autoshrinking vertically, just set width and the stackView into scrollView and you are at it :)
I've successfully created UITableView with custom cells (automatically sized) that contain a label and UITextView - detailsTextView.
When scrolling of detailsTextView is disabled, the cells are properly resized according to textView's text.
I don't want, however, to have extremely large cells and set maximum height of a cell (and enable scrolling for detailsTextView when it reaches max cell height).
How can I achieve this?
When enabling scrolling for detailsTextView autoresizing of cells shrinks it to 0 height (overriden by min. height constraint in IB), but still it does not fill my planned maximum size of cell.
override func viewDidLoad() {
mainTableView.rowHeight = UITableViewAutomaticDimension
mainTableView.estimatedRowHeight = 200
self.mainTableView.register(UINib.init(nibName: EventCell.nibName,
bundle: Bundle.main),
forCellReuseIdentifier: EventCell.cellIdentifier)
...
}
The idea is simple. You need to keep track of UITextView's height change in textViewDidChange method.
Then if UITextView exceeds your maximum predefined height then you are gonna add a constraint which restrict the UITextView's height growth. Constraint should be something like setting minimum height of UITextView to your maximum predefined height and enable the scroll for UITextView.
If you use only auto layout, UITextView can define it's own size only if scrolling is disabled, unfortunately.
You should try to avoid recursive layout passes (e.g. when text view's height changes, then install more constraints, this will cause the height to change again, etc, etc).
What you can do is limit maximumNumberOfLines of your textView, and provide other ways to see the full text: as it was pointed out, it is a very bad UX practice to have a scroll view within another scroll view.
From a UI/UX standpoint I wouldn't do that what you are trying to do.
Having a Scrollview (UITableview) with another scrollview in it (UITextview) is bad. It can confuse the user because he could scroll the "textbox" on smaller devices instead of the "table".
However, you could go with this solution: UITextView change height instead of scroll
Another solution would be to check what happens when the cell with 0 height is shown. Check viewWillLayoutSubviews() and the constrains in IB.
Hope this gets you one step closer to the solution/answer.
I have a scrollView which contains textView. If text is long and does not fit on a screen, I would like to increase textView height (which I think I could do by adding NSLayoutConstraint outlet and modifying it, correct me if I am wrong) so that view would be scrollable (or non scrollable) depending on text length.
Pretty much, like I can set Scrolling Enabled for textView, except I wan to scroll whole view because of images and labels I have on that view.
EDIT. Basically - how to guess textView height based on string, in order to have proper size textView
To make your scrollview content scrollable you need to set the size of your UIScrollView content ( your textview ), e.g. use:
scrollView.contentSize = CGSizeMake(textview.frame.size.width, textview.frame.size.height)
I'm fairly new to iOS development and am having a hard time sizing a UITextView to it's content. I've found several examples of calculating a strings render size and/or updating the frame size of a textview, however I can't seem to get those to work.
When I add a UITextView to my view it adds a Height constraint which I think is locking it to a specific height. How can I work around it, update the constraint via code, and/or get my UITextView to resize vertically to show my content?
You can create an outlet to the vertical size constraint, and then adjust the .constant property of the constraint to amend the height of your text view.
A UITextView inherits from UIScrollView. As part of it's logic, it changes the height of its contentSize property to allow the user to scroll vertically to any portion of the TextView's content.
I would recommend using the ContentSize property of your UITextView to figure out how "tall" your content really is. You can then set the frame of your UITextView accordingly.
OR: You could figure out the size of your text using the right method from the NSString+UIKitAdditions category.
If you don't want autolayout then disable it in the xib file.
If you want to change the contraint and it's value this tutorial will help you to deal with autolayout.