在 objective-c 中设置自定义字体
Setting custom font in objective-c
嗨,
我正在尝试为我的标签设置字体,但当我 运行 应用程序时,它没有出现在我的 iOS-模拟器中。
_textLabel1 = [[UILabel alloc] initWithFrame:CGRectMake((CGFloat) (self.view.center.x - (self.view.bounds.size.width / textWidth / 2)),
(CGFloat) (self.view.center.y * textPositionY), (CGFloat) (self.view.bounds.size.width / textWidth), (CGFloat) textHeight)];
_textLabel1.numberOfLines = 0;
_textLabel1.textAlignment = NSTextAlignmentCenter;
_textLabel1.textColor = clr;
_textLabel1.text = @"Favoriten";
[_textLabel1 setFont:[UIFont fontWithName:@"assets/fonts/Kingthings_Trypewriter_2.ttf" size:36]];
[_favouriteViewer addSubview:_textLabel1];
_textLabel1.font=[_textLabel1.font fontWithSize:25];
我还在我的 plist 中添加了自定义字体:
<key>UIAppFonts</key>
<array>
<string>Kingthings_Trypewriter_2.ttf</string>
</array>
我将我的自定义字体保存在我调用的命名资产文件夹中:
[_textLabel1 setFont:[UIFont fontWithName:@"assets/fonts/Kingthings_Trypewriter_2.ttf" size:36]];
我在模拟器中没有显示字体。
感谢您的帮助!
编辑:
我使用的是 AppCode,而不是 Xcode.
编辑2:
感谢 Ganesh Somani,我使用他的代码来尝试确定我的字体是否已通过 Copy Bundle 资源正确添加到项目中,但不幸的是它仍然没有出现在日志中。
for(NSString *fontfamilyname in [UIFont familyNames])
{
NSLog(@"Family:'%@'",fontfamilyname);
for(NSString *fontName in [UIFont fontNamesForFamilyName:fontfamilyname])
{
NSLog(@"\tfont:'%@'",fontName);
}
NSLog(@"---");
}
解决方法:
出于某种原因,我的项目中有两个 .plist 文件。我输入了以下代码,现在可以运行了:
<key>UIAppFonts</key>
<array>
<string>"fontName.ttf"</string>
</array>
感谢所有贡献者的帮助!
自定义字体有时会令人头疼。
我曾经遇到过同样的问题,但有标签。
This answer 帮我解决了。也许这对你有帮助。
还在 how to add custom fonts 到 iOS app
上找到了一篇非常好的文章
编辑
另一种可能是你没有使用正确的字体名,而是使用了文件名
使用以下代码查找真实姓名
for(NSString *fontfamilyname in [UIFont familyNames])
{
NSLog(@"Family:'%@'",fontfamilyname);
for(NSString *fontName in [UIFont fontNamesForFamilyName:fontfamilyname])
{
NSLog(@"\tfont:'%@'",fontName);
}
NSLog(@"---");
}
只需将您的字体文件导入您的项目:
然后将它们添加到您的 Info.plist 文件中:
然后你就可以在你的代码中使用它们了:
确保你使用了正确的名称,在字体应用程序中(点击两次并在你的电脑上安装你的字体后,你可以看到这个信息)点击CMD + i,然后检查你的"PostScript name"字体,这就是你如何称呼它。
在 Xcode 打开你的项目,点击项目图标,select 目标,然后是构建阶段,转到复制包资源部分并检查你的字体是否在那里(记住你需要将它们添加到你的项目文件夹中):
如果您使用的是 Objective-c,那么要显示字体系列,您可以使用以下循环:
for(int i =0; i < UIFont.familyNames.count; i++){
NSString *myFontName = [UIFont.familyNames objectAtIndex:i];
NSLog(@"here myFontFamily is %@",myFontName);
for (int j = 0; j < [UIFont fontNamesForFamilyName:myFontName].count; j++){
NSLog(@" here the font is %@",[[UIFont fontNamesForFamilyName:myFontName]objectAtIndex:j]);
}
}
除了解释的很好, if you're still not getting your fonts, Just use or assign your font to any Label
in one of your Storyboard
, You will start getting it using
如果使用[UIFont fontWithName:@"Font-Regular" size:11];
还是无法得到字体,一定是命名有问题。在调试器中的任何断点处停止并使用不同的名称对其进行调试,然后查看哪个 po
打印不是 nil
的对象
po [UIFont fontWithName:@"FontRegular" size:11];
或
po [UIFont fontWithName:@"Font Regular" size:11];
或
po [UIFont fontWithName:@"Font-Regular" size:11];
您将获得 <UICTFont: 0x7fe2044021d0> font-family: "Interstate-Regular"; font-weight: normal; font-style: normal; font-size: 11.00pt
作为实际字体名称,否则您将获得 nil
嗨,
我正在尝试为我的标签设置字体,但当我 运行 应用程序时,它没有出现在我的 iOS-模拟器中。
_textLabel1 = [[UILabel alloc] initWithFrame:CGRectMake((CGFloat) (self.view.center.x - (self.view.bounds.size.width / textWidth / 2)),
(CGFloat) (self.view.center.y * textPositionY), (CGFloat) (self.view.bounds.size.width / textWidth), (CGFloat) textHeight)];
_textLabel1.numberOfLines = 0;
_textLabel1.textAlignment = NSTextAlignmentCenter;
_textLabel1.textColor = clr;
_textLabel1.text = @"Favoriten";
[_textLabel1 setFont:[UIFont fontWithName:@"assets/fonts/Kingthings_Trypewriter_2.ttf" size:36]];
[_favouriteViewer addSubview:_textLabel1];
_textLabel1.font=[_textLabel1.font fontWithSize:25];
我还在我的 plist 中添加了自定义字体:
<key>UIAppFonts</key>
<array>
<string>Kingthings_Trypewriter_2.ttf</string>
</array>
我将我的自定义字体保存在我调用的命名资产文件夹中:
[_textLabel1 setFont:[UIFont fontWithName:@"assets/fonts/Kingthings_Trypewriter_2.ttf" size:36]];
我在模拟器中没有显示字体。
感谢您的帮助!
编辑: 我使用的是 AppCode,而不是 Xcode.
编辑2:
感谢 Ganesh Somani,我使用他的代码来尝试确定我的字体是否已通过 Copy Bundle 资源正确添加到项目中,但不幸的是它仍然没有出现在日志中。
for(NSString *fontfamilyname in [UIFont familyNames])
{
NSLog(@"Family:'%@'",fontfamilyname);
for(NSString *fontName in [UIFont fontNamesForFamilyName:fontfamilyname])
{
NSLog(@"\tfont:'%@'",fontName);
}
NSLog(@"---");
}
解决方法: 出于某种原因,我的项目中有两个 .plist 文件。我输入了以下代码,现在可以运行了:
<key>UIAppFonts</key>
<array>
<string>"fontName.ttf"</string>
</array>
感谢所有贡献者的帮助!
自定义字体有时会令人头疼。
我曾经遇到过同样的问题,但有标签。
This answer 帮我解决了。也许这对你有帮助。
还在 how to add custom fonts 到 iOS app
上找到了一篇非常好的文章编辑
另一种可能是你没有使用正确的字体名,而是使用了文件名
使用以下代码查找真实姓名
for(NSString *fontfamilyname in [UIFont familyNames])
{
NSLog(@"Family:'%@'",fontfamilyname);
for(NSString *fontName in [UIFont fontNamesForFamilyName:fontfamilyname])
{
NSLog(@"\tfont:'%@'",fontName);
}
NSLog(@"---");
}
只需将您的字体文件导入您的项目:
然后将它们添加到您的 Info.plist 文件中:
然后你就可以在你的代码中使用它们了:
确保你使用了正确的名称,在字体应用程序中(点击两次并在你的电脑上安装你的字体后,你可以看到这个信息)点击CMD + i,然后检查你的"PostScript name"字体,这就是你如何称呼它。
在 Xcode 打开你的项目,点击项目图标,select 目标,然后是构建阶段,转到复制包资源部分并检查你的字体是否在那里(记住你需要将它们添加到你的项目文件夹中):
如果您使用的是 Objective-c,那么要显示字体系列,您可以使用以下循环:
for(int i =0; i < UIFont.familyNames.count; i++){
NSString *myFontName = [UIFont.familyNames objectAtIndex:i];
NSLog(@"here myFontFamily is %@",myFontName);
for (int j = 0; j < [UIFont fontNamesForFamilyName:myFontName].count; j++){
NSLog(@" here the font is %@",[[UIFont fontNamesForFamilyName:myFontName]objectAtIndex:j]);
}
}
除了解释的很好Label
in one of your Storyboard
, You will start getting it using
如果使用[UIFont fontWithName:@"Font-Regular" size:11];
还是无法得到字体,一定是命名有问题。在调试器中的任何断点处停止并使用不同的名称对其进行调试,然后查看哪个 po
打印不是 nil
po [UIFont fontWithName:@"FontRegular" size:11];
或
po [UIFont fontWithName:@"Font Regular" size:11];
或
po [UIFont fontWithName:@"Font-Regular" size:11];
您将获得 <UICTFont: 0x7fe2044021d0> font-family: "Interstate-Regular"; font-weight: normal; font-style: normal; font-size: 11.00pt
作为实际字体名称,否则您将获得 nil