简体   繁体   English

UIButton.titlelabel setfont不起作用

[英]UIButton.titlelabel setfont doesn't work

I have added button in interface builder and referenced them, then I want to change their title font, but it doest work. 我在界面生成器中添加了按钮并引用了它们,然后我想更改其标题字体,但是它不起作用。 When I use: 当我使用时:

[_button setFont:[UIFont fontWithName:APP_FONT_FUTURASTD_LIGHT size:24.0f]];

it WORKS, but this code is depreciated. 可以,但是此代码已弃用。 So I have looked online to see what to use instead of this code, I found this code to use: 因此,我在网上查看了要使用的代码而不是此代码,我发现此代码要使用:

[_button.titleLabel setFont:[UIFont fontWithName:APP_FONT_FUTURASTD_LIGHT size:24.0f]];
_button.titleLabel.font = [UIFont fontWithName:APP_FONT_FUTURASTD_LIGHT size:24.0f];

but NEITHER of this work. 但是没有这项工作。 I have no idea what to do with it or what is wrong. 我不知道该怎么办或出了什么问题。 I have tried to set globally appearance of UIButton, that works too, but I cant use it as I need that font only on some buttons. 我尝试设置UIButton的全局外观,该方法也可以使用,但是我不能使用它,因为我只需要在某些按钮上使用该字体。

Button is as: 按钮为:

@property (nonatomic, weak) IBOutlet UIButton *button;

and added in storyboard. 并添加到情节提要中。

Font is defined: 字体定义为:

#define APP_FONT_FUTURASTD_LIGHT @"FuturaStd-Light"

and added to Info.plist section "Fonts provided by application". 并添加到Info.plist部分“应用程序提供的字体”。

I have found out what the problem was. 我发现了问题所在。 The other programmer who has started this project has set 启动该项目的其他程序员已经设置

[[UIButton appearance] setFont:[UIFont fontWithName:APP_FONT_FUTURASTD_BOLD size:12.0f]];

in CSAppDelegate.m and so all my customizations were overwritten by this. 在CSAppDelegate.m中,因此我的所有自定义设置都被此覆盖。

So to anyone who configures buttons font globally like this, you will not be able to then customize font of some buttons differently. 因此,对于像这样全局配置按钮字体的任何人,您将无法以其他方式自定义某些按钮的字体。 And second advice, if you continue work after someone else make sure that they give you some technical documentation or to explain to you what how and where they did. 第二个建议是,如果您在别人之后继续工作,请确保他们给您一些技术文档,或者向您解释他们的工作方式和方式。

EDIT 编辑

You can however set appearance for one view in viewWillAppear and then set it back to what you have in your CSAppDelegate.m in viewDidAppear of the view. 但是,您可以在viewWillAppear中为一个视图设置外观,然后将其设置回该视图的viewDidAppear中CSAppDelegate.m中的外观。

EDIT 编辑

Now this code is depreciated so you have to use this instead: 现在此代码已折旧,因此您必须改用以下代码:

[[[UIButton appearance] titleLabel] setFont:UIFont];

The Problem seems to me is the Font name. 在我看来,问题在于字体名称。 The Font Name you are using in the code is not the exact name. 您在代码中使用的字体名称不是确切的名称。

You Can Use the Following code to get List Of all Fonts name supported by your app 您可以使用以下代码获取应用支持的所有字体名称的列表

  // List all fonts on iPhone

for (NSString* family in [UIFont familyNames])
{
    NSLog(@"%@", family);

    for (NSString* name in [UIFont fontNamesForFamilyName: family])
    {
        NSLog(@"  %@", name);
    }
}

from the Console of xcode you can find out the exact name of the font that you added in your application 从xcode的控制台中,您可以找到在应用程序中添加的字体的确切名称。

Please remove the AutoLayout checkmark if You used with Xib.It will works fine. 如果您与Xib一起使用,请删除自动版式复选标记,它将正常工作。 I think its helps to you. 我认为它对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM