How do I make a UITextView Wrap to accommodate text within it? - objective-c

I have several UITextViews that contain numbers between one and three digits. I'd like these views to expand and shrink as the number becomes larger or smaller. At the moment the text is shrinking and the TextViews size remains the same.
I'd preferable like to do this using the xib file (I'm very surprised this isn't a meagre check box) but a coded answer would be great if this isn't possible.

I'd recommend using the contentSize property of the UITextView:
self.textView.frame = CGRectMake(self.textView.frame.origin.x, self.textView.frame.origin.y, self.textView.contentSize.width, self.textView.contentSize.height);

Related

How could I create a horizontal UIPickerView style control?

I want to create a custom UIView that would be equivalent to a horizontal UIPickerView. The data elements, such as "Mountain View, Sunnyvale, Cupertino, Santa Clara, San Jose" would move left to right. The view would be long and thin. 50px in height, 350x in width sort of thing.
I will need to use protocols to obtain my datasource elements likely, but what I'm more interested in figuring out is the visual aspect of this.
How would I animate things left and right? Should I just make one incredibly long view (off screen), and change the frame left and right. This seems like a bad idea, as likely only 3-5 options need to be visible in the control at one time. If the datasource is 100 elements, there is no point in loading the other 95 off screen in a long view.
So perhaps I should load ~9 or something. The 3-5 on screen, plus an additional ~3 left and right. Each time the control is triggered to move left and right, it will load up another element on the view(?).
Is this a good way to achieve this? A long thin view with ~9 UILabel's. As the control moves to the right, I would shift the further left UILabel to move hidden to the far right, and change the UILabel to be the next in the data source.
I also likely want to change the text size based on its position. If it's currently selected, I either want to bold it and possibly increase the font size. How can I gradually achieve this as the view is moved? It would be weird if the text only changed once it was moved perfectly center inside the view. It should likely gradually grow as it gets closer to the middle.
How can I achieve this?
A collection view would do the trick, obviously there's some coding involved.
I'd suggest you to take a look at the code of, or use, the following library.
I've used it once,does almost exactly what you need and works very well:
AKPickerView

Having troubles fitting the UIImageView and Label into Scroll View

I want to fit the Image + Label into Scrollable View.
It looks like so My setup
So trouble here is that width of the picture and the label did not match the screen width.
I tried to change the UIImageView width from 400 to 300 or so, BUT those changes either wont take the effect(if above 300), OR those changes make label disappear (with width setted up on 300 or below). Observe.
Funny thing is - label actually appears when you see view hierarchy, even though it looks kinda distorted. But no label on the iOS screen for you, sir.
I also tried to set equal width and height from the superview, but this option just disabling scroll, making view strictly fit the screen (label appears to be cropped)
I want the label to be nice and scrollable, but I also want to see it on the screen. Too much to ask?
EDIT: I just tried removing Auto-layout and got the same as with auto-layout. (click "Observe" link to take a look again, exact same situation) Could somebody explain why?
This is quite a simple fix. For the image, you should use equal width & center horizontally; however, make sure you use a fixed width. For the image to look nice, play with the scale modes - I find Aspect Fit/Fill work best.
The label should also be equal width & centered horizontally. You may need to calculate the height of the label programmatically if the text isn't always going to be the same size.
So the answer to my particular question was - shortening the actual text that is passed to the label.
Apparently UIScrollView can't handle stuff with crazy abnormal heights (I was passing a huuuge text to it) So in my particular situation solution was - shorten the text. Now it works like a charm (even though I still need to play a little with constraints to get rid of the warnings and stuff)

Resizing & Layout: How to implement this design?

OK, this might seem very simple but I've been struggling for too long, so I decided to ask for some help.
Basical, I've got a container NSView.
The contents are (from left to right):
an NSSegmentedControl
an NSTextField
an NSSegmentedControl
And they are all aligned horizontally (= they're in the same "line").
What I'm trying to do is:
Fixed width for the first element, and fixed on the left
resizeable textfield
Fixed width for the last element, and fixed on the right
I guess it's pretty self-explanatory.
And here are my autoresizing settings for the 3 views:
View 1
View 2
View 3
And here's the issue: the whole resizing works fine. Until the textfield is resized at such a point where it is collapsed. When the superview/container is resized at a normal size again, the whole design is messed up (with the textfield overflowing the container and lots of such weird issue).
What's going on? Any suggestions?
Note: I got it to work with Auto Layout (merely setting "Auto Layout" and "Add(ing) missing constraints"), but I most definitely want to avoid it since the performance impact is huge -- the overall design is rather complicated, with lots of nesting, so let's stick to the old-fashioned way. :-)
The old springs-and-struts model using autoresizing masks is based on proportional distribution of the change in size based on the current sizes of the subviews being resized. Once any subview gets to zero size, it goes haywire. Either there's division by zero or multiplication by zero and you get bogus results. This is a long-standing issue with the system.
You must set minimum window size to prevent that from happening.
Auto layout is the solution to that and many other limitations of the old model, for all of its flaws.

How to resize UILabels to fit content?

My program currently has thirty UILabels that may contain numbers between one and three digits. I want my UILabels to expand and shrink horizontally as the numbers within them change size. At the moment the text is shrinking and the UILabels size remains the same.
If this can be done just using the xib file this would be preferable to doing so programmatically but if this isn't possible either would be great.
Thanks
After setting the text of the label, call sizeToFit on the label. This will make the label just big enough for the text.
BTW - why do you need to do this? Why not just make the labels big enough for the possible values and leave them that size? What benefit do you think you will get by shrinking the labels?

Constraining the area for text in an UILabelView

I've got a UILabelView that takes up a large amount of space (and it has to do this).
However, I want the text within it to stay within a small area within it. I've had a look around, and I see there is a method fro containing the area text can occupy in a text view - is there something similar for UILabelView? So I can keep text within a CGRect of the label view?
Thanks for your help,
Sam
Thanks #Dustin - basically it looks like the only way is to put it into a view.