【问题标题】:Title and subtitle with different fontsize for UIButtonUIButton 不同字体大小的标题和副标题
【发布时间】:2011-09-03 07:22:13
【问题描述】:

我需要一个UIButton 来显示两行文本,其中第一行用作标题,第二行用作某种副标题,因此它应该具有较小的字体大小。 现在我的按钮有两行,自定义字体,标题/副标题的内容分别来自UILabel

UIFont *displayFont = [UIFont fontWithName:@"Chalkboard" size:15];
NSString *buttonLabel;
UILabel *headline = [[UILabel alloc] init];
headline.text = @"This is the title";
UILabel *subtitle = [[UILabel alloc] init];
subtitle.text = @"this is the sub";

buttonLabel = [NSString stringWithFormat:@"%@\n%@", headline.text, subtitle.text];
[openDetail.titleLabel setLineBreakMode:UILineBreakModeWordWrap];
[openDetail setTitle:buttonLabel forState:UIControlStateNormal];
[openDetail.titleLabel setTextAlignment:UITextAlignmentCenter];
openDetail.titleLabel.font = displayFont;

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

知道如何在第二行中实现更小的字体吗?

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

【问题讨论】:

    标签: iphone objective-c uibutton font-size line-breaks


    【解决方案1】:

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

    如果您想要更复杂的按钮,您必须自己构建它们。

    您的一个选择是将字幕添加到现有按钮。这是一个简单的例子。我假设你有一个名为 myButton 的 IB 按钮出口。

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

    如果您在多个地方需要此功能,您应该将按钮变成它自己的类,并为标题和副标题提供很好的属性。其实无论如何你都应该这样做。 :-)

    【讨论】:

      【解决方案2】:

      试试这个

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

      【讨论】:

      • 我这样做的方法是创建一个 UIButton 的子类,其中包含两个 UILabel。所以,如果你需要重用这个按钮,你不需要重写所有的代码。此外,您可以创建诸如 setTitle:forState: 之类的方法。
      • @Raphael Petegrosso 我不确定如何子类化 UIButton,但这似乎比仅仅在按钮顶部添加标签(我现在使用 IB 和 UIFont 所做的)更干净。我读过 UIButton 不应该被子类化的地方。你以前做过吗?
      • 是的,你可以继承 UIButton。我总是这样做没有问题。
      猜你喜欢
      • 2016-08-23
      • 1970-01-01
      • 1970-01-01
      • 2015-08-07
      • 2019-05-27
      • 2014-10-28
      • 2013-06-23
      • 2021-12-25
      • 2011-08-28
      相关资源
      最近更新 更多