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).
Related
I need a button to set up via autolayout which height will increase if the horizontal space is not enough i.e. after rotation. I have a leading and trailing constraint to the parent view, and in portrait mode there is lack of space, so I was thinking about not to reduce font size, but increase height and set Word Wrap for Line Break. I was experimenting size classes, but in somehow always the wCompact hAny is used, because I am testing in iPhone. So to set different height for button for different size classes had no effect.
Anybody has idea how to set depending button height on the available button width and the content via autolayout constraint?
On iPhone button below needs two lines, not 30px, but appr. 60px.
Why are you using a button? This is not what a button is for. If you have that amount of text to display then use either a UILabel or UITextView. You can then add a gesture recogniser to it to capture the tap.
You can use UILabel and can use UITapGestureRecognizer on it. and set number of lines=0 in nib file and have one action of tap gesture. it will work like a button with almost no code as u want
Because I am suspicious it is not possible with Interface Builder I made it with subclassing an UIButton:
class DynamicHeightButton: UIButton {
override func layoutSubviews() {
super.layoutSubviews()
let size = (self.titleForState(UIControlState.Normal)! as NSString).boundingRectWithSize(CGSizeMake(self.bounds.size.width, CGFloat.max), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName : UIFont.systemFontOfSize(17)], context: nil)
self.bounds.size.height = size.height + 8
}
}
I am working on auto layouts and at the same time i need to increase the font size of my button title .I have written a method that increases the font sizes of my UI controls.But when it comes to UI buttons on my view the fonts gets increased but the frame of the button is not increased to accommodate the font size.
Is there any property for UIbutton like sizethatfits that i can use for my button so that it resizes its frame to accommodate the increased font size ?
Any help will be appreciated.
thanks
button.titleLabel.numberOfLines = 1; <--or any other number
button.titleLabel.adjustsFontSizeToFitWidth = YES;
button.titleLabel.lineBreakMode = NSLineBreakByClipping;
try this and if you still had a problem let me know!
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]];
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?