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.
Related
I am trying to alter the width and height of an UIImageView object but for some reason I can't.
This is the code I used:
-(void)setMenuImages
{
self.menuImageButton.image=[UIImage imageNamed:#"myimage.jpg"];
self.menuImageButton.frame=CGRectMake(0,0,20,150);
}
What am I doing wrong? The (UIImageView) menuImageButton is not created programmatically, but from storyboard interface with initial leading, trailing, width and height constraints.
You can self. menuImageButton.translatesAutoresizingMaskIntoConstraints = false in viewDidLoad and remove their constraints or you can add those constraints as IBOutlets and change them like
self.imageWidthConstraint.constant = 20;
self.imageHeightConstraint.constant = 150;
UPDATE
To declare constraints in your header file you need to select those constrains and right click drag to the .h file as show in the gif
I have UILabel that the text is dynamically change based on server data. Sometime the data is so long that make my UILabel become multiline. Is there any way to calculate the height of my UILabel?
You can use this :
var labelHeight : CGFloat
labelHeight = theLabel.boundingHeightForFixedWidth(theLabel.bounds.width)
Hope it help :)
If you are using Autolayout then no need to calculate height, just add leading,top and trailing constraint, set number of line to 0 for that label and line break mode to word-wrap, it will automatically update its height.
P.S. Add the bottom contraints too if there is any other control in nib after label.
I need to update UIView size.
Now i use
var titleSize = CGRectMake(0, 30, 40 , 1000)
self.contentCoverScrollView.frame = titleSize
in viewDidLoad, but nothing happens.
In Autolayout if you want to change the frame then try the constraints (IBOutlet of NSLayoutConstraint).
Set the constraint outlets and change constant value by :
self.sampleConstraint.constant = 20
self.view.layoutIfNeeded()
I thought you had used the AutoLayout in your application that's why it can not change the view frame, please write the below code.
var titleSize = CGRectMake(0, 30, 40 , 1000)
self.contentCoverScrollView.frame = titleSize
[self.contentCoverScrollView updateConstraintsIfNeeded];
Note : updateConstraintsIfNeeded will tell the system to recalculate the constraints in that particular moment of execution.
Look like you want to change ScrollView size use:
[self.contentCoverScrollView setContentSize:(CGSizeMake(40, 1000))];
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.
I'm using autolayout on iOS7 and I have a problem like this:
I'm putting a UILabel onto a UIView and I'm arranging my autolayout constraints so that the label's centerX = parent view's centerX. I'm not giving any width constraint to the label. When I set the label's text on runtime, the label is drawn just wide enough for the text to fit, there are no margins/paddings on the left and right sides. What I want is to have some padding on the left and right sides, so that the text doesn't begin just where the label begins. The hack to achieve this could be setting the text as #" text " but of course that's not the way to go :)
How can I achieve what I want?
You can extend UILabel and override the intrinsicContentSize by yourself. Please make sure you have set the textAlignment = NSTextAlignmentCenter as well.
-(CGSize)intrinsicContentSize{
CGSize contentSize = [super intrinsicContentSize];
return CGSizeMake(contentSize.width + 50, contentSize.height);
}
Swift 5.0
open override var intrinsicContentSize: CGSize {
let size = super.intrinsicContentSize
return CGSize(width: size.width + 16, height: size.height)
}
This probably only works when you only have just one line of text to display.
You can create a UILabel subclass and override intrinsicContent,
-(CGSize)intrinsicContentSize {
CGSize s = [super intrinsicContentSize];
s = CGSizeMake(s.width + 20, s.height);
return s;
}
This will add a padding of 20 points to the width. If you want your text in the middle, be sure to set the text alignment to center.
If you're using auto layout, you can set the horizontal constraints and use an NSDictionary in the metrics parameter to set this dynamically.
For instance, if you wanted to give a 10pt padding to the inner content of a UIButton, you could do something like the following:
NSDictionary *padding = #{ #"padding" : #(button.intrinsicContentSize.width + 20) };
NSArray *buttonHConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|[button(==padding)]|" options:0 metrics:padding views:NSDictionaryOfVariableBindings(button)];
If you are trying to give the UILabel a different colour to its parent view, then you will need to enclose the UILabel in a UIView with the padding and the background colour you want.
If your UILabels background colour is the same as its parent view, then I don't understand the problem just use auto layout to specify how much space you want relative to the thing it is next to.