我想在iOS应用程序中使用自定义字体,目前我正在使用iOS默认字体。是否有可能实现同样的效果?
我应该如何包含自定义字体以便使用它?
我想在iOS应用程序中使用自定义字体,目前我正在使用iOS默认字体。是否有可能实现同样的效果?
我应该如何包含自定义字体以便使用它?
创建一个分类:
#import "UILabel+Helper.h"
@implementation UILabel (Helper)
- (void)setSubstituteFontName:(NSString *)name UI_APPEARANCE_SELECTOR {
self.font = [UIFont fontWithName:name size:self.font.pointSize]; }
@end
然后在AppDelegate.m中:
[[UILabel appearance] setSubstituteFontName:@"SourceSansPro-Regular"];
这将改变整个应用程序中的字体,甚至包括UIButton
、UILabel
、UITableViewCell
、UICollectionViewCell
、UIView
、UIContainerView
等任何位置,而不会更改字体大小。
这是一种内存高效的方法,而不是枚举您的UIViewController
中的所有视图。
将字体文件复制到资源中
在Info.plist文件中添加一个名为UIAppFonts
(“应用提供的字体”)的键。
将此键设置为数组
对于您拥有的每种字体,将其完整文件名(包括扩展名)作为项输入到UIAppFonts
数组中
保存Info.plist
现在,在您的应用程序中,您只需调用以下方法即可获取用于UILabel
和UITextView
等的自定义字体:
[UIFont fontWithName:@"CustomFontName" size:15];
func allFonts(){
for family in UIFont.familyNames(){
println(family)
for name in UIFont.fontNamesForFamilyName(family.description)
{
println(" \(name)")
}
}
}
UILabel
中更改整个应用程序的字体:在AppDelegate.m中添加以下代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
[[UILabel appearance] setFont:[UIFont fontWithName:@"YourFontName" size:17]];
...
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self setFontFamily:@"YourFontName" forView:self.view andSubViews:YES];
}
-(void)setFontFamily:(NSString*)fontFamily forView:(UIView*)view andSubViews:(BOOL)isSubViews
{
if ([view isKindOfClass:[UILabel class]])
{
UILabel *lbl = (UILabel *)view;
[lbl setFont:[UIFont fontWithName:fontFamily size:[[lbl font] pointSize]]];
}
if (isSubViews)
{
for (UIView *sview in view.subviews)
{
[self setFontFamily:fontFamily forView:sview andSubViews:YES];
}
}
}
1- 将.ttf字体文件拖放到您的项目中
2- 确认该字体已在项目中。
通过选择字体并在实用程序区域验证“目标成员资格”来进行确认。
3- 在plist文件中添加信息
4- 在代理中添加以下代码
[[UILabel appearance] setFont:[UIFont fontWithName:@"YourFontName" size:11.0]];
[[UITextField appearance] setFont:[UIFont fontWithName:@"YourFontName" size:11.0]];
[[UITextView appearance] setFont:[UIFont fontWithName:@"YourFontName" size:11.0]];
好的,从其他答案中你现在已经非常熟悉如何将自定义字体(而不是iOS默认字体)添加到应用程序中。
那么现在呢?还有一件事没有解决,我不确定你是否想在整个应用程序中使用相同大小的自定义字体。如果是这样的话,
我使用以下方式在我的应用程序中使用自定义字体。
如果您的应用程序只有一个自定义字体,则可以像这样在整个应用程序中使用它。
#define appFontBold(x) [UIFont fontWithName:@"CustomFont-Bold" size:x]
#define appFontLight(x) [UIFont fontWithName:@"CustonFont-Light" size:x]
#define appFontRegular(x) [UIFont fontWithName:@"CustonFont-Regular" size:x]
yourLabel.font = appFontRegular(12);
或 yourTextField.font = appFontBold(14);
,这有助于您在以下情况下使用:
[UIFont fontWithName: size:]
。#define appFontBoldOtherFont(x) [UIFont fontWithName:@"CustomOtherFont-Bold" size:x]
#define appFontLightOtherFont(x) [UIFont fontWithName:@"CustonOtherFont-Light" size:x]
#define appFontRegularOtherFont(x) [UIFont fontWithName:@"CustonOtherFont-Regular" size:x]
将自定义字体添加到您的项目中,即将字体文件(CALIBRIZ_0.TTF)拖入XCode项目。
编辑Info.plist
:添加一个新条目,键为“应用提供的字体
”。
对于每个文件,请将文件名添加到此数组
中
现在将字体设置为您的标签
yourLabel.font = [UIFont fontWithName:@"Calibri" size:15];
#import <UIKit/UIKit.h>
@interface UILabel (AppFont)
@end
@implementation UILabel (AppFont)
-(void)awakeFromNib
{
self.font = [UIFont fontWithName:@"yourCustomFont" size:self.font.pointSize];
}
@end
在创建类别之后,您需要为所有文件导入此类,或将其添加到项目的“.pch”文件中。
按钮/文本视图的解决方案相同。