how to implement the autoresizing UITextView vertically? - ios

First look at the screenshot:
second press the return button, the screenshot for your reference:
after four times click
return
button, the height of text field will not be higher any more.
now here is the question: how can I implement the function of textfield? and what about the background Image behind the text Field , should auto resize too? if there are any solution, let me know! thanks very much----

Make it UITextView not a text field, track the text changes at shouldChangeTextInRange of the textView delegate.
In this method calculate the number of lines (textView.contentSize.height/textView.font.lineHeight), compare it with the previous value to decide whether you need to proceed or just return. If the number of lines is changed, check whether the textView is already displaying the maximum number of lines (that must be 3 on the image) and based on the results you will change the textView, it's superview and a button frames (you can also make it animated easily) and probably scroll the text to the change region with scrollRangeToVisible.

Related

iOS - increase UITextField height based on its content

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

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.)

UITextview not getting completely filled with the text in IPhone

In my app, I have a UITextView. When I key in data in the UITextView, the words are not getting completely filled in the UITextview. There is a boundary within the UITextView and the words scroll up as I key in the words. Is there a setting which I am missing in UITextView. Please let me know. Please find the screenshot below
You can set a frame and a contentSize as it inherits from the UIScrollView to make it bigger. I do not have access to IDE right now but something like this should work:
myTextView.frame = CGRectMake(10,10,300,300);//example values
myTextView.contentSize = CGSizeMake(300,300);//example values
but sooner or later you want be able to see the whole text on the screen and you will need to scroll anyway.
You can also change this values while user is typing by using some delegate methods

Adding background image to the selected in an UITextView

I would like to add a background image for the selected text in the UITextView, not for the whole view, as I marked in the below picture.
How can I add a background image for the selected text?
Or, how can I add one small image before and another one after the
selected without making the other text move back or forward?
UITextInput protocol should provide everything you need.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInput_Protocol/Reference/Reference.html
Specifically:
selectedTextRange
firstRectForRange
firstRectForRange: which will tell you the rectangle covering a range of characters. If the range of characters spans multiple lines, you have to call it repeatedly. The first time it will give you the first rect and tell you what range that covers. Then, you adjust the range you're asking about to be the remaining characters you're interested in and repeat.

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