xcode自定义ttf字体不工作 [英] xcode custom ttf font not working

查看:178
本文介绍了xcode自定义ttf字体不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在阅读,做各种谷歌搜索,但我不知道为什么我的字体不工作。

我认为我做的一切都正确,但是当我运行应用程序时,我的按钮上的文本显示为标准系统字体,而不是我导入的那个。我添加了 NSLog(@%@,[UIFont familyNames]); 来查看它是否在列表中,但它不是。这让我觉得我已经把它设置错了。



我希望有人能帮助我以我的自定义字体显示标签中的文本。
感谢任何认为他们可能有任何建议的人!



这是我所做的一步一步。第一步:我从互联网上下载了.ttf文件。 在我的发现者,它看起来像:


第2步:我从查找器中将字体文件拖入XCode,然后选中复制文件到项目文件夹选项。所以在我的项目中,我可以看到:





步骤4:我使用XCode中的文件名(包括文件类型)向我的MyApp-Info.plist文件添加了一个键。它看起来像这样:





第五步:然后在我的代码中写下:

  UIButton * thisLevelButton = [UIButton buttonWithType:UIButtonTypeCustom]; 

[thisLevelButton setBackgroundImage:[UIImage imageNamed:@ccLSButtonPlayed] forState:UIControlStateNormal];
thisLevelButton.frame = CGRectMake(x,y,BUTTON_WIDTH,BUTTON_HEIGHT);
thisLevelButton.tag = j;
[thisLevelButton addTarget:self action:@selector(userSelectedButton :) forControlEvents:UIControlEventTouchUpInside];
[thisLevelButton.titleLabel setFont:[UIFont fontWithName:@PressStartKsize:24]];
[thisLevelButton setTitle:[NSString stringWithFormat:@%i,(j + 1)] forState:UIControlStateNormal];

//将按钮添加到面板
[subScroll addSubview:thisLevelButton];

作为参考,这是在字体系列列表中打印的内容:

 
Thonburi,
Snell Roundhand,
Academy Engraved LET,
Marker Felt ,
Geeza Pro,
Arial Rounded MT Bold,
Trebuchet MS,
Arial,
Marion,
Gurmukhi MN ,
Malayalam Sangam MN,
Bradley Hand,
Kannada Sangam MN,
Bodoni 72 Oldstyle,
Cochin,
Sinhala Sangam MN,
Hiragino Kaku Gothic ProN,
纸莎草纸,
Verdana,
Zapf标志,
快递,
Hoefler Text,
Euphemia UCAS,
Helvetica,
Hiragino Mincho ProN,
Bodoni装饰品,
Apple Color Emoji,
Optima,
Gujarati Sangam MN,
Devanagari Sangam MN,
Times New Roman,
Kailasa,
Telugu Sangam MN,
Heiti SC,
Apple SD Gothic Neo,
Futura,
Bodoni 72,
Baskerville,
黑板SE,
Heiti TC,
铜板,
派对LET,
美国打字机,
Bangla Sangam MN,
值得注意的是,
Zapfino,
泰米尔Sangam MN,
DB LCD Temp,
Arial Hebrew,
Chalkduster,
格鲁吉亚,
Helvetica Neue,
Gill S
Palatino
新快递
Oriya Sangam MN
Didot
Bodoni 72小袋
$ b $ $ b


解决方案

我会怀疑你没有添加字体文件您的目标,以便它不被复制到应用程序的资源。你的代码在这里工作的很好,字体在字体系列列表中显示为Press Start K。


ok so I've been reading through SO and doing all sorts of googling but I cant figure out why my font isn't working.

I think I've done everything right, but when I run the app, the text on my button appears with the standard system font instead of the one I imported. I added NSLog(@"%@",[UIFont familyNames]); to see if it was in the list but it isnt. Which makes me think I've set it up wrong.

Im hoping someone can help me display the text in my label in my custom font. Thanks to anyone who thinks they might have any suggestions!

Step-by-step this is what I've done.

Step 1: I downloaded .ttf file from the internet. In my finder it looks like:

Step 2: I dragged the font file into XCode from finder and check the "copy file to project folder" option. So in my project I can see:

Step 3: I opened the font in font book to see what the real filename is and I see this:

Step 4: I added a key to my MyApp-Info.plist file with the filename from XCode including the file type. It looks like this:

Step 5: And then in my code I write this:

UIButton *thisLevelButton = [UIButton buttonWithType:UIButtonTypeCustom];

[thisLevelButton setBackgroundImage:[UIImage imageNamed:@"ccLSButtonPlayed"] forState:UIControlStateNormal];
thisLevelButton.frame = CGRectMake(x, y, BUTTON_WIDTH, BUTTON_HEIGHT);
thisLevelButton.tag = j;
[thisLevelButton addTarget:self action:@selector(userSelectedButton:) forControlEvents:UIControlEventTouchUpInside];
[thisLevelButton.titleLabel setFont:[UIFont fontWithName:@"PressStartK" size:24]];
[thisLevelButton setTitle:[NSString stringWithFormat:@"%i",(j+1)] forState:UIControlStateNormal];

// add the button to the panel
[subScroll addSubview:thisLevelButton];

For reference, this is what's printed in the list of font families:

(
    Thonburi,
    "Snell Roundhand",
    "Academy Engraved LET",
    "Marker Felt",
    "Geeza Pro",
    "Arial Rounded MT Bold",
    "Trebuchet MS",
    Arial,
    Marion,
    "Gurmukhi MN",
    "Malayalam Sangam MN",
    "Bradley Hand",
    "Kannada Sangam MN",
    "Bodoni 72 Oldstyle",
    Cochin,
    "Sinhala Sangam MN",
    "Hiragino Kaku Gothic ProN",
    Papyrus,
    Verdana,
    "Zapf Dingbats",
    Courier,
    "Hoefler Text",
    "Euphemia UCAS",
    Helvetica,
    "Hiragino Mincho ProN",
    "Bodoni Ornaments",
    "Apple Color Emoji",
    Optima,
    "Gujarati Sangam MN",
    "Devanagari Sangam MN",
    "Times New Roman",
    Kailasa,
    "Telugu Sangam MN",
    "Heiti SC",
    "Apple SD Gothic Neo",
    Futura,
    "Bodoni 72",
    Baskerville,
    "Chalkboard SE",
    "Heiti TC",
    Copperplate,
    "Party LET",
    "American Typewriter",
    "Bangla Sangam MN",
    Noteworthy,
    Zapfino,
    "Tamil Sangam MN",
    "DB LCD Temp",
    "Arial Hebrew",
    Chalkduster,
    Georgia,
    "Helvetica Neue",
    "Gill Sans",
    Palatino,
    "Courier New",
    "Oriya Sangam MN",
    Didot,
    "Bodoni 72 Smallcaps"
)

解决方案

I would suspect that you haven't added the font file to your target, so that it's not copied to the app's resources. Your code works fine here and the font does show up as "Press Start K" in the list of font families.

这篇关于xcode自定义ttf字体不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆