无法更改UIButton上的自定义UIFont大小

问题描述 投票:7回答:5

我有一个按钮,使用自定义UIFont来显示文本。字体正确加载并正确应用于按钮标题。我的问题是我似乎无法更改字体大小:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(loginButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Login" forState:UIControlStateNormal];
button.frame = CGRectMake(63.0, 200.0, 194.0, 60.0);
button.titleLabel.font = [UIFont fontWithName:@"My-Font" size:8.0f];
[self.view addSubview:button];

无论我输入什么字体大小,我都会得到一些默认大小。另一方面,如果我做这样的事情:

button.titleLabel.font = [UIFont systemFontOfSize:32.0f];

我得到32号字体,但是,当然,我没有自定义字体。那么,如何设置自定义字体的大小?

(我使用Xcode 4.6.3)

ios uibutton uifont
5个回答
3
投票

似乎您的UIFont对象未正确创建,并且正在返回nil

这是我发现的有关放入自定义字体的最佳教程。祝你好运;)

[http://refactr.com/blog/2012/09/ios-tips-custom-fonts/Archived Version


2
投票

要获取必须在fontWithName:size:中传递的字体名称,请使用Font Book应用程序打开字体,然后查找PostScript name属性。请确保您也已将其添加到Info.plist文件。


2
投票

执行此操作:

    NSMutableDictionary *fontsByFamilyName = [NSMutableDictionary dictionary];
    NSArray *sortDescriptors = [NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES] autorelease]];

    NSArray *familyNames = [[UIFont familyNames] sortedArrayUsingDescriptors:sortDescriptors];
    for (NSString *aFamilyName in familyNames) {
        NSArray *fontNames = [[UIFont fontNamesForFamilyName:aFamilyName] sortedArrayUsingDescriptors:sortDescriptors];

        [fontsByFamilyName setObject:fontNames forKey:aFamilyName];
        NSLog(@"fonts :%@", fontsByFamilyName);           
    }

寻找您的字体的“ iOS”名称...我用这种方法找到了自己的字体。如果没有,让我们检查一下如何添加字体(也许忘记了操作)。来源:http://forgecode.net/2010/08/uifont-name-grabber/


1
投票

有趣的是,事实证明是我的Mac。当我给我的朋友安装它们时,他没有任何问题。我的Mac很旧,所以也许这就是原因。这些是规格:

**Mac:** MacBook Pro 15" (Late 2008)
**Processor:**  2.4 GHz Intel Core 2 Duo
**Memory:** 4 GB 1067 MHz DDR3
**Graphics:** NVIDIA GeForce 9600M GT 256 MB

-1
投票

为'My-Font'输入'nil'可以为默认字体调整文本大小-无需知道实际使用的字体是什么:

button.titleLabel.font = [UIFont fontWithName:nil size:10.0f];
© www.soinside.com 2019 - 2024. All rights reserved.