IOS How to add a view next to another view? - uiview

I have a situation like this
I want to add a UILabel next to an UILabel dynamically.
But the position of the 2nd label depends on the length of the string of the 1st label which is not fixed.
How to do this?

Once you set the new text to the first label, access frame property of your label.
The position for the second label would be :
CGRect f = firstLabel.frame;
CGRect f2 = CGRectMake(f.origin.x + f.size.width,
f.origin.y,
theWidthYouWant,
f.size.height);
secondLabel.frame = f2;
The second label is now next to the first.
If you want to add a new label to the container view, use initWithFrame: initializer with the computed frame above.

Related

How can I place a UILabel below an UIImageView such that it is always evenly placed

I have made a custom pop view which looks like this
I want to place the label of user name below the user image which is the rounded UIImageView in the given image
I want the user name to be placed evenly irrespective of the size of the text.
Like I want the name to appear like this
Align the Label content to Center.
Set a constraint from label to Image View. Horizontally center label to Image View.
As you are not using the storyboard, Below code will help you in setting the frame of label:-
UILabel *labelName;//This will be your label instance
CGFloat yAxis = 50;//This will be your image view's y axis + image view's height
[labelName setFrame:CGRectMake(0, yAxis, self.view.frame.size.width, self.view.frame.size.height)];
//Set the color/Font other properties of label as desired
[labelName setTextAlignment:NSTextAlignmentCenter];
Setting the constraint is the efficient solution.
Either you can follow the storyboard way as suggested by Mr.UB or the approach it by code way.I suggest you to use Masonry for setting the constraints if you wish to proceed by code.
And you can simply do:
[your-label mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(imgView.mas_top); //with is an optional semantic filler
make.mas_centerX.equalTo(imgView.mas_centerX);
}];

Set height UILabel to 0

Is there anyway to set the height of a UILabel programmatically? I've added a bunch of constraints to my .Xib files so every other label is dependent upon the one above or below it for it's positioning. It'd make my life so much easier if I could just use:
nameLabel.height = 0
My .Xib looks like this:
plz take the IBoutlet of constraint height and set the height you want
try this:
var frame: CGRect = nameLabel.frame
frame.size.height = height
nameLabel.frame = frame
Write this line :
constLblHeight.constant = 0;
//Below image step
//Step 1: Add Constraint in label height
//Step 2: Create object of label height constraint (constLblHeight)
If you empty the label its height will be zero.
nameLabel.text = ""
Even no need to hide it.

Create View below dynamic View

I have a label without defined height, her height will be set accordingly to the text that she receives from the array(can be any height).
Now I want to create a webview that goes below the label, if I create it through interface builder the webview will not receive clicks so I thought that i could create programmatically but how can I find the Y(CGRectMake(x,y,w,h)) after the label gets his height to create the frame for that position?
EDIT 1: What i want is to create a View below that label, but what happens is that when the view goes down because of the label she stops to receive clicks
EDIT 2: I Have a label below that is separator(Gray Line to separate) and then a View that programmatically will receive a WebView, but when the Label is short, the UIWebView is clickable, when I have a long label I can't click in the WebView.
The label can have like 50 or 500 as height soo tell me how can I Create a view below that label that will work properly
Thanks in advance
You can use auto layout here..
UILabel *yourlabel = [[UILabel alloc]init];
yourlabel.numberOfLines = 0;
yourlabel.lineBreakMode = NSLineBreakByWordWrapping;
yourlabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[self.view addSubview:infoLabel];
yourlabel -- set leading , trailing and top constrain
below your label you can add your other view.
To add webVIew under UILabel, you need to use insertSubView instead of addSubView.
Try looking into insertSubview:atIndex:
Also look into insertSubview:aboveSubview

Dynamically resize label based on lines of text

I am finding it surprisingly hard to resize a label containing newlines based on the quantity of lines and text. It displays fine in a large enough textview. However, I'd like the economy of sizing the label--or I'd be happy with resizing a textview--exactly.
This is the code I am using from an answer on SO but it is having no effect on the size of the label. Would appreciate any suggestions on how to make this work:
NSString *list = self.list.list;
// use font information from the UILabel to calculate the size
UITextView *view=[[UITextView alloc] initWithFrame:CGRectMake(0, 0, 280, 10)];
//make random size view
view.text=list;
CGSize size=[view sizeThatFits:CGSizeMake(280, CGFLOAT_MAX)];
// create a frame that is filled with the UILabel frame data
CGRect newFrame = _listLabel.frame;
// resizing the frame to calculated size
newFrame.size.height = size.height;
// put calculated frame into UILabel frame
_listLabel.frame = newFrame;
Why are you setting the frame of your label with reference of a newly created UITextView, it will create a useless object in your memory, to set the label frame according to your text just use this 2 line of code
lbl.numberOfLines=0;
[lbl sizeToFit];
It will make the label as large as your text.
You really should use autolayout.
Just constrain the label where you need and let UIKit do it's job.
Here an example:
I set a top space and a leading margin constraints
Then I added a width constraint and then I added some more text
As you can see the label resized itself as it knows how much text it has inside and how much space it occupies.

Dynamically Sized UILabel Followed Directly by UIButton

How can I position a UIButton right at the end of a dynamic UILabel?
Here is a screenshot of what I'm trying to achieve:
Are you using Autolayout? If so, add horizontal spacing constraint and baseline alignment constraint between the label and the button. Make sure also that numbers of lines of the label is 0.
Try
[your_label sizeToFit];
CGRect rect = your_button.frame;
rect.origin.x = CGRectGetMaxX(your_label.frame);
your_button.frame = rect;
I'd override -[UIView layoutSubviews] and do what you want there. Once you do that, the code will look something like Avt's answer. If you want the label to have multiple lines, but a constrained width, you may want to try something like
CGSize labelSize = [yourLabel sizeThatFits:CGSizeMake(someWidth, 9999)];
and use that to set the label's frame.
If you want the button to appear at the bottom of the UILabel, use constrains.

Resources