Objective C 查找字符串的像素宽度
我有一个固定宽度的 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;
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以告诉按钮的标签来调整字体本身的大小。
You can tell the button's label to resize the font itself.
使用以下方法:
这将允许您指定默认大小(第一个参数)和最小大小,并且如果需要,字体将自动缩放。
Use the following method:
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.