iOS - increase UITextField height based on its content - ios

I've a collection view that has on its header some views as user inputs.
In one of them I've something like a Biography textField, but when I'm typing the text continues all the way to the left, and what I'm trying to achieve is that when the text reaches the visible end, it goes to a new like and so it increases its own height.
How can I achieve this?
I want the Bio text field to increase its height based on its text and not stay the same height and the text goes all the way long to the left.
Regards ;)

UITextField is one-line only element. You can try UITextView instead
EDITED
I've never done this, but this thread seems to do something like dynamic size with UITextView

Related

Horizontally scroll single line UITextView?

I need a view that allows me to have an editable method of text entry that is limited to one line, but can scroll horizontally. After a bit of research, I found that maybe a UITextView would work well.
This is going in a table view cell so I've tried putting this in awakeFromNib()
code.textContainer.maximumNumberOfLines = 1
code.layoutManager.textContainerChangedGeometry(code.textContainer)
but this is what happens (I'm repeating the same line to fill the text view)
So for attributes I need to assign, I need:
Disable Vertical scrolling
Enable Horizontal scrolling
Limit Text View to one line
If anyone also knows a better way to do this (maybe with a scrollable Text Field?) I would love to hear it.
Thanks in advance!

iOS UITextView content offset and bounds origion change after changing text programmatically

I have a UITextView. The text in the view keeps changing based on user action. On many occasions, the text is too large to fit and so my text view (which has a desired fixed width and height) scrolls the text. The control flow is something like this
1) user takes some action, and text view appears with some text (possibly more than what can fit, so comes with a scroll)
2) user dismisses the view (I simply hide the text view)
3) user takes another action, and text view appears with new text (i use UITextView setText to reassign the text, and simply turn the hidden property to NO)
My text cannot be edited by the user. It is always reset to a different string in code
Problem:
When the textview is redrawn/rendered with the new text, on some occassions the contentoffset.y AND bounds.origin.y values of the text view are set to something besides 0, which is not desirable. It is intermittent, but creates a major UI issue. At least I think those new values are what causes the UI to look so weird. Open to other suggestions here...
I want to know why this happens, and how to fix this? Since the text is changed programatically, I can't depend on textViewDidChange: to reset these values.
EDIT: This is how I am changingt he text everytime something new has to be placed in the textview
[textView1 setText:someNSString];
where someNSString is, well, some string with text

Making some parts of a `UITextField` un-selectable

I have a UITextField that will represent an integer number with a fraction (numerator and denominator). As an example: "27 3/16". I want to make the denominator "/16" at the end both un-editable and also un-selectable.
I can use the delegate method textField:shouldChangeCharactersInRange:replacementString: to prevent the "/16?" from being edited with an approach a bit like this.
Is there some way that I can also prevent the "/16" from being selectable at all? So, the caret can't be moved in to it, and the selection marque can't be made around it.
If this isn't possible, is there a hook so that once the user finishes placing their selection, I can update the selection and move the caret to just before this piece of the text.
Thanks.
You can have a UILabel and UITextField constrained via autolayout next to each other, the UILabel containing the denumerator and the UITextField the numerator.
Here is an answered question for how to do that: Using Auto Layout to have UILabel and UITextField next to each other (the essence is, you need to adjust the content hugging priority of the UITextField to make it always as wide as the contained text.
If your text field gets too small to be tapped, you can apply the code of this answer to a UITextView and make the tappable area of your view bigger: UIButton: Making the hit area larger than the default hit area (but better use method swizzling than overriding a method in a category like in the answer! Or a subclass.)

How do I have padding at the bottom of my UITextView like in Mail.app so the text isn't scrunched?

In Mail.app, if I go to create a new message and type all the way so the text is at the last line of the UITextView there is still a decent amount of space between the last line of text and the keyboard if you scroll down. But you can't type here. It's just nice padding so you can read it without it being scrunched against the keyboard, and if you select the text, the selection handles don't extend under the keyboard.
My question is, how do I mimic this with a traditional UITextView?
I think you can do this by setting the contentInset property of your UITextView:
myTextView.contentInset = UIEdgeInsetsMake(-4,-8,0,0);
btw... the values above are not specific to your situation. You will want to adjust these to get the inset you are looking for.

Resizing UITextView with UIView below

I can't understand how create something like this:
The idea is the UITextView is resizing. But how to resize it when user enter 100 rows? I need to check /n and then increment frame by lineHeight and frame of cell?
Suggest pls.
No need. The text view should be resized to fit the keyboard. After that it scrolls the text if there is more text than fits into the visible part.
The path of least resistance to getting what you're describing to work is to create a method that updates the frames of the UITextView and UIView when the keyboard goes up or down. You know when the keyboard goes up and down based on UIKeyboardWillShowNotification and UIKeyboardWillDisappearNotification. The notification contains a userInfo dictionary that specifies the y-coordinate of the keyboard with respect to your view controller's window. All you need to do is recalculate your frames subtracting the height of the keyboard (either something like 216 or 0). If you still have questions about this let me know, and I'd be happy to explain further.

Resources