My Xcode 5 project has a label where the User enters a string of numbers. That string is then sent to one of two other labels (either labelA or labelB) by the User pressing one of two buttons. Font size in both labelA and labelB are dynamically resized (if necessary) using:
_labelA.numberOfLines = 1;
_labelA.minimumScaleFactor = 8./_inchesDisplayLabel.font.pointSize;
_labelA.adjustsFontSizeToFitWidth = YES;
and:
_labelB.numberOfLines = 1;
_labelB.minimumScaleFactor = 8./_inchesDisplayLabel.font.pointSize;
_labelB.adjustsFontSizeToFitWidth = YES;
If a string in labelA gets resized to fit within the label, how can the font size in labelB be programmatically resized to match the smaller resized font in labelA? Thanks!
After labelA's font gets resized, use this to make labelB's font match labelA's:
[labelB setFont:[UIFont fontWithName:labelA.font.fontName size:labelA.font.pointSize]];
Related
I have a label called ratingstext and I want its contents to fit into a single line of text by means of adjusting height in case of multiple lines. How do I achieve that in iOS Objective -C.
My label:
as you can see the label shows 200/20 and is called ratingstext label. Actually the value is 200/200 but since I have used
self.ratingsText.textContainer.maximumNumberOfLines = 1;
The last 0 value gets chopped off due to fixed label width. How do I auto adjust width or height to fit in the content in a single line?
My code is:
self.ratingsText = [[UITextView alloc]initWithFrame:self.frame];
self.ratingsText.editable = NO;
self.ratingsText.text = #"000/00";
self.ratingsText.textContainer.maximumNumberOfLines = 1;
self.ratingsText.font = [UIFont systemFontOfSize:self.ratingSize weight: UIFontWeightRegular];
[self.ratingsText sizeToFit];
self.ratingsText.backgroundColor = UIColor.clearColor;
[self addSubview:self.ratingsText];
If you are using a storyboard autoLayout will expand the frame to match the size of the text input. In the Attributes Inspector make the width textLabel "Relation" to "Less Than or Equal". I gave the text label in the storyboard 200/20 and in code set it to 200/200. Auto Layout took care of the rest.
Use the below properties:
adjustsFontSizeToFitWidth
contentScaleFactor
minimumFontSize
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
}
}
I have a universal app which i used the story board to constrain a button so that from the iPhone 4 to the iPhone 6plus, the size will be well proportioned to the device's screen. The title of the button is the number "2" (All that is shown on the screen is the number "2" which can be pressed). For some reason I cannot figure out how to auto resize the font with the button size so that the number looks proportioned to the screen as intended.
Ideas?
Use Interface Builder only. You can add an UILabel as the same size as your button. Remove the title of Your button. Set text for the UILabel. Then change the AutoShrink property of the UILabel to "Minimum Font Scale". But you will not able to change the text color for different states.
Use code to fit width. Try this:
self.yourButton.titleLabel.adjustsFontSizeToFitWidth = YES;
self.yourButton.titleLabel.minimumScaleFactor = 0.5;
If you want to fit the height of title. Maybe you must calculate the height by yourself. Use [font lineHeight] to make sure the font fit your button.
Call this function (from e.g. viewDidAppear) for every button in your app:
func setAppropriateFontSize( butt:UIButton){
let maxFontSize = butt.frame.height
butt.titleLabel!.font = UIFont(name: "Helvetica", maxFontSize)
butt.titleLabel!.adjustsFontSizeToFitWidth = true
butt.titleLabel!.minimumScaleFactor = 0.01
}
Above, maxFontSize is the maximum font-size for which the text still fits vertically within the button. This font is probably too large, and therefore we allow it to shrink (adjustsFontSizeToFitWidth = true means that we allow the font to shrink, as it will never increase to fit the width).
My view controller has a UILabel. It displays a dollar amount to the user. I have a default size that I like the dollar amount to be displayed as, shown below
When the amount is too large, some text is replaced with "..."
I want to adjust the text of the UILabel (make it smaller) such that the point size of the text is just enough to not cause the dots to appear. Is there some quick math I can do to accomplish this?
label.adjustsFontSizeToFitWidth = YES;
label.numberOfLines = 1;
No need for you to make the math, the system-provided API can do it for you. In Interface Builder, set the label's autoshrink to minimum scale factor of 0.7 (or whatever value fits your need). Text will now shrink as necessary.
Use this:
UILabel * label = // your label
// update from #rmaddy and #z s
label.adjustsFontSizeToFitWidth = YES;
label.minimumScaleFactor = 0.5;
This means that you'll allow your text to shrink to up to half its size before truncating. Adjust accordingly.
If you're working in a pre iOS 7 environment, you can also use:
UILabel * label;
label.minimumFontSize = 14;
To set a specific minimum font size; however, this has been deprecated since iOS 7
So I have a custom table view cell and I want a label to be resized based on the text in it. I've already tried doing this in the cellForRowAtIndexPath method-
cell.titleLabel.adjustsFontSizeToFitWidth = YES;
cell.titleLabel.numberOfLines = 1;
[cell.titleLabel setMinimumScaleFactor:8.0/[UIFont labelFontSize]];
I also tried setting minimum scale factor and minimum font size in IB...but the label font doesn't resize.
The case I want to take care of is the case where the text in the label is larger than the phone width and so I want to resize when that happens.
What's going wrong/How do I achieve that?