UIButton文本大小问题

4
我有以下代码:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"My Title" forState:UIControlStateNormal];
button.frame = myCustomRect;
UIColor *fontColor = [UIColor colorWithRed:0.43 green:0.84 blue:0.86 alpha:0.9];
[button setTitleColor:fontColor forState:UIControlStateNormal];
button.backgroundColor = [UIColor whiteColor];
button.titleLabel.textAlignment = NSTextAlignmentCenter;
button.titleLabel.font = [UIFont fontWithName:@"Cooperplate" size:9];
[button addTarget:self
                action:@selector(moreDetails:)
 forControlEvents:UIControlEventTouchUpInside];    
[self.view addSubview:button];

我正在尝试在视图中以编程方式添加按钮。 MyCustomRect的参数为(0,0,75,50)。
两件事: a)无论我输入什么字体,它始终显示系统字体。 b)大小也根本不改变。我尝试增加字体大小,它不会改变,我尝试减小字体大小,它也不会改变。
我做错了什么?

1
这意味着你的字体“Cooperplate”没有被添加到你的应用程序中。请检查是否已将其添加到你的捆绑包和属性列表中。 - Ramaraj T
4个回答

3

您可以在项目中使用自定义字体,如此处所述

一旦将自定义字体添加到您的项目中,您可以使用它,如下:

[button.titleLabel setFont:[UIFont fontWithName:@"Cooperplate" size:24.0]];

首先,在XCode中找到应用程序的.plist文件。通常称为[YOUR_APP_NAME]-Info.plist。一旦您找到了该文件,添加一个新的键:"Fonts provided by application"。XCode将为您创建第一个键/值对。这是我的.plist文件条目的屏幕截图。

图片

在“value”列中添加字体文件的名称。确保拼写字体文件的名称与项目文件列表中看到的完全相同。这不是您在UIFont语句中引用的名称。这只是让您的应用程序知道在哪里找到您将要引用的字体。
接下来,您需要使用字体的名称。现在您可以在应用程序中引用它了。

2

对于UIButton,试试这个方法:

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(20 , 13, 200, 85)];
UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(8, 8, 185, 30)];
[title setBackgroundColor:[UIColor clearColor]];
[title setFont:[UIFont fontWithName:@"Chalkboard" size:14]];
[title setFont:[UIFont boldSystemFontOfSize:18]];
title.text=@"This is the title";
[title setTextColor:[UIColor blackColor]];
[btn addSubview:title];
[title release];
[self.view addSubview:btn];

1

尝试使用这个

[button.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:13.0]];

1
那个可行。这可能意味着“Cooperplate”字体与UIButton的标签不兼容。我能在哪里找到UIButton兼容字体的列表? - BlueChips23

-1
在代码后面加入这行代码:这应该解决大小的问题。
button.titleLabel.font = [UIFont fontWithName:@"Cooperplate" size:9];
button.titleLabel.font = [UIFont systemFontOfSize:30];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接