iOS 7 中的自定义字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21737788/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:25:36  来源:igfitidea点击:

Custom fonts in iOS 7

iosiphonefonts

提问by José María

I'm developing a game and I would like to use a custom font in my app. I'm using SpriteKit for it, if that's important. I've tried using this https://github.com/deni2s/IBCustomFontsbut I cannot get it to work with this font http://www.fontspace.com/freaky-fonts/emulogic

我正在开发一个游戏,我想在我的应用程序中使用自定义字体。如果这很重要,我正在使用 SpriteKit。我试过使用这个https://github.com/deni2s/IBCustomFonts但我无法让它与这种字体一起使用http://www.fontspace.com/freaky-fonts/emulogic

I've tried various things, and I've registered the font on the info.plist of the app... Does anyone know what's going on?

我已经尝试了各种方法,并且我已经在应用程序的 info.plist 上注册了字体......有谁知道发生了什么?

回答by jovanjovanovic

First of all I'm assuming that SpriteKit doesn't make any difference.

首先,我假设 SpriteKit 没有任何区别。

  1. You need your font in .otf or .ttf copied to your project. For example in Supporting Files.
  2. You need to edit .plistfile. Add "Fonts provided by application"key into your plist and in Item 0copy the exact filename of the font you copied to your Supporting files WITH extension. For example: "JosefinSansStd-Light_0.otf"
  3. Make sure that the font you imported to your app is being packed into app itself. Do that by selecting your Target, then Build Phases, then Copy Bundle Resources. If you don't see your font in there, drag it from Supporting Files.
  1. 您需要将 .otf 或 .ttf 中的字体复制到您的项目中。例如在支持文件中。
  2. 您需要编辑.plist文件。将“应用程序提供的字体”键添加到您的 plist 中,并在Item 0中将您复制的字体的确切文件名复制到带有扩展名的支持文件中。例如:“JosefinSansStd-Light_0.otf”
  3. 确保您导入到应用程序的字体已打包到应用程序本身中。通过选择您的目标,然后构建阶段,然后复制捆绑资源来做到这一点。如果您在那里没有看到您的字体,请将其从Supporting Files 中拖出。

Finally, you would like to list all your fonts when the app starts just to see useable name for your font. You will do that with this little piece of code:

最后,您希望在应用程序启动时列出所有字体,以便查看字体的可用名称。你将用这段代码做到这一点:

NSArray *fontFamilies = [UIFont familyNames];
for (int i = 0; i < [fontFamilies count]; i++)
{
    NSString *fontFamily = [fontFamilies objectAtIndex:i];
    NSArray *fontNames = [UIFont fontNamesForFamilyName:[fontFamilies objectAtIndex:i]];
    NSLog (@"%@: %@", fontFamily, fontNames);
}

Search for your font in printed results, for example, I would search for "Josefin" and I would see that actual font name is "JosefinSansStd-Light". After that you only need to use that font by:

在打印结果中搜索您的字体,例如,我会搜索“Josefin”,我会看到实际的字体名称是“JosefinSansStd-Light”。之后,您只需要通过以下方式使用该字体:

UIFont *customFont = [UIFont fontWithName:@"JosefinSansStd-Light" size:20];

In iOS8 you add your fontsdirectly to the project and they are visible in the interface builder. Modify your code to account for this but programmatically setting font for iOS7 and selecting it in xCode6 interface builder. PS. Interface builder in xCode6 gives you the correct font name that you can copy-paste into the code below.

在 iOS8 中,您可以将字体直接添加到项目中,并且它们在界面构建器中可见。修改您的代码以解决此问题,但以编程方式为 iOS7 设置字体并在 xCode6 界面构建器中选择它。附注。xCode6 中的界面构建器为您提供了正确的字体名称,您可以将其复制粘贴到下面的代码中。

#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)

 if(SYSTEM_VERSION_LESS_THAN(@"8.0"))
    {
        UIFont *customFont = [UIFont fontWithName:@"OpenSans-Light" size:32];
        self.registerLabel.font = customFont;  
    }

Hope this helps, cheers.

希望这有帮助,干杯。

回答by zago

No problem to use custom fonts, I did it. Follow these steps:

使用自定义字体没问题,我做到了。按着这些次序:

  1. Register the font in your info.plist adding the "Fonts provided by application" entry. Put the font file name, not the font name. Like font.ttf (I think you already did that)
  2. Go to your project on the project navigator, click on the project name and go to "build phases" then look down in "Copy Bundle Resources" go to the bottom, there are a lot of files and search for the "+" sign. A popup window will open with your project files. Search for your font file and add it to the bundle.
  3. Finally check the font name. When you use it in your code, you have to use the font name, not the file name. To know the font name, double click on the font file in Finder and a window will open showing the font. Check that window title, that's the name of the font that you have to use.
  1. 在 info.plist 中注册字体,添加“应用程序提供的字体”条目。输入字体文件名,而不是字体名称。像 font.ttf (我想你已经这样做了)
  2. 在项目导航器上转到您的项目,单击项目名称并转到“构建阶段”,然后在“复制捆绑资源”中往下看,转到底部,有很多文件并搜索“+”号。一个弹出窗口将打开您的项目文件。搜索您的字体文件并将其添加到包中。
  3. 最后检查字体名称。当您在代码中使用它时,您必须使用字体名称,而不是文件名。要知道字体名称,请在 Finder 中双击字体文件,将打开一个显示字体的窗口。检查该窗口标题,这是您必须使用的字体名称。

And advice, add that name as a macro in your ..Prefix.pch file. It will be easier to use it in your project if you have to use the font multiple times.

并建议,将该名称作为宏添加到您的 ..Prefix.pch 文件中。如果您必须多次使用该字体,在您的项目中使用它会更容易。

The problem that I think you had is that you properly registered the font but it was not copied to the app bundle. So when the app executed it couldn't find the font. That's why step 2 is important. I spent a lot of time trying to figure that out the first time :)

我认为您遇到的问题是您正确注册了字体,但未将其复制到应用程序包中。因此,当应用程序执行时,它找不到字体。这就是为什么第 2 步很重要。我第一次花了很多时间试图弄清楚这一点:)

回答by Toye

To achieve this in swift

为了快速实现这一目标

If you would like to list all your fonts when the app starts just to see useable name for your font. You can do that with this little piece of code:

如果您想在应用程序启动时列出所有字体,只是为了查看字体的可用名称。你可以用这段代码做到这一点:

    UIFont.familyNames.forEach {
        print("fontFamily: \(
let fontFamilies:[String] = UIFont.familyNames

for fontFamily: String in fontFamilies {
    let fontNames: [String] = UIFont.fontNames(forFamilyName: fontFamily)
    print("\(fontFamily)  \(fontNames)");
}
), fonts: \(UIFont.fontNames(forFamilyName:
please try below code.
@end
@implementation UILabel (CustomFont)
-(void)setFontName:(NSString *)value{
CGFloat newsize = (self.font.pointSize * [UIScreen mainScreen].bounds.size.width)/320;
[self setFont:[UIFont fontWithName:self.font.fontName size:newsize]];
}
@end
@implementation UITextField (CustomFont)
-(void)setFontName:(NSString *)value{
CGFloat newsize = (self.font.pointSize * [UIScreen mainScreen].bounds.size.width)/320;
[self setFont:[UIFont fontWithName:self.font.fontName size:newsize]];
}
@end
@implementation UIButton (CustomFont)
-(void)setFontName:(NSString *)value{
CGFloat newsize = (self.titleLabel.font.pointSize * [UIScreen mainScreen].bounds.size.width)/320;
[self.titleLabel setFont:[UIFont fontWithName:self.titleLabel.font.fontName size:newsize]];
}
@end
@end

@interface UILabel (CustomFont)
-(void)setFontName:(NSString *)value;
@end
@interface UITextField (CustomFont)
-(void)setFontName:(NSString *)value;
@end
@interface UIButton (CustomFont)
-(void)setFontName:(NSString *)value;
@end
))") }

回答by Stuart Clark

I have never been able to get these answers to work having tried multiple times. The only way I have ever got a custom font in it to follow the instruction in the accepted answer then create a hidden label in the story board that uses the font. The I can use the font programatically. Weird and hacky I know but it worked for me.

多次尝试后,我一直无法得到这些答案。我在其中使用自定义字体以遵循已接受答案中的说明的唯一方法,然后在使用该字体的故事板中创建一个隐藏标签。我可以以编程方式使用字体。我知道奇怪和hacky,但它对我有用。

回答by Wojtek

If none of the methods above worked for you, AND you happen to use your custom font "programatically" only (i.e. you didn't use it in any xib) then you will need to use it at least once somewhere in one xib, anywhere.

如果上述方法均不适合您,并且您碰巧仅“以编程方式”使用自定义字体(即您没有在任何 xib 中使用它),那么您将需要在一个 xib 的某处至少使用一次,任何地方.

(P.S. Stuart Clark - already answered this, so no credit for me please. However, I decided to rephrase and post it as a separate answer, because I missed his answer, it wasn't clear to me when I read it. I found out this solution by chance when I was testing the custom font in a xib, and I saw that it worked elsewhere programatically, but when I removed the font from the xib, it stopped working programatically.)

(PS Stuart Clark - 已经回答了这个问题,所以请不要相信我。但是,我决定改写并将其作为单独的答案发布,因为我错过了他的答案,我在阅读时不清楚。我发现当我在 xib 中测试自定义字体时偶然发现了这个解决方案,我看到它以编程方式在其他地方工作,但是当我从 xib 中删除字体时,它停止以编程方式工作。)

回答by Idris

If you're in Xcode 6 it's as easy as it gets. Just add a fontfolder into your project, add your font(s) into the folder, and you're good to go.

如果您使用的是 Xcode 6,那么它就变得非常简单。只需font在您的项目中添加一个文件夹,将您的字体添加到该文件夹​​中,您就可以开始了。

enter image description here

在此处输入图片说明

回答by Gavin Williams

Updated for Swift 3.

为 Swift 3 更新。

##代码##

回答by herry.master

##代码##

回答by brainray

Maybe a quick fix - this did it for me when the font has already worked, but a new e.g. Label didn't show the desired font:

也许是一个快速修复 - 当字体已经工作时,这对我来说是这样的,但是一个新的例如标签没有显示所需的字体:

clean, deep clean and empty derived data folder.

清洁、深度清洁和清空派生数据文件夹。