如何在不指定大小的情况下为整个 iOS 应用程序设置自定义字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11907761/
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
How to set a custom font for entire iOS app without specifying size
提问by Janum Trivedi
I'm trying to apply a custom font throughout my iOS app
. I found that I could use:
我正在尝试在整个iOS app
. 我发现我可以使用:
[[UILabel appearance] setFont:[UIFont fontWithName:@"Proxima Nova" size:17.0]];
To set the default font and size for all
UILabels
. However, not all myUILabels
share the same font size.In Set a default font for whole iOS app?, someone had the same concern, and was told to set the size parameter to 0.0 to only set the font and not font size.
- When I tried doing this, all the
UILabel
text in my app disappeared (because evidentlyiOS
took the 0.0 font size literally).
为所有
UILabels
. 但是,并非所有我都UILabels
共享相同的字体大小。在为整个 iOS 应用设置默认字体?,有人有同样的担忧,并被告知将大小参数设置为 0.0 以仅设置字体而不设置字体大小。
- 当我尝试这样做时,
UILabel
我的应用程序中的所有文本都消失了(因为显然iOS
字面意思是 0.0 字体大小)。
Any suggestions as to how I can universally set a font but not size? Thanks a lot!
关于如何普遍设置字体而不是大小的任何建议?非常感谢!
采纳答案by Raegtime
- (void)viewDidLoad
{
[super viewDidLoad];
[self setFontFamily:@"FagoOfficeSans-Regular" 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];
}
}
}
回答by Damien Debin
Here is a solution in Objective-C, put this categoryanywhere you want to change UILabel
Apperance
without setting UILabel
FontSize
:
这是Objective-C中的一个解决方案,将此类别放在您要更改的任何位置UILabel
Apperance
而无需设置UILabel
FontSize
:
@implementation UILabel (SubstituteFontName)
- (void)setSubstituteFontName:(NSString *)name UI_APPEARANCE_SELECTOR {
self.font = [UIFont fontWithName:name size:self.font.pointSize];
}
@end
Then, you can change the Apperance
with:
然后,您可以更改为Apperance
:
[[UILabel appearance] setSubstituteFontName:@"SourceSansPro-Light"];
回答by marmor
I've used the accepted answer in my project, but needed a more generic function, so it'll change the font to every one possible, also I've chose to set a mapping between some stock fonts to our custom fonts, so they'll be accessible via storybuilder and xib files as well.
我在我的项目中使用了公认的答案,但需要一个更通用的功能,所以它会将字体更改为每一种可能的字体,而且我选择在一些库存字体与我们的自定义字体之间设置映射,所以他们也可以通过 storybuilder 和 xib 文件访问。
+ (void)setupFontsForView:(UIView *)view andSubViews:(BOOL)isSubViews
{
if ([view respondsToSelector:@selector(setFont:)] && [view respondsToSelector:@selector(font)]) {
id viewObj = view;
UIFont *font = [viewObj font];
if ([font.fontName isEqualToString:@"AcademyEngravedLetPlain"]) {
[viewObj setFont:[UIFont fontWithName:PRIMARY_FONT size:font.pointSize]];
} else if ([font.fontName hasPrefix:@"AmericanTypewriter"]) {
[viewObj setFont:[UIFont fontWithName:SECONDARY_FONT size:font.pointSize]];
}
}
if (isSubViews) {
for (UIView *sview in view.subviews) {
[self setupFontsForView:sview andSubViews:YES];
}
}
}
回答by aViNaSh
By writing the category for the Label we can change the fonts of entire app.
通过为标签编写类别,我们可以更改整个应用程序的字体。
@implementation UILabel (CustomeFont)
-(void)awakeFromNib
{
[super awakeFromNib];
[self setBackgroundColor:[UIColor clearColor]];
[self setFont:[UIFont fontWithName:@"Helvetica" size:self.font.pointSize]];
}
@end
回答by dimo hamdy
Swift3 https://gist.github.com/dimohamdy/c47992e14c1a3ee3315c06e2480e121e
Swift3 https://gist.github.com/dimohamdy/c47992e14c1a3ee3315c06e2480e121e
you can't set appearance for label font that make me create other value of default font and when i set the new font i get only the name of new font and old size of label and create other font and set this font
你不能设置标签字体的外观,这让我创建默认字体的其他值,当我设置新字体时,我只得到新字体的名称和标签的旧大小,并创建其他字体并设置此字体
//when you want to set font for all labels in Application
UILabel.appearance().defaultFont = UIFont.systemFont(ofSize: 15/*Any Value*/, weight: UIFontWeightThin)
extension UILabel{
dynamic var defaultFont: UIFont? {
get { return self.font }
set {
//get old size of lable font
let sizeOfOldFont = self.font.pointSize
//get new name of font
let fontNameOfNewFont = newValue?.fontName
self.font = UIFont(name: fontNameOfNewFont!, size: sizeOfOldFont)
}
}
}
回答by loglesby
Raegtime's answer ala Swift...
Raegtime 的回答 ala Swift ...
import UIKit
extension UIViewController {
func setFontFamilyForView(_ fontFamily: String, view: UIView, andSubviews: Bool) {
if let label = view as? UILabel {
label.font = UIFont(name: fontFamily, size: label.font.pointSize)
}
if let textView = view as? UITextView {
textView.font = UIFont(name: fontFamily, size: textView.font!.pointSize)
}
if let textField = view as? UITextField {
textField.font = UIFont(name: fontFamily, size: textField.font!.pointSize)
}
if andSubviews {
for v in view.subviews {
setFontFamilyForView(fontFamily, view: v, andSubviews: true)
}
}
}
}
回答by Peter Kreinz
UIView+DefaultFontAndColor.h
UIView+DefaultFontAndColor.h
#import <UIKit/UIKit.h>
@interface UIView (DefaultFontAndColor)
-(void)setDefaultFontFamily:(NSString*)fontFamily andSubViews:(BOOL)isSubViews andColor:(UIColor*) color;
@end
UIView+DefaultFontAndColor.m
UIView+DefaultFontAndColor.m
#import "UIView+DefaultFontAndColor.h"
@implementation UIView (DefaultFontAndColor)
//sets the default font for view classes by default
-(void)setDefaultFontFamily:(NSString*)fontFamily andSubViews:(BOOL)isSubViews andColor: (UIColor*) color
{
if ([self isKindOfClass:[UILabel class]])
{
UILabel *lbl = (UILabel *)self;
[lbl setFont:[UIFont fontWithName:fontFamily size:[[lbl font] pointSize]]];
if( color )
lbl.textColor = color;
}
else if ([self isKindOfClass:[UIButton class]])
{
UIButton *btn = (UIButton *)self;
[btn.titleLabel setFont:[UIFont fontWithName:fontFamily size:[[btn.titleLabel font] pointSize]]];
if( color )
{
btn.tintColor = color;
}
}
if (isSubViews)
{
for (UIView *sview in self.subviews)
{
[sview setDefaultFontFamily:fontFamily andSubViews:YES andColor:color];
}
}
}
@end
@usage: without color:
@用法:没有颜色:
#import "UIView+DefaultFontAndColor.h"
UIView myView = [[UIView alloc] init]
[myView setDefaultFontFamily:@"Arial" andSubViews:YES andColor:nil];
@usage: with color:
@用法:带颜色:
#import "UIView+DefaultFontAndColor.h"
UIView myView = [[UIView alloc] init]
[myView setDefaultFontFamily:@"Arial" andSubViews:YES andColor:[UIColor greenColor] ];