在整个应用程序中将默认字体更改为自定义字体
我想保留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
所有视图。
-
将您的字体文件复制到资源中
-
将一个密钥添加到名为
UIAppFonts
的Info.plist文件中。 (“应用程序提供的字体”) -
使这个键是一个数组
-
对于每种字体,请将字体文件的全名(包括扩展名)作为项目input到
UIAppFonts
arrays中 -
保存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
更改整个应用程序的UILabel
:
在AppDelegate.m中添加代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { ... [[UILabel appearance] setFont:[UIFont fontWithName:@"YourFontName" size:17]]; ... }
在一个viewConroller
改变字体与在视图devise中设置的字体大小相同:
- (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]; } } }
好吧,从所有其他的答案,你现在非常熟悉,如何添加自定义字体(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]
我已经在全球添加这些macros(所以可以从任何地方访问它)
你可以像这样使用它, yourLabel.font = appFontRegular(12);
或yourTextField.font = appFontBold(14);
这有助于你在以下情况下,
- 无论何时需要更改自定义字体,只需要在macros中更改字体名称。
- 你的代码看起来很干净,不需要写
[UIFont fontWithName: size:]
; 到处。
如果你的应用程序将有多个自定义字体,那么你可以像这样对整个应用程序使用它。
那么你也可以像这样定义不同的macros,
然后上面的macros将是相同的(因为这是在应用程序中使用的最大字体),对于其他字体:
#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]
1-拖放字体文件.ttf到您的项目
2-确认字体在项目中。
通过select字体,并在“实用程序”区域中validation“目标成员”。
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]];
我希望这个能帮上忙
将您的自定义字体添加到您的项目,即将字体文件(CALIBRIZ_0.TTF)拖到XCode项目。
编辑Info.plist
:用“ Fonts provided by application
”键添加一个新条目。
对于每个文件,将文件名添加到该array
现在将字体设置为您的label
yourLabel.font = [UIFont fontWithName:@"Calibri" size:15];
如果要在一个位置更改所有UILabel的所有字体,则需要创build一个UILabel类别。
例如:
#import <UIKit/UIKit.h> @interface UILabel (AppFont) @end @implementation UILabel (AppFont) -(void)awakeFromNib { self.font = [UIFont fontWithName:@"yourCustomFont" size:self.font.pointSize]; } @end
在创build类别之后,您需要为所有文件导入此类,或者可以将其添加到项目的“.pch”文件中。
相同的解决schemebutton/ textview ..