ios xcode 自定义 ttf 字体不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10367091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
xcode custom ttf font not working
提问by bkbeachlabs
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.
好的,所以我一直在阅读 SO 并进行各种谷歌搜索,但我无法弄清楚为什么我的字体不起作用。
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.
我想我已经做对了一切,但是当我运行应用程序时,按钮上的文本显示为标准系统字体,而不是我导入的字体。我添加NSLog(@"%@",[UIFont familyNames]);
以查看它是否在列表中,但它不是。这让我觉得我设置错了。
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:
第 1 步:我从互联网上下载了 .ttf 文件。在我的查找器中,它看起来像:
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:
第 2 步:我将字体文件从 finder 拖到 XCode 中并选中“将文件复制到项目文件夹”选项。所以在我的项目中我可以看到:
Step 3:I opened the font in font book to see what the real filename is and I see this:
第 3 步:我在 font book 中打开字体以查看真正的文件名是什么,我看到了:
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:
第 4 步:我在 MyApp-Info.plist 文件中添加了一个密钥,其文件名来自 XCode,包括文件类型。它看起来像这样:
Step 5:And then in my code I write this:
第 5 步:然后在我的代码中我这样写:
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"
)
回答by omz
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.
我怀疑您没有将字体文件添加到目标中,因此它不会被复制到应用程序的资源中。您的代码在这里工作正常,字体确实在字体系列列表中显示为“按开始 K”。
回答by ColossalChris
Another possibility is that you might be calling the wrong font name when you try to call your font. Use:
另一种可能性是您在尝试调用字体时可能调用了错误的字体名称。用:
Swift 2.1+
斯威夫特 2.1+
//Check which fonts available
for family: String in UIFont.familyNames()
{
print("\(family)")
for names: String in UIFont.fontNamesForFamilyName(family)
{
print("== \(names)")
}
}
Objective C
目标 C
//Check which fonts available
for (NSString* family in [UIFont familyNames])
{
NSLog(@"FONT %@", family);
for (NSString* name in [UIFont fontNamesForFamilyName: family])
{
NSLog(@" %@", name);
}
}
To find the actual names of the fonts you should be using with UIFont(name: "yourFontName", size: 24.0) (swift) or [UIFont fontWithName:@"yourFontName" size:16.0f] (obj c). The custom font names you add to your plist must match whatever the files are called in your directory (including the .ttf or .otf), but the code referencing the fonts must match the font names you get from this output. Good luck fontmasters!
要查找您应该使用 UIFont(name: "yourFontName", size: 24.0) (swift) 或 [UIFont fontWithName:@"yourFontName" size:16.0f] (obj c) 的实际字体名称。您添加到 plist 的自定义字体名称必须与目录中调用的任何文件(包括 .ttf 或 .otf)相匹配,但引用字体的代码必须与您从此输出中获得的字体名称相匹配。祝字体大师好运!