【问题标题】:UIButton Text Size IssuesUIButton 文本大小问题
【发布时间】:2012-10-26 04:33:43
【问题描述】:

我有以下代码:

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)大小也根本没有变化。我尝试增加字体大小,它没有改变,我尝试减小字体大小,它也没有改变。

我做错了什么?

【问题讨论】:

  • 这意味着您的字体“Cooperplate”没有添加到您的应用程序中。检查您是否已将其添加到您的捆绑包和 plist 中。

标签: iphone objective-c ios xcode uibutton


【解决方案1】:

您可以在项目中使用自己的自定义字体mentioned here

在项目中添加自定义字体后,您可以将其用作,

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

首先,在 XCode 中找到应用程序 .plist 文件。通常称为, [YOUR_APP_NAME]-Info.plist。找到文件后,添加一个新行 键:“应用程序提供的字体”。 XCode 将创建第一个 为您提供键/值对。这是我的 .plist 文件条目的屏幕截图。

Image

在“值”列中添加字体文件的名称。一定要拼写 字体文件的名称与列表中所见的完全一致 项目中的文件。这不是您要引用的名称 从您的 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];
    

    【讨论】:

      【解决方案3】:

      试试这个

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

      【讨论】:

      • 那行得通。这可能意味着“Cooperplate”字体不适用于 UIButton 的标签。有没有什么地方可以找到 UIButton 的兼容字体列表?
      【解决方案4】:

      在后面使用这行代码:这应该可以解决大小问题

      button.titleLabel.font = [UIFont fontWithName:@"Cooperplate" size:9];
      button.titleLabel.font = [UIFont systemFontOfSize:30];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-16
        • 2020-07-18
        • 1970-01-01
        • 2016-08-24
        • 2017-07-17
        相关资源
        最近更新 更多