ios 开发--字体

#import 

NS_ASSUME_NONNULL_BEGIN

typedef NSString * UIFontName NS_TYPED_ENUM;

// Default
static UIFontName const UIFontName_Default                          = @"__sys_default_font";

static UIFontName const UIFontName_DINAlternateBold                 = @"DINAlternate-Bold";
// DIN Condensed
static UIFontName const UIFontName_DINCondensedBold                 = @"DINCondensed-Bold";

// Avenir Next Condensed
static UIFontName const UIFontName_AvenirNextCondensed              = @"AvenirNextCondensed-DemiBold";

// Helvetica
static UIFontName const UIFontName_Helvetica                        = @"Helvetica";
static UIFontName const UIFontName_HelveticaBold                    = @"Helvetica-Bold";
static UIFontName const UIFontName_HelveticaLight                   = @"Helvetica-Light";
static UIFontName const UIFontName_HelveticaOblique                 = @"Helvetica-Oblique";
static UIFontName const UIFontName_HelveticaBoldOblique             = @"Helvetica-BoldOblique";
static UIFontName const UIFontName_HelveticaLightOblique            = @"Helvetica-LightOblique";

// Helvetica Neue
static UIFontName const UIFontName_HelveticaNeue                    = @"HelveticaNeue";
static UIFontName const UIFontName_HelveticaNeueItalic              = @"HelveticaNeue-Italic";
static UIFontName const UIFontName_HelveticaNeueMedium              = @"HelveticaNeue-Medium";
static UIFontName const UIFontName_HelveticaNeueBold                = @"HelveticaNeue-Bold";
static UIFontName const UIFontName_HelveticaNeueLight               = @"HelveticaNeue-Light";
static UIFontName const UIFontName_HelveticaNeueUltraLight          = @"HelveticaNeue-UltraLight";
static UIFontName const UIFontName_HelveticaNeueThin                = @"HelveticaNeue-Thin";
static UIFontName const UIFontName_HelveticaNeueCondensedBlack      = @"HelveticaNeue-CondensedBlack";
static UIFontName const UIFontName_HelveticaNeueBoldItalic          = @"HelveticaNeue-BoldItalic";
static UIFontName const UIFontName_HelveticaNeueCondensedBold       = @"HelveticaNeue-CondensedBold";
static UIFontName const UIFontName_HelveticaNeueThinItalic          = @"HelveticaNeue-ThinItalic";
static UIFontName const UIFontName_HelveticaNeueLightItalic         = @"HelveticaNeue-LightItalic";
static UIFontName const UIFontName_HelveticaNeueUltraLightItalic    = @"HelveticaNeue-UltraLightItalic";
static UIFontName const UIFontName_HelveticaNeueMediumItalic        = @"HelveticaNeue-MediumItalic";

// PingFang SC
static UIFontName const UIFontName_PingFangSCRegular                = @"PingFangSC-Regular";
static UIFontName const UIFontName_PingFangSCMedium                 = @"PingFangSC-Medium";
static UIFontName const UIFontName_PingFangSCSemibold               = @"PingFangSC-Semibold";
static UIFontName const UIFontName_PingFangSCLight                  = @"PingFangSC-Light";
static UIFontName const UIFontName_PingFangSCThin                   = @"PingFangSC-Thin";
static UIFontName const UIFontName_PingFangSCUltralight             = @"PingFangSC-Ultralight";

@interface UIFont (Ext)

/**
 查询系统中可用的字体集合, 该方法只会调用一次,之后的查询都会使用缓存值

 @return 字体名字集合
 */
+ (NSArray *)useableFontNames;

/**
 获取一个系统中存在的字体, 如果系统中不存在此字体则返回默认字体.

 @param name 字体名称
 @param size 字体大小
 @return 一个字体实例
 */
+ (UIFont *)sw_fontWithName:(UIFontName)name size:(CGFloat)size;

@end

NS_ASSUME_NONNULL_END

@implementation UIFont (Ext)

+ (NSArray *)useableFontNames {
    static id res = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        res = [[UIFont familyNames] reduce:@[] body:^id _Nonnull(NSArray* result, NSString * _Nonnull name) {
            return [result arrayByAddingObjectsFromArray:[UIFont fontNamesForFamilyName:name]];
        }];
        
    });
    return res;
}

+ (UIFont *)sw_fontWithName:(UIFontName)name size:(CGFloat)size {
    if ([name isEqualToString:UIFontName_Default]) {
        return [self systemFontOfSize:size];
    }
    
    if ([[self useableFontNames] containsObject:name] == NO) {
        return [self systemFontOfSize:size];
    }
    
    return [self fontWithName:name size:size];
}

@end

注:

- (id)reduce:(id)initial body:(id (^)(id, id))body; 
//参考NSArray+Block

你可能感兴趣的:(ios 开发--字体)