views:

50

answers:

2

alt text

I have a UIButton of fixed width and I want to place text in it. Since the size of the String is unknown in advance, I will have to resize the font as I read the String from a flat file. How can I do that? A function similar to the following will be great:

UIFont resizeFontAs:(UIFont)initialFont andStringAs:(NSString*)string andWidthAs:(int)width

Thanks in advance

Edit : This code doesnt seem to work:

// Method for creating button, with background image and other properties
- (UIButton *) getCallAgentButtonWithTitleAs:(NSString *)aTitle andImageAs:(UIImage*)bgImg atIndex:(int)index{    
    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
    aButton.frame = CGRectMake(10, 2+index*50, 300, 48);    
    aButton.titleLabel.frame = CGRectMake(aButton.titleLabel.frame.origin.x + 25.0,                   aButton.titleLabel.frame.origin.y, aButton.titleLabel.frame.size.width - 50.0,     aButton.titleLabel.frame.size.height);     
    aButton.titleLabel.adjustsFontSizeToFitWidth = YES;
    [aButton setTitle:aTitle forState:UIControlStateNormal];
    //aButton.titleLabel.font = [UIFont boldSystemFontOfSize:12];   
    [aButton setTitleColor:UIColorFromRGB(0xFDD428) forState:UIControlStateNormal];
    [aButton setBackgroundImage:bgImg forState:UIControlStateNormal];
    [aButton addTarget:self action:@selector(callAgent:) forControlEvents:UIControlEventTouchUpInside];
    // set the tag as the index and use it later to obtain the phoneNo
    [aButton setTag:index];
    return aButton;  
}
+3  A: 

You can tell the button's label to resize the font itself.

myButton.titleLabel.adjustsFontSizeToFitWidth = YES;
myButton.titleLabel.minimumFontSize = 6.0; // Pick your own value here.
Robot K
Yep, gotta give that a +1.
jlehr
Thanks a lot. okay, there is one more complication ... i have some fixed size graphics on either edge of the button. say the button is 300px, the graphics each are 25px each. The problem with auto resizing is that the text comes over the graphics. Any ways around it?
Amarsh
Are the graphics part of the button's view, or are they siblings of the button?
Robot K
The adjustsFontSizeToFitWidth shouldn't change the width of the label, only the size of the font it displays.
Robot K
It sounds like you need to change the frame of the label. Something like this: `myButton.titleLabel.frame = CGRectMake(myButton.titleLabel.frame.origin.x + 25.0, myButton.titleLabel.frame.origin.y, myButton.titleLabel.frame.size.width - 50.0, myButton.titleLabel.frame.size.height);` This should (if I've typed it correctly) decrease the width of the label by 50 points and move it 25 points to the right, making room for your images.
Robot K
It doesnt seem to resize. See the screenshot and the code i am using.
Amarsh
Huh. It doesn't look like you can resize the label in a button, at least not in a straightforward way. Give me a minute.
Robot K
You need to adjust titleEdgeInsets (https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/doc/uid/TP40006815-CH3-SW20). Use this code: `myButton.titleEdgeInsets = UIEdgeInsetsMake(0, 25, 0, 25);'
Robot K
@Robot : You rock :) WOnder why Apple has made is so complicated. Perhaps UIButton doesnt encapsulate UILabel, or it overrides UILabel settings while composing the button. Surprisingly the docs say : Although this property is read-only, its own properties are read/write. Use these properties to configure the appearance of the button label.
Amarsh
Thanks! My guess (and it's pure speculation) is that UIButton manages its own layout and drawing, and it's too expensive to allow for arbitrary frames on the titleLabel.
Robot K
+2  A: 

Use the following method:

-  (CGSize)sizeWithFont:(UIFont *)font
            minFontSize:(CGFloat)minFontSize
         actualFontSize:(CGFloat *)actualFontSize
               forWidth:(CGFloat)width
          lineBreakMode:(UILineBreakMode)lineBreakMode

That'll allow you to specify both a default size (the first arg) and a minimum size, and the font will be automatically scaled if necessary.

jlehr
I like Robot K's solution better. :-)
jlehr
Thanks :). I considered this approach initially, but it doesn't return what font size was finally used to compute the size, which is what I think Amarsh is looking for.
Robot K
That's the actualFontSize out parameter, no?
Ken
D'oh! I totally missed that it was an out parameter. I thought you passed in your 'ideal' size, which in retrospect makes no sense because the UIFont has a size property. I need to pay better attention to the documentation.
Robot K
Yep, which is why it's typed `CGFloat *` instead of just `CGFloat`.
jlehr
@jlehr: I will try this one out later. Thanks a lot for the reply,
Amarsh