iOS: finding correct font size to fit in a UILabel depending on its size

会有一股神秘感。 提交于 2019-12-31 05:45:09

问题


I have a UILabel whose property text I would like to set paragraphs of text to using NSString.

I have an array in which I store a sequence of characters that represent a text paragraph.

The paragraph doesn't have to fit/included perfectly within this UILabel. If the paragraph doesn't end, I would move on to the next label.

Let say if I had rect size of this UILabel 160 X 240, how could I be able to determine the correct font size in order to fill this string of the UILabel within nicely?

Is there a mathematical way for the calculation of font size based upon the size of the rect on the screen?

For example:

UILabel *aLabel = [[UIlabel alloc] initwithFrame(CGRect){{0, 0}, 160, 240}];
aLabel.font = [UIFont fontWithName:@"Courier New" size:<???>]; //(font unit in points)

NSMutableString *aString = [[NSMutableString alloc] init];

//The following tries to fit the character one by one within the label rect based on   
//the width and height of the UILabel by appending String
int index = 0;
for(int x = 0; x < 240; x+=<???>) //height of label (pixel unit)
{
    for(int y = 0; y < 160; y+=<???>) //width of label (pixel unit)
    {
        [aString appendWithString:[paragraphArray objectAtIndex:index]];
        index++;
    }
    [aString appendWithString:@\n"];  //line break
}
aLabel.text = aString;

How can I be able to determine the values where the ??? are (namely the font size and the pixel size for the for loops)?

If this is not the optimal way to perform such a task, please kindly suggest me any other.


回答1:


+(void)resizeFontForLabel:(UILabel*)aLabel{

    // use font from provided label so we don't lose color, style, etc
    UIFont *font = aLabel.font;

    float lblWidth = aLabel.frame.size.width;
    float lblHeight = aLabel.frame.size.height;

    CGFloat fontSize = [font pointSize];
    UIFont *newFont = font;
    TRC_DBG(@"%@", aLabel.text);
    CGFloat height = [aLabel.text sizeWithFont:font constrainedToSize:CGSizeMake(lblWidth,MAXFLOAT) lineBreakMode:aLabel.lineBreakMode].height;

    TRC_DBG(@"Label Height %f Constraint height %f", lblHeight, height);
    //Reduce font size while too large, break if no height (empty string)
    while (height > lblHeight && height != 0) {
        fontSize--;
        newFont = [UIFont fontWithName:font.fontName size:fontSize];
        height = [aLabel.text sizeWithFont:newFont constrainedToSize:CGSizeMake(lblWidth,MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping].height;
        TRC_DBG(@"Constrained Height %f", height);
    };


    TRC_DBG(@"Font size before adjustment %f", aLabel.font.pointSize);
    // Set the UILabel's font to the newly adjusted font.
    aLabel.font = newFont;
    TRC_DBG(@"Adjust to font size of %f", newFont.pointSize);
    [aLabel setNeedsLayout];
}



回答2:


In storyboard click on adjust to fit and pick a minimum size.



来源:https://stackoverflow.com/questions/21751738/ios-finding-correct-font-size-to-fit-in-a-uilabel-depending-on-its-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!