Using UITextView - ios

I am trying to create a textview that holds a 3 line string. As the user works, the string will update and become wider, the height (number of lines) will remain the same. Reading through the documentation I understand that the size of the container cannot change, it is readonly.
I have code that places text into the text property of a UITextView but it will not scroll the text right or left. I played around with inserting a UIView into the textview and believe that will work, but doesn’t seem like the way to about this. I’m kind of figuring that I need a custom UITextView class and a custom NSTextContainer. Wondering if the proper direction? Any help is appreciated.
Thanks,
Jeff

Related

Layout UITextField regardless of clear button X

I need to horizontally center layout a UITextField, however, since the clear button is part of it, the (text entry portion of) text field doesn't look centered - the clear button isn't always visible but it is taken into account when laying out.
In the image above, the UITextField is horizontally centered, however, without the clear button (X on the right) and with only search text, it doesn't look like centered.
A way I can think of, is to subclass UITextField and provide alignmentRectInsets, for inset on the right I would use clearButtonRect(forBounds:) to get the size of clear button, whose width will be the right inset for alignmentRectInsets.
Question:
Apple's doc specifically mentions that one should not call clearButtonRect(forBounds:) directly, so I'm feeling a bit nervous for doing it; However, from the doc it feels like Apple's intention is to let people not changing the rect, and in my case I'm just getting its size, I guess it's fine?
Is there any better way of achieving this? I know I can tweak edgeInsets, or give the text field a leftView, to make the text field looking centered; But they all need some hardcoded assumption for the size of the clear button.
Thanks!
You can make the UITextField Alignment settings as middle then it will work. Check the below image
You can control over here.It may helps to you.Thank you.

Textfield Connecting and Labels

I have always had this problem and could never figure out how to solve it. My issue is that I want to produce a similar thing as the photo: Image
I want to be able to have text fields connect like this and be able to have a 'label' on the left while still being able to input on the right. Any help is greatly appreciated.
If I am not mistaken, I think that what you want is to have a label on the left and a textview on the right. The image you have supplied looks like the textViews and labels reside within a UITableView controller as static cells. What they have most likely have done here is simply extended the label from the left to the centre and the textview from the right to the centre, such that each takes up half the cell (and added constraints). Then you can set the text alignment to left for the label and right for the textview. The final thing you should do is get rid of the border of the textview, which can be found in the attributes inspector and is called Border Style. You want to set it to the far left option, like this:
As the background of the cell is white and the textview's also, you shouldn't need to change it, but if you do there is an attribute for that a bit further down that you can have a play with.

Adding UITextField inline UILabel (Fill in the blanks)

I am trying to achieve something like this for iOS in Objective C.
The fill in the blanks(UITextField) should be inline, and should be able to have its own inputType.
Also, each View is a type of cell.contentView of a UITableViewCell.
My current approach is to find the length of string and also calculate the wrapping content length to the next line. Calculate the x's and y's for UITextField and add another UILabel after the UTextField
Is there any other approach other than this?
As EmilioPelaez says, this is not exactly an answer to your question, but a suggestion:
You can use a collection view with an horizontal flow for each "sequence" (i.e. UILabel-UItextfield-etc...)
That collection view has 2 kind of cell:
One with a uilabel with the number of line set to "1"
and the correct layout to fit the cell.
Another with a uitextfield and the correct layout
Coupled with:
My current approach is to find the length of string and also calculate the wrapping content length to the next line.
You may be able to easily adjust the width of the different cells, hide a uitextfield (if needed) and display a more dynamic "sequence" (if needed)
This is not exactly an answer to your question, instead it's a suggestion for a different interaction.
I think that instead of using inline textFields, you could use a UILabel with an attributed string, and in where the textFields would be, you add a different character with a different color that you can tap (For example, this character ✚).
When tapped, you can show an overlay with a text input, and once that input is completed, you update the label with the text (still tappable, and with a different color).
I think this answer might also be relevant: Detecting taps on attributed text in a UITextView in iOS
I think your solution of separating the UILabels and calculating their required positions is a good one for versions lower than iOS9, but if you can count on iOS, UIStackView can dramatically simplify the process for you. You can read more about the process in this tutorial:
UIStackView Tutorial
Good luck!

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

"Manually" handling text and cursor position of UITextView breaks when there are multiple lines

I have a narrow UITextView (about 1 line in height) to which I only add text programmatically, setting the text property of it as I click buttons. I also handle the cursor position manually, setting the selectedRange as the text changes. All works great as long as I have only one line of text.
When I have two or more lines of text and try to insert text at the first line, the text is inserted correctly and the cursor position is still at the right place but the UITextView scrolls to the bottom. When I then add another piece of text at the top it scrolls up to the "correct" position. This pattern then repeats for every entered piece of text at a line other than the last, making the UITextView scroll up and down for every button pressed.
I also tried calling UITextView scrollRangeToVisible: passing the selectedRange property as argument after setting the new text. That didn't work either.
I finally tried setting selectedRange after a 0.5 s delay after I set the text. Then it works as it should, but only after the UITextView has first been scrolled down to the bottom for 0.5 s. This seems to indicate that the setText method of UITextView is asynchronous in some way, and completes after I have already set selectedRange or called scrollRangeToVisible, and readjusts the UITextView to what it believes is the desired.
Can anyone tell me what is going on, and how I can get around the problem.
Thanks!
The auto scroll behavior of UITextFields can be annoying and difficult to harness at times.
Do you need the user to manually edit the text? If not, use a multiline UILabel.
If yes, also use a multiline UILabel and exchange it on the fly against a UITextView once the user taps. Change it back on didEndEditing. This techniques has worked well for me in table views.
I solved the problem!
When starting in the maze of creating a custom input view, I began implementing UITextInput and I found that the UITextInput protocol declares many interesting methods. And what is awesome is that UITextView implements that protocol. So even though it's not in the immediate UITextView API reference, you can call all UITextInput methods on your UITextView. For instance insertText: (from UIKeyInput protocol) and replaceRange:withText:. They made it much more flexible to programmatically work with a UITextView. Hope this can help someone else too!

Resources