0

我正在尝试按照以下方式设置字体,label但它给了我零CGSize

UIFont *abbrFont = [UIFont fontWithName:@"Helvetica Cyrillic Bold_5" size:50]; //Helvetica Cyrillic Bold_5 added Custom Font

CGSize abbrSizeOfString = [_addbrTitle sizeWithFont:abbrFont];//_addbrTitle is a NSString
NSLog(@"%f %f",abbrSizeOfString.width,abbrSizeOfString.height); //Everytime Prints (0.000,0.000)

帮我解决这个问题。

谢谢你。

4

6 回答 6

1

尝试这个,

UIFont *abbrFont = [UIFont fontWithName:@"Helvetica" size:50]; //Helvetica Cyrillic Bold_5 added Custom Font
CGSize abbrSizeOfString = [_addbrTitle.text sizeWithFont:abbrFont];//_addbrTitle is a Label
NSLog(@"%f %f",abbrSizeOfString.width,abbrSizeOfString.height); //E
于 2013-09-16T08:00:33.820 回答
0

您可以使用此行来设置标签的大小..

[label setFont:[UIFont fontWithName:@"Helvetica" size:20.0]];
于 2013-09-16T07:50:49.067 回答
0
NSURL *fontURL = [NSURL URLWithString:[[NSBundle mainBundle]pathForResource:@"your_font_name"  ofType:@"TTF"]];

例如:

NSURL *fontURL2 = [NSURL URLWithString:[[NSBundle mainBundle]pathForResource:@"segoe_semi_bold" ofType:@"TTF"]];

 NSURL *fontURL2 = [NSURL URLWithString:[[NSBundle mainBundle]pathForResource:@"segoe_semi_bold" ofType:@"TTF"]];

现在将 url 添加到数组

NSArray *arrFont =[[NSArray alloc]initWithObjects:fontURL1,fontURL2, nil];


int result =CTFontManagerRegisterFontsForURLs((CFArrayRef)arrFont, kCTFontManagerScopeUser, nil);
if(result)
{
  NSLog("Font Install successfully");
}

现在你可以得到字体

于 2013-09-16T11:49:17.410 回答
0
UILabel *_addbrTitle = [[UILabel alloc] init];
[_addbrTitle setText:@"Hello"];
UIFont *abbrFont = [UIFont fontWithName:@"Helvetica" size:50]; //Helvetica Cyrillic Bold_5 added Custom Font
[_addbrTitle setFont:abbrFont];
CGSize abbrSizeOfString = [_addbrTitle.text sizeWithFont:abbrFont];//_addbrTitle is a Label
NSLog(@"%f %f",abbrSizeOfString.width,abbrSizeOfString.height); //Everytime Prints (0.000,0.000)

您在代码中输入的字体名称在系统中不存在。所以它每次都打印(0,0)。输入系统中存在的字体名称,它将打印大小。

于 2013-09-16T07:58:08.210 回答
0

将您的字体名称更改为其他内置字体名称,然后尝试这样

yourLabel.font=[UIFont fontWithName:@"Helvetica" size:16];
于 2013-09-16T07:58:35.553 回答
0

您的字体名称不是当前的,请遵循此 IOS 支持字体列表和字体名称

http://iosfonts.com/

于 2013-09-16T08:55:38.270 回答