大刘的 iOS 自学笔记

UIFont

2022-06-11  本文已影响0人  大刘

Created by 大刘 liuxing8807@126.com

关于字体的设计,可参见: Apple

font_1.png

font.familyName和font.fontName是字体家族和字体名字

UIFont *font = [UIFont fontWithName:@"Courier-Bold" size:18];
NSLog(@"%@", font.familyName); // 字体家族:Courier
NSLog(@"%@", font.fontName);   // 字体名字:Courier-Bold

systemFontSize

NSLog(@"system fontSize: %f", UIFont.systemFontSize); // 14, 系统字体默认大小

pointSize

pointSize: The receiver’s point size, or the effective vertical point size for a font with a nonstandard matrix.
字体的pointSize可以理解成在竖直方向字体所占的点个距离,比如字体大小是80,则Label的高度为80在竖直方向上是可以容纳下这个字体的。
这个pointSize其实也就是在创建字体时指定的fontSize, 比如:

UIFont *font = [UIFont systemFontOfSize:30];
NSLog(@"pointSize: %f", font.pointSize); // 30

其他

结合上面图示了解其他常见属性意义

// ==== pointSize ====
// The receiver’s point size, or the effective vertical point size for a font with a nonstandard matrix.
UIFont *font = [UIFont systemFontOfSize:30];
// 释义:The receiver’s point size, or the effective vertical point size for a font with a nonstandard matrix.
NSLog(@"pointSize: %lf", font.pointSize); // 30
// 释义:The top y-coordinate, offset from the baseline, of the receiver’s longest ascender.
NSLog(@"ascent: %lf", font.ascender); // 28.564453
// 释义:The bottom y-coordinate, offset from the baseline, of the receiver’s longest descender.
NSLog(@"descent: %lf", font.descender); // -7.236328
// 释义:The receiver’s leading information.
NSLog(@"leading: %lf", font.leading); // 0
// 释义:The receiver’s cap height information.
NSLog(@"capHeight: %lf", font.capHeight); // capHeight: 21.137695
// 释义:The x-height of the receiver.
NSLog(@"xHeight: %lf", font.xHeight); // xHeight: 15.234375
// 释义:The height of text lines (measured in points).
NSLog(@"lineHeight: %lf", font.lineHeight); // 35.800781

使用非系统字体

  1. 把字体导入XCode
  2. 在Plist中添加对应的字体

UIFont扩展

在项目中常常由设计师指定字体,可以加入扩展方便调用,比如:

NS_ASSUME_NONNULL_BEGIN

@interface UIFont (DaLiu)

/// For [UIFont fontWithName:@"PingFangSC-Regular" size:size]
/// Syntactic sugar of code: [UIFont fontWithName:@"PingFangSC-Regular" size:size]
/// example: UIFont.pingFangSC(11)
/// call UIFont.pingFangSC_Regular inner
@property (nonatomic, class, readonly) UIFont* (^pingFangSC)(CGFloat size);

/// For [UIFont fontWithName:@"PingFangSC-Regular" size:size]
@property (nonatomic, class, readonly) UIFont* (^pingFangSC_Regular)(CGFloat size);

/// For [UIFont fontWithName:@"PingFangSC-Medium" size:size];
@property (nonatomic, class, readonly) UIFont* (^pingFangSC_Medium)(CGFloat size);

@end

NS_ASSUME_NONNULL_END
#import "UIFont+DaLiu.h"

@implementation UIFont (DaLiu)

+ (UIFont * _Nonnull (^)(CGFloat))pingFangSC {
    return [self pingFangSC_Regular];
}

+ (UIFont * _Nonnull (^)(CGFloat))pingFangSC_Regular {
    return ^UIFont *(CGFloat size) {
        return [UIFont fontWithName:@"PingFangSC-Regular" size:size];
    };
}

+ (UIFont * _Nonnull (^)(CGFloat))pingFangSC_Medium {
    return ^UIFont *(CGFloat size) {
        return [UIFont fontWithName:@"PingFangSC-Medium" size:size];
    };
}

@end

使用时调用UIFont.pingFangSC(15)即可

上一篇下一篇

猜你喜欢

热点阅读