UIFont

作者: 大刘 | 来源:发表于2022-06-11 18:26 被阅读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)即可

相关文章

  • 设置镂空文字

    UIFont *font = [UIFont boldSystemFontOfSize:20]; UICol...

  • swift html字符串转富文本

    func attributedString(font: UIFont = UIFont.systemFont(of...

  • YYLabel 设置 baseline 没有效果

    改成 设置 UIFont * nameFont = [UIFont yb_PingFangOfSize:14];U...

  • 导航条设置

    //导航条统一设置 UIFont *font = [UIFont fontWithName:@"Helveti...

  • 无标题文章

    //需要判断是否登录 NSString *str = @"866.44"; //设置字体UIFont UIFont...

  • UIFont

    UIFont(字体) 父类是NSObject UIFont常用方法 系统默认字体(参数是多少号字)label.fo...

  • UIFont

    UIFont的主要属性列表 属性名/类型 获得字体的family名称例:NSString *str = font....

  • UIFont

  • UIFont

    引用:https://blog.csdn.net/a_horse/article/details/83057075...

  • UIFont

    IOS使用第三方字体(ttf/otf)https://www.jianshu.com/p/55ace1bdab3b...

网友评论

    本文标题:UIFont

    本文链接:https://www.haomeiwen.com/subject/lilwmrtx.html