我正在尝试在iOS app
中应用自定义字体。我发现我可以使用:
[[UILabel appearance] setFont:[UIFont fontWithName:@"Proxima Nova" size:17.0]];
设置所有UILabels
的默认字体和大小。但是,并非所有UILabels
共享相同的字体大小。
在Set a default font for whole iOS app?中,有人有同样的顾虑,并被告知将size参数设置为0.0以仅设置字体而不是字体大小。
UILabel
文字都消失了(因为显然iOS
字面上取了0.0字体大小)。 关于如何普遍设置字体而不是大小的任何建议?非常感谢!
答案 0 :(得分:24)
- (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];
}
}
}
答案 1 :(得分:19)
以下是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
然后,您可以使用以下内容更改Apperance
[[UILabel appearance] setSubstituteFontName:@"SourceSansPro-Light"];
答案 2 :(得分:12)
我在我的项目中使用了已接受的答案,但需要更通用的功能,因此它会将字体更改为每个可能的字体,我也选择将某些字体字体之间的映射设置为我们的自定义字体,因此它们也可以通过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];
}
}
}
答案 3 :(得分:7)
通过编写Label的类别,我们可以更改整个应用程序的字体。
@implementation UILabel (CustomeFont)
-(void)awakeFromNib
{
[super awakeFromNib];
[self setBackgroundColor:[UIColor clearColor]];
[self setFont:[UIFont fontWithName:@"Helvetica" size:self.font.pointSize]];
}
@end
答案 4 :(得分:5)
Swift3 https://gist.github.com/dimohamdy/c47992e14c1a3ee3315c06e2480e121e
你不能设置标签字体的外观让我创建默认字体的其他值,当我设置新字体时我只得到新字体的名称和旧标签的大小并创建其他字体并设置此字体< / p>
//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)
}
}
}
答案 5 :(得分:3)
的UIView + DefaultFontAndColor.h
#import <UIKit/UIKit.h>
@interface UIView (DefaultFontAndColor)
-(void)setDefaultFontFamily:(NSString*)fontFamily andSubViews:(BOOL)isSubViews andColor:(UIColor*) color;
@end
的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:没有颜色:
#import "UIView+DefaultFontAndColor.h"
UIView myView = [[UIView alloc] init]
[myView setDefaultFontFamily:@"Arial" andSubViews:YES andColor:nil];
@usage:带颜色:
#import "UIView+DefaultFontAndColor.h"
UIView myView = [[UIView alloc] init]
[myView setDefaultFontFamily:@"Arial" andSubViews:YES andColor:[UIColor greenColor] ];
答案 6 :(得分:2)
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)
}
}
}
}