UIScrollView and variable text size - ios

I'm creating an app with quite a lot of text. It will be of variable size, so I was wondering if there is a way to adapt my ScrollView to the size of the UILabel, not knowing in the IB what will be the number of lines of that label. I'm doing it from the IB, drag n dropping it, then I'm putting 4 elements inside : an image for the logo, a title, another image, and then the label.
My question is : how can I tell my ScrollView to adapt to the text ? With constraints ? In the code ? I'm sooooo lost with that scrolling thing…
I think I need a very very good tutorial about autolayout…
Here's some screenshots of my project
The project on simulator
The layout
Thanks a lot!

You have to add the constraints to your label so that it will always have the same spacing to the scroll view. Also, you will have to set your labels lines to 0.
Don't press Add Missing Constraints because it will probably set your height and you don't want that to happen. Use the Pin button to set the spacing to nearest neighbor.
These are the buttons you will have to use, they are bottom right.
First of all, press the one in the right and select All Views > Clear Constraints.
Then, select your label in the storyboard and press the button with a square Pin, here you will have to select everything until you have something like this with different values... Also click in the red lines so that you get a continuous red line. Don't check any of the boxes you see.
After this, you have to do the same process for your Scroller.
It should be enough. I will also need to know where your scroller is.
If you have anything else in your view, do the same.

Related

JSQMessagesViewController Custom Cell Auto layout Issue - IOS

I am using JSQMessagesViewController and I created a custom cell according to this answer.
How to add custom view to JSQMessagesViewController cell so that it includes one view with some buttons and text view?
Now I am struggling to set auto layout constrains correctly. So, the message bubble not display correctly. Please help me set the constraints correctly
So lets take a stab at this. So first things that I would do it set a constraint for the first button to be equal hight of the other. You can accomplish a couple of ways but I will only describe one here for brevity. You can do hold the control button on your computer and select button1 and drag to button2. This will present you with a couple of options that look like this.
You want to select Equal Heights this will make it so both your buttons have the same hight. Then you will want to give it a hight. Once again hold down the control button on your keyboard but this time click button1 drag and release within button1. you should get something along the lines of this.
If you do not get the desired options try dragging in a diagonal direction. Xcode is tying to guess what constraints you want based on the direction of your drag. I.E. If you drag vertically you should get the Height option.
Then you can go to the properties inspector on the right and set a number for how high you would like. Text is normally around 12pt so I would go with about 30pt or more for the hight of a button. Then add a constraint for spacing between them and leading and trailing to the containing view or you could give them a standard width and center them in the view. Which ever fits best for you.
Edit:
You should also adjust the bubble size calculation.
It can be found in the JSQMessagesBubblesSizeCalculator class.
Eg: In the
- (CGSize)messageBubbleSizeForMessageData {
if([messageData isOptionMessage]){
// add button height also (In this case i have set constant 200. But we have to calculate the actual button heights instead)
finalSize = CGSizeMake(finalWidth, stringSize.height + verticalInsets + 200)
} else {
finalSize = CGSizeMake(finalWidth, stringSize.height + verticalInsets)
}
}
Let me know if you need any more help and keep up the good work. 👍🏼 🚜

Easiest way to Add a label to scrollview

So I am wanting to create a Scrollview with a list of labels. I am having a super hard time with the constraints and what not. It is obviously nothing like adding labels to a simple UIView.
What i need is one label in the top left corner, about 40% width from parent view with a top and left border of 10. Then i need a second label right next to it also with a 40% width and a top and right border of 10. After doing this i will continue creating labels under each other in the same sequence. All with borders of 10.
(P.S. I know labels have a set height value but i have been giving them values of 0.05% height of parent view just for organizing reasons.)
Now, normally when adding to a normal view i would match the parent width and height then go to the identity inspector and under the sizes-equalwidthsconstraint-multiplier set it to 0.4 and height etc. Then I would go to the pin and add the constraints top: 10 and left: 10. By that time i would be golden with the first label! However it doesnt work that way with scrollviews. I keep getting red lines every where and a caution alert saying "Scrollable content size is ambiguous for "Scroll View"".
(P.S. I have however been able to successfully create the first label, with red lines though, but when creating the second label i can never get it to line up right.)
If you dont know how to do it through storyboard and should do it programmatically then how would i do that? Swift please, always.
for example:
make: Chevrolet
model: Silverado
year: 2016
color: Pearl White
so on and so forth....
its basically a list of non editable text..
i was wanting to go for that look.
Please help, thank you!
Use UITableView's. Apple has designed UITableView for these kinds of problem statements.
Here is a good place to start.
http://code.tutsplus.com/tutorials/ios-from-scratch-with-swift-table-view-basics--cms-25160
And for your case, you need to use Custom UITableViewCell for your specific problem

Auto height setting for labels in iOS

I have previously worked in Windows phone and see that every control in windows phone has an Auto property, meaning occupy the size of the content.
I see that in iOS such a property does not exist. When there are dynamic data to be bound to a UILabel, I always need to calculate the height of the data and then assign to the UILabel. This takes a good amount of time and bit painful. Is not there an Auto property or am I missing anything here?
iOS has AutoLayout which is really helpful, get familiar with it.
Click on the Label
Click on the pin constraints button (little square button)
Add your custom LEFT, RIGHT, TOP margins or LEFT, RIGHT, BOTTOM margins
Click on "Add 3 Constraints"
Set number of Lines to 0 which means as much lines as view needs
Then you probably got warning lines, but you can solve them
Just click on fix constraints button (little triangle button)
Click update frames
UPDATE
Important: the answer to your question is to PUT NUMBER OF LINES TO 0 you can use that UILabel with 0 lines(which is autosizing) with frames and AutoLayout. AutoLayout is just a friendly suggestion that can be helful to setup views. Also put Line Breaking Word Wrap
Here you go also with some useful links for working with AutoLayout. AutoLayout is great because you don't care anymore what size is the screen, what orientation has the device at that moment. You just need to setup everything correctly and everything works amazingly but if your setup is wrong then AutoLayout might become your enemy. So start learning and experiencing right now.
Very good point to Begin learning AutoLayout
If Your are being lazy, start from video tutorial series
Great iOS7+ table view tutorial with autoresizing cells
Also check out this Stack Overflow discussion
You need to familiarize yourself with Auto Layout:
Auto Layout dynamically calculates the size and position of all the
views in your view hierarchy, based on constraints placed on those
views.
Just give top, left and right constraints and make label's numberOfLines to 0. That's it. Label's height will resize automatically.

Two Buttons. Above each other. Over whole screen

I just like two buttons above each other with no margins. This seems like an easy task, but I am new to iOS and I wasted too much time now. I tried dozens of contraints but it never went out well. I always ended up with setting the leading Spaces to -24 and having marings above and beneath the buttons. I am using xcode6, iOS8.1 and storyboard. My app is landscape only. Can someone please teach me what to do?
The buttons should look like seen in this picture: Fullscreen, no margins and Button1 should reach unter the statusbar!
I'll outline the steps:
Pin Button 1 to 0 spacing on Left, Right, Top
Pin Button 2 to 0 spacing on Left, Right, Bottom
Select both buttons, then Pin heights equally
(You can access pin either from the Editor tool bar in the top menu, or from the second icon in the constraints area)
The thing with constraints and auto layout is that you only need enough constraints to let the app calculate where elements should be at all times. You might be able to get away with less constraints.
To get remove the borders, you need to change the button Type in IB from System to Custom, then you can drag and alter the margins to remove them. Following that you will need to update constraints on the buttons.

Set fixed space between 2 views in Interface Builder

I am trying to learn to use layout constraints in interface builder but I am running across an issue...
I've got two views, one UILabel and one UIImageView, and I'd like a layout as follows:
the label should be centered in the superview
the imageview should be a set distance to the left of the label
Seems pretty simple, but I can't figure out how to impose a constraint for the second condition (the first is done automatically). Anyone know how to do this?
Thanks!
Click on both views while holding the command key so they're both highlighted. Then click on the pin button in the bottom right hand corner of IB (the middle button). Select horizontal spacing and the constraint should now be added. You should be able to adjust it as needed from there.

Resources