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

目标C求字符串的像素宽度

  •  1
  • Amarsh  · 技术社区  · 14 年前

    我有一个固定宽度的ui按钮,我想在其中放置文本。因为字符串的大小事先未知,所以在从平面文件读取字符串时,我必须调整字体的大小。我该怎么做?类似于以下功能的功能将非常好:

    uiFONT resizefontas:(uiFONT)initialFONT andstringas:(nsstring*)string andwidthas:(int)width
    < /代码> 
    
    

    提前谢谢

    编辑:此代码似乎不起作用:

    创建按钮的方法,具有背景图像和其他属性 -(uibutton*)带标题的getcalagentbuttons:(nsstring*)标题和图像:(uiimage*)bgimg atindex:(int)索引{ uibutton*abuton=[uibutton buttonWithType:uibuttonTypeRoundedRect]; abuton.frame=cDirectmake(10,2+索引*50,300,48); abutton.titlelabel.frame=cDirectmake(abutton.titlelabel.frame.origin.x+25.0,abutton.titlelabel.frame.origin.y,abutton.titlelabel.frame.size.width-50.0,abutton.titlelabel.frame.size.height); //abuton.titlelabel.font=[uifont-boldSystemFontOfSize:12]; [abuton settitlecolor:uicolorfromRGB(0xFDD428)for state:uicontrol状态正常]; [Abuton BackgroundImage:bgimg for State:uicontrol状态正常]; [abuton addtarget:self action:@selector(callagent:)forcontrolevents:uicontroleventTouchupInside]; //将标记设置为索引,稍后使用它获取电话号码 [abuton settag:索引]; 返回阿布顿; } < /代码> <

    我有一个固定宽度的ui按钮,我想在其中放置文本。因为字符串的大小事先未知,所以在从平面文件读取字符串时,我必须调整字体的大小。我该怎么做?类似于以下功能的功能将非常好:

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

    提前谢谢

    编辑:此代码似乎不起作用:

    // 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;  
    }
    
    2 回复  |  直到 14 年前
        1
  •  5
  •   Kris Markel    14 年前

    myButton.titleLabel.adjustsFontSizeToFitWidth = YES;
    myButton.titleLabel.minimumFontSize = 6.0; // Pick your own value here.
    
        2
  •  2
  •   jlehr    14 年前

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