Iphone UIButton标题和副标题的字体大小不同

Iphone UIButton标题和副标题的字体大小不同,iphone,objective-c,uibutton,font-size,line-breaks,Iphone,Objective C,Uibutton,Font Size,Line Breaks,我需要一个UIButton来显示两行文本,第一行作为标题,第二行作为某种字幕,所以它的字体应该更小。 现在,我的按钮有两行,自定义字体,标题/副标题的内容来自UILabel 我尝试将标签本身设置为displayFont,这样我就可以制作第二个UIFont,制作它的副标题。不幸的是,按钮标签将不是这种字体,如果我保持上面所示的其余部分,但将切换回Arial,我想是的,并且根本没有更改字体大小 你知道我如何在第二行实现更小的字体大小吗 字体的附带问题:我将Chalkboard.ttc添加到我的资源中

我需要一个UIButton来显示两行文本,第一行作为标题,第二行作为某种字幕,所以它的字体应该更小。 现在,我的按钮有两行,自定义字体,标题/副标题的内容来自UILabel

我尝试将标签本身设置为displayFont,这样我就可以制作第二个UIFont,制作它的副标题。不幸的是,按钮标签将不是这种字体,如果我保持上面所示的其余部分,但将切换回Arial,我想是的,并且根本没有更改字体大小

你知道我如何在第二行实现更小的字体大小吗

字体的附带问题:我将Chalkboard.ttc添加到我的资源中,并将其作为应用程序提供的字体添加到我的info.plist中。不过,如果我尝试在IB中设置字体,我会看到另一种与Helvetica非常相似的字体。我真的必须现在以编程方式设置每个按钮字体吗?

试试这个

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

UILabel *subtitle = [[UILabel alloc]initWithFrame:CGRectMake(8, 20, 185, 30)];
[subtitle setBackgroundColor:[UIColor clearColor]];
[subtitle setFont:[UIFont fontWithName:@"Helvetica" size:14]];
subtitle.text=@"This is the sub";
[subtitle setTextColor:[UIColor blackColor]];
[btn    addSubview:subtitle];

[title  release];
[subtitle release];

[self.view addSubview:btn];

您的问题是,您正在使用的系统按钮中只有一个标签。而且只能有一种字体

如果你想要更复杂的按钮,你必须自己制作

您的一个选择是将字幕添加到现有按钮。下面是一个简单的例子。我想您的IB按钮有一个插座,叫做myButton

UILabel *subTitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, 20, 100, 30)] autorelease];
[subTitle setText:@"Subtitle"];
[subTitle setTextColor:[UIColor blackColor]];
[subTitle setBackgroundColor:[UIColor clearColor]];
[subTitle setFont:[UIFont fontWithName:@"American Typewriter" size:12]];

[myButton setTitle:@"Main title" forState:UIControlStateNormal];
[myButton addSubview:subTitle];

如果这是您在多个地方需要的东西,您应该将按钮转换为它自己的类,并为标题和副标题提供良好的属性。事实上,无论如何你都应该这样做-

我的方法是创建一个子类UIButton,里面有两个UILabel。所以,如果你需要重用这个按钮,你不需要重写所有的代码。此外,您还可以创建setTitle:forState:。@Raphael Petegrosso之类的方法。我不知道如何对UIButton进行子类化,但这似乎是一个比现在使用IB和UIFont在按钮顶部添加标签更干净的解决方案。我读过UIButton不应该被子类化的地方。你以前做过吗?是的,你可以子类UIButton。我总是毫无问题地做这件事。
UILabel *subTitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, 20, 100, 30)] autorelease];
[subTitle setText:@"Subtitle"];
[subTitle setTextColor:[UIColor blackColor]];
[subTitle setBackgroundColor:[UIColor clearColor]];
[subTitle setFont:[UIFont fontWithName:@"American Typewriter" size:12]];

[myButton setTitle:@"Main title" forState:UIControlStateNormal];
[myButton addSubview:subTitle];