I am trying to programmatically manipulate a UILabel so that it has a dynamic width.
I found that using sizeToFit() does this exact behavior.
Now as this label becomes my navigationItem.titleView. I want it to have a maximum size, which can't be extended.
It is important to fit 2 barButtonItems in the navigationItem.
For instance: a backBarButton on the left, an addButton on the right.
Is there a way to change the sizeToFit() method?
You can use sizeThatFits: see docs here.
You have to specify a CGSize that you want to fit in.
to set maximum width use preferredMaxLayoutWidth than set the maximum size like that bounds.width - 120
for example:
yourLabel.sizeToFit()
yourLabel.numberOfLines = 0
yourLabel.preferredMaxLayoutWidth = bounds.width - 120
Related
I have a UILabel that I want to expand vertically rather than horizontally after some maximumWidth. Currently I am trying to do this by doing the following:
_caption.numberOfLines = 0;
_caption.lineBreakMode = NSLineBreakByWordWrapping;
and then when I size I use sizeToFit
However, I always have a width that is much larger than my maximumWidth.
I tried using preferredMaxLayoutWidth but this did not work.
I could do a simple calculation by dividing the width by the maximumWidth that I want and then adjust the height accordingly but I'm wondering if there is any way to do this automatically.
You need to use NSLineBreakByCharWrappinginstead of NSLineBreakByWordWrapping
_caption.lineBreakMode = NSLineBreakByCharWrapping
I had tested this using a simple label and storyboard and setting the constraints to top, left and width and here is the result
I have a UILabel and I want to show some text in this label. I want to increase the label width at most 70% of the full screen of my device. If text length of that label doesn't fit this 70% of size then the label automatically goes to the next line as long as the text length. Every time the label length cross the 70% width of main screen then lines break as well. I have tried several ways but unable to solve yet. Please help me to solve this.
Thanks in advance;
Drag a label to your storyboard and add top and leading constraints to it.
Now select the label and control drag to the view holding the label (in your case view of ViewController) you will see the pop up and then select equal width
Now your Label's width is equal to your view's width :) That's not you want you want ur label width to be 70% of your view. So select the equal constraint of label, go to property inspector and change the multiplier to 0.7
Now your label width is 70% of your view!
But you don't want it to be 70% always. It can be at max 70% of screen, so
now change the relationship of constraint from being equal to less than or equal to.
select label and change number of lines to 0.
That's it :) have fun :)
Sample O/P:
When text is short - vs - long:
- - -
EDIT:
Not using a storyboard? Not a problem; write the same constraint programmatically and apply it to label simple enough. If you need help lemme know :)
EDIT:
As you have specified that you want to leave the gap at the beginning of each line in label you can achieve it by using Edge insets
- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = {0, 5, 0, 0};
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
You must have forgotten to increase the label's height.
The code below is for allowing the UILabel to have multiple lines:
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;
Then you have to make sure the UILabel's frame has enough height to show the lines. You can achieve this by calculating the required height for the given text (NSString):
NSString *text = #"YourText";
CGFloat your70Width; // whatever your width is
CGSize constraintSize = CGSizeMake(your70Width, MAXFLOAT);
UIFont *yourLabelFont; // whatever your font is
CGRect requiredFrame = [text boundingRectWithSize:constraintSize options:NSStringDrawingUsesFontLeading attributes:#{NSFontAttributeName:yourLabelFont} context:nil];
// Keeps the old x,y coordinates and replaces only the width and height.
CGRect oldLabelFrame = label.frame;
label.frame = CGRectMake(oldLabelFrame.origin.x, oldLabelFrame.origin.y, requiredFrame.size.width, requiredFrame.size.height);
Now the label will be shown nicely in multiple lines.
To increase the height of the label according to the content if you are using storyboard. Give the label FOUR constraints (Top, Bottom, Leading, Trailing) then go to Attribute Inspector and make lines to 0 and in line break do it WORD WRAP.
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.
#IBAction func sizeChanged(sender: UISlider) {
let senderValue = CGFloat(sender.value)
myLabel?.font = UIFont(name: (myLabel?.font.fontName)!, size:senderValue * 20)}
I want to change myLabel.font size with a slider, but myLabel does not change adjust its width and height as the font size increases.
How do I change the UILabel size to follow its font size?
Thanks.
After updating the font size of the UILabel, you'll want to call sizeToFit().
myLabel?.sizeToFit()
According to the UIView Class Reference,
Call this method when you want to resize the current view so that it uses the most appropriate amount of space. Specific UIKit views resize themselves according to their own internal needs. In some cases, if a view does not have a superview, it may size itself to the screen bounds. Thus, if you want a given view to size itself to its parent view, you should add it to the parent view before calling this method.
myLabel?.sizeToFit() will set the size of label to fit its content...just make sure you haven't added height and width constraints for the label.
I am making a app without using storyboard. The app has a long text so I can't get enought space on one line. The app is for both iPad and iPhone. The adjustSizeToFit = true does not work, is there a metode to adjust size of label with multiple lines?
There is no property on UILabel called adjustSizeToFit. Are you sure you didn't mean adjustsFontSizeToFitWidth? Which if you look at the documentation, says:
Normally, the label text is drawn with the font you specify in the font property. If this property is set to true, however, and the text in the text property exceeds the label’s bounding rectangle, the receiver starts reducing the font size until the string fits or the minimum font size is reached. In iOS 6 and earlier, this property is effective only when the numberOfLines property is set to 1.
Which I'm not sure is what you wanted.
If you wanted a UILabel with an arbitrary number of lines, where the text is contained within a certain width, continue reading:
What you do will depend on whether you're using AutoLayout or not:
Not AutoLayout
Just use:
let size = label.sizeThatFits(CGSize(width: myWidth, height: CGFloat.max))
// CGFloat.max, because we don't want to limit the UILabel's height.
label.frame.size = size
AutoLayout
Firstly, you should set numberOfLines to zero.
Secondly, you need to tell AutoLayout how long each line can be, this doesn't default to the width of the label. For this you need a UILabel subclass:
class myLabel : UILabel {
override func layoutSubviews() {
// 1. Get the label to set its frame correctly:
super.layoutSubviews()
// 2. Now the frame is set we can get the correct width
// and set it to the preferredMaxLayoutWidth.
self.preferredMaxLayoutWidth = self.frame.width
}
}