How to make a UITextView scroll horizontally as the user types? - iphone

This seems like it should be simple but I can't work out how to do it.
I've created a UITextView in interface builder. It doesn't sit in a table cell or anything fancy like that.
What I'd like to do is have the UITextView scroll itself to the left when the user has typed their way all the way to the right margin.
At the moment it just does a word wrap, I understand the word wrap makes sense in most situations but I need it to scroll instead. What I'm after is the same behavior it exhibits vertically.
I've tried adjusting the content size of the UITextView in viewDidLoad and also in viewDidAppear and that doesn't make a difference.
I've also played with the inset settings in IB but that doesn't affect the scrollable size, just WHERE it's displayed.
Does anyone have any ideas?

If you only need a single line of text that scrolls, you would probably be better served by a UITextField. UITextViews are meant for multi-line text input, which is why word wrap is the default. You can't, as far as I know, override that behavior.

Ok, it doesn't look like it can be done exactly how I wanted but I've come up with a hack to get me there.
Let's say I want a scrollable width of 600 and a visible width of 250.
The first step is to make the UITextView a wide as the area you want to be scrollable. So what you would have put into contentSize, in this case 600.
Then the right inset is set to the difference between the actual width and the width you wanted. In this case 350.
This way cursor is restrained to the width you need BUT there is text visible to the right of your desired width, after all, the UITextView IS 600 pixels wide.
Now here's where the really hackish bit comes in, don't read on if you're sensitive or have a weak stomach.
Get an image of the user interface to the right where UITextView should end. Insert it into the NIB as an UIImage view and put it back in it's place, making sure that it's on top of the UITextView.
When the view is displayed, the cutout sits on top of the UITextView and hides the text overrun.
I'm not proud, but it works.

Related

What anchors to use for UITextView?

If I set the leading/trailing, top/bottom anchors, my UITextView appears; if I set leading/trailing, top anchor + height, then it appears too. But if I leave one anchor out, my UITextView doesn't appear at all. Why is this? I'd assume it would spill out? For example, if I didn't specify a bottom anchor, I'd assume it would overflow to the bottom. But I don't think it appears at all - what exactly is happening here?
It's very simple. You must specify, for every view, all four aspects:
horizontal position
vertical position
horizontal size
vertical size
That's all there is to it. If you omit one of those, then your specifications are incomplete ("ambiguous") and the view is, if you're lucky, not drawn.
Some view types, such as a UILabel or a UIButton or a UIImageView, supply one or both of the size aspects internally based on their contents. So for those view types you might be able to omit specifying that aspect yourself. But a UITextView is not one of those; it's all entirely up to you.

Swift: UIScrollView doesn't scroll after the user edits the height of a UITextView

I have two UITextView inside a UIScrollView and the TextView can resize as the user types more text into them. I need the page to be scrollable to account for this so I put them in a ScrollView, however the height of the ContentView doesn't change with the TextViews. Here's what I tried so far:
adding a bottom constraint from the last helper text to the bottom of
the content view with priority 1000 and constant 210. This doesn't
work because the section will be scrollable before the TextViews get
taller and on tablets or larger devices it will make my first
TextView taller.
adding a bottom constraint with a constant of 210 or lower will give me a layout error.
I also thought about calculating the height of the content view and maybe adjusting the constraint based on that but this doesn't work either.
Here is a link to the file in question: https://www.dropbox.com/s/g7q1lbj0pxx14jh/Main.storyboard?dl=0
Any suggestions?
It's also worth noting that the keyboard will cover the second TextView when the first one gets too big so I will eventually need to edit that constraint based on the height of the first TextView (to give it more padding at the bottom so that the user can bring the second TextView in view.
Here is the fixed variant https://www.dropbox.com/s/gujry7ngziaipm7/Main.storyboard?dl=0
I found just 1 error:
You forgot to specify bottom offset from last view in View Container which is UILabel - "Being clear...", so your view container doesn't know when to resize as soon as you do not specified all offsets to it.

iOS: how to adjust positions of UI elements one by one properly by IB when some of them may change size?

For example, on a UIView, from left to right there are three buttons:,button1,button2,button3.
When button 1 become wider, such as its width become twice as its original width, is there any interface builder way that can make button2&3 move right automatically?
sorry for I didn't make my question clear.
I mean such kind of size change: I push button1, then I change it's frame in my code, not caused by the change of text in butotn1. I want button2&3 to move right automatically, keeping the width of the gap between button 1 and 2 unchanged.
Thanks everyone.
IB can be used only for initional positioning of views.
True, you can also define autoresizing masks of the views but that's about it.
Any additional laying out should be done in code.
I could be wrong, but I don't know of any way you can do this in IB. It's pretty straight forward in code though, just link the buttons to some IBOutlets and check the sizes of the images of the buttons (myUIImage.size), then adjust the frames of the buttons (do it in viewWillAppear).
Seen your edit - if you're adjusting its size using code, adjust its position too.
If your buttons are in a row at the bottom of the screen, consider using a UIToolBar. Its UIBarButtonItem objects automatically adjust to fit each other's width changes. For more generic cases, you'll need to recalculate positions and sizes as in Franklyn Weber's answer.
Yes - by using autoresizing masks. If you allow the margins to be flexible (no red lines connecting the frame to the superview's frame) and allow flexible width and height, the buttons will size and move proportionally.

How to position my UIScrollView and Text Field inside?

You may find my question stupid but I'm new to Objective C dev and I'm still learning basic things. I made a form in my app with a text field in the bottom of the view. But when this field get the focus, keyboard shows up and it hides the field so no way to see what I'm typing. I've found solutions online about it especially with the use of UIScrollView to fix the issue.
The issue I've now is with the Scroll View itself. I moved my text field to replace it with a UIScrollView of about same size, then I drag drop my field back but this time as a subview of the ScrollView. But when running the app the text field is going up, that is its position in interface builder is different to what I can see when running the app.
I need your help about the size I need to apply to the Scroll View and its content to make it useful in this case. Should it have the exact size as the text field ? should it have the same size as the whole view ?
Thx for helping,
Stephane
A UIScrollView has two "sizes". The one is that of its own frame (i.e. how much space does it take up). And how large is the contentSize of the scrollview (i.e. how far left/right/up/down can we scroll)
The general Idea would be this:
your UIScrollView has the size of the available screen real estate when the keyboard is hidden (say 320 x 460 or so), it takes up as much space as its content (assuming your content also needs 320 x 460), scrolling isn't enabled.
once the keyboard shows, the scrollView will have to scale down (say 320 x 244), the content obviously doesn't scale down, that way you're now able to scroll.
I'd probably use something like
moveTextViewForKeyboard:(NSNotification*)aNotification up: (BOOL) up
to trigger the frameSize change of the scrollView and enable scrolling.

How do i bring text in UITextView inwards from both sides and from above?

I want my UITextView to show text but not have the text be as wide as the screen. The problem I face with this, however, is that I want the scrollbar to be in the position it's in now. I'm unsure how to do this. Here's an image of the text i want bringing in, to show you what i mean a bit more clearly:
You can customize the position of the text using the contentInset, contentOffset and contentSize properties of UIScrollView (which is the superclass of UITextView).