I have a label that I create using the following way:
UILabel *label = [[UILabel alloc] initWithFrame:cell.contentView.bounds];
label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
label.textAlignment = UITextAlignmentCenter;
I would like to center the on the bottom of the label and center it horizontally.
I did not find any UITextAlignmentBottom in the typedef enums.
Can somebody suggest a solution?
To 'align' the text to the bottom, you will want to make the UILabel the height of the text it needs to display - Google UILabel sizeToFit.
I'm not entirely sure what you mean by "center the on the bottom of the label", but I'm thinking you might be asking about how to center a label at the bottom of the view controller that it's in. To do this, you will want to add some NSLayoutConstraints to specify where in the window you want the label to go.
To do this programmatically, you will use constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:. The "attribute" argument refers to NSLayoutAttribute, which can be set as NSLayoutAttributeCenterX when attempting to center the label, and NSLayoutAttributeBottom to specify the amount of space between the label and the bottom of the view controller.
Related
I'm creating a UILabel, adding some text, and then calling [myLabel sizeToFit], which resizes the label to fit the text. However, I'm noticing some margin -- or edge insets -- to the left and right, almost as if it's purposely buffering my text.
I'm very short on space, and I'd like to run sizeToFit without these margins, i.e. I want the last pixel of my text to run up against the bounds on left & right. How do I accomplish this?
- (void) layoutGroupTabLabeled:(NSString*)title {
// Determine how long a label we need:
UILabel* label = [[UILabel alloc] init];
label.text = NSLocalizedString(title, #"");
[label sizeToFit];
}
The label should be tight, but has space to left & right.
The answer to your problem is using AutoLayout constraints. In your case it should be covered by adding leading and trailing constraints to the label.
If you don't know your way around AutoLayout I recommend checking these answers.
Adding constraints programmatically in Objective-C
I would like to pin a button to the bottom right corner of a textfield. I completed this in Android using a relativelayout. I don't know how to do this in IOS. I attempted to set top and bottom constraints, but it didn't work.
Help?
Example
UITextField * rightField;
rightField.rightViewMode = UITextFieldViewModeAlways;
UIImageView *rightImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.png"]];
rightField.rightView = rightImageView; // Set right view as image view
set UITextField rightView as your button. Here in my example I added UIImageView to UITextField rightView.
Since you're using a storyboard you'd probably want something like this if I'm not mistaken:
To do this set the following trailing and leading constraints on your textfield:
Your button should have the following constraints:
Ok I have successfully pinned the button.
This is what I did.
I used control+click to set bottom and trailing constraints between the textfield and the button. Then I set equal height and equal width constraints between the button and the view and used a multiplier.
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);
}];
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
I have a container UIView, and a UILabel. The container view size is set, I then add text to the UILabel, call sizeToFit on the label, and add the label as a subview of the container view.
I then use the following code to center the label inside the container view:
viewCountLabel.frame = CGRectMake(viewCountContainer.frame.size.width/2 - viewCountLabel.frame.size.width/2, viewCountContainer.frame.size.height/2 - viewCountLabel.frame.size.height/2, viewCountLabel.frame.size.width, viewCountLabel.frame.size.height);
This is pretty standard code to center something to it's superview/container, and it always works pretty well.
However, right now I have a small problem. This label's text is a simple number. It could be 1, 9, 15, 22, etc.
My problem is that when I center everything, some numbers look off center even though they technically are centered perfectly.
Here's an example pic with borders for both the container and the label so you can see that they are centered perfectly:
But here's how it looks without the borders:
The 15 does not look like it's centered even though it technically is. It looks like its a little too far to the right. If I subtract 2 or 3 from the x value, it will visually appear centered, but see that's the problem.
How can you setup logic to "visually center" various values for a label like this? I feel like setting a bunch of if statements based on the text value is overkill and only applying a bandaid to the problem.
I have also tried using NSTextAlignmentCenter and that does not make a difference. I'm not sure if I'm using NSTextAlignmentCenter incorrectly, or if there's a potential problem with sizeToFit.
You'll notice that that label's blue border has extra space on all sides, but most importantly for this problem it has extra space on the left and right sides. I feel like that could be causing this issue, but I'm not sure how to fix that or if that's even the problem.
Here is my code:
// Setup view count label container
UIView *viewCountContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 95, 95)];
viewCountContainer.frame = CGRectMake(self.view.frame.size.width/2 - viewCountContainer.frame.size.width/2, 87, viewCountContainer.frame.size.width, viewCountContainer.frame.size.height);
// Setup + add number label to container
UILabel *viewCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.pushModal.frame.size.width, 95)];
viewCountLabel.textAlignment = NSTextAlignmentCenter;
viewCountLabel.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:72];
viewCountLabel.textColor = [UIColor colorWithRed:0.278 green:0.278 blue:0.278 alpha:1];
viewCountLabel.text = [NSString stringWithFormat:#"%d", viewCount];
[viewCountLabel sizeToFit];
// Center the label to it's super view (viewCountContainer)
viewCountLabel.frame = CGRectMake(viewCountContainer.frame.size.width/2 - viewCountLabel.frame.size.width/2, viewCountContainer.frame.size.height/2 - viewCountLabel.frame.size.height/2, viewCountLabel.frame.size.width, viewCountLabel.frame.size.height);
[viewCountContainer addSubview:viewCountLabel];
[self.view addSubview:viewCountContainer];
Unfortunately there is no "perfect" way to do this. I've added character spacing to the label, tried setting its width to the superview's width and centering, etc. but you'll never be able to do this dynamically and get it perfect this way.
The only way I could see this being perfect every time is if you made the UILabel first, and then programmatically drew a circle around it, and then centered the label.
That or the hard way of adding logic for every number and visually centering it that way.
Number can be centered by using monospacedDigitSystemFont(ofSize:weight:) in combination with textAlignment = .center