代码之家  ›  专栏  ›  技术社区  ›  memmons

计算适合矩形的最大字体大小?

  •  8
  • memmons  · 技术社区  · 15 年前

    我试图找到一个给定字符串的最大字体大小。算法的目标是用尽可能大的字体填充尽可能多的矩形。我的方法——从我在网上找到的方法修改而来——做得不错,但它通常不会填满整个rect。我很想看到一些关于如何改进这个算法的合作,以便每个人都能从中受益:

    -(float) maxFontSizeThatFitsForString:(NSString*)_string 
                                   inRect:(CGRect)rect 
                                 withFont:(NSString *)fontName 
                                 onDevice:(int)device 
    {   
    
        // this is the maximum size font that will fit on the device
        float _fontSize = maxFontSize;
        float widthTweak;
    
        // how much to change the font each iteration. smaller
        // numbers will come closer to an exact match at the 
        // expense of increasing the number of iterations.
        float fontDelta = 2.0;
    
        // sometimes sizeWithFont will break up a word 
        // if the tweak is not applied. also note that 
        // this should probably take into account the
        // font being used -- some fonts work better
        // than others using sizeWithFont.
        if(device == IPAD)
            widthTweak = 0.2;
        else
            widthTweak = 0.2;
    
        CGSize tallerSize = 
               CGSizeMake(rect.size.width-(rect.size.width*widthTweak), 100000);
        CGSize stringSize = 
               [_string sizeWithFont:[UIFont fontWithName:fontName size:_fontSize]
                   constrainedToSize:tallerSize];
    
        while (stringSize.height >= rect.size.height)
        {       
            _fontSize -= fontDelta;
            stringSize = [_string sizeWithFont:[UIFont fontWithName:fontName 
                                              size:_fontSize] 
                                 constrainedToSize:tallerSize];
        }
    
        return _fontSize;
    }
    
    3 回复  |  直到 12 年前
        1
  •  4
  •   NNikN    13 年前

    使用以下方法计算给定矩形和字符串的字体大小。

    您可以将字体更改为所需的字体。 此外,如果需要,可以添加默认字体高度;

    方法是不言而喻的。

    -(UIFont*) getFontTofitInRect:(CGRect) rect forText:(NSString*) text {
           CGFloat baseFont=0;
            UIFont *myFont=[UIFont systemFontOfSize:baseFont];
            CGSize fSize=[text sizeWithFont:myFont];
            CGFloat step=0.1f;
    
            BOOL stop=NO;
            CGFloat previousH;
            while (!stop) {
                myFont=[UIFont systemFontOfSize:baseFont+step ];
                fSize=[text sizeWithFont:myFont constrainedToSize:rect.size lineBreakMode:UILineBreakModeWordWrap];
    
                if(fSize.height+myFont.lineHeight>rect.size.height){           
                    myFont=[UIFont systemFontOfSize:previousH];
                    fSize=CGSizeMake(fSize.width, previousH);
                    stop=YES;
                }else {
                     previousH=baseFont+step;
                }
    
                step++;
        }
        return myFont;
    
     }
    
        2
  •  0
  •   Steffen D. Sommer    11 年前

    没有必要浪费时间做循环。首先,在最大和最小字体点设置下测量文本宽度和高度。根据更严格的限制,宽度或高度,使用以下数学公式:

    如果宽度更严格(即。, maxPointWidth / rectWidth > maxPointHeight / rectHeight

    pointSize = minPointSize + rectWidth * [(maxPointSize - minPointSize) / (maxPointWidth - minPointWidth)]
    

    否则,如果高度限制使用:

    pointSize = minPointSize + rectHeight * [(maxPointSize - minPointSize) / (maxPointHeight - minPointHeight)]
    
        3
  •  0
  •   gnasher729    11 年前

    可能不可能完全填充矩形。

    假设在一定的字体大小下,你有两行文字,水平地填充屏幕,而垂直地,你几乎没有三行空格。

    所以你别无选择,只能忍受垂直差距。