Objective C 查找字符串的像素宽度

发布于 2024-09-26 05:35:46 字数 1468 浏览 3 评论 0原文

alt text

我有一个固定宽度的 UIButton,我想在其中放置文本。由于字符串的大小事先未知,因此当我从平面文件中读取字符串时,我必须调整字体大小。我怎样才能做到这一点?类似于以下的功能会很棒:

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;  
}

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;  
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

开始看清了 2024-10-03 05:35:46

您可以告诉按钮的标签来调整字体本身的大小。

myButton.titleLabel.adjustsFontSizeToFitWidth = YES;
myButton.titleLabel.minimumFontSize = 6.0; // Pick your own value here.

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.
吃兔兔 2024-10-03 05:35:46

使用以下方法:

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

这将允许您指定默认大小(第一个参数)和最小大小,并且如果需要,字体将自动缩放。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文