UIFont
UIFont.png
屬性介紹
familyName
:字體家族的名字
fontName
:字體的名字
pointSize
:字號大小
ascender
:基準線以上的高度
descender
:基準線以下的高度(負數(shù))
capHeight
:大寫字母的高度
xHeight
:小寫x的高度
lineHeight
:行高
leading
:行間距(一般為0)
NSString *text = @"字體高度??";
UIFont *font = [UIFont fontWithName:@"Didot Italic" size:18];
NSLog(@"字體信息:\npointSize:%lf,\nascender:%lf,\ndescender:%lf,\ncapHeight:%lf,\nxHeight:%lf,\nlineHeight:%lf,\nleading:%lf",font.pointSize,font.ascender,font.descender,font.capHeight,font.xHeight,font.lineHeight,font.leading);
字體信息:
pointSize:18.000000,
ascender:16.956000,
descender:-5.220000,
capHeight:12.942000,
xHeight:7.956000,
lineHeight:22.176000,
leading:0.450000
1.設(shè)置的字體大小就是 pointSize
细移。
-
lineHeight
=ascender + descender
(按照圖上所示應(yīng)該為lineHeight
=ascender + descender +leading
,但是實際查看了幾個字體汪茧,都是lineHeight
=ascender + descender
)通危。
3.實際行與行之間就是存在間隙的,間隙大小即為 lineHeight - pointSize
微渠,在富文本中設(shè)置行高的時候,其實際文字間的距離就是加上這個距離的盏浇。(原來一直錯誤的理解文字間的距離就是行間距)匠璧。
行數(shù)計算(猜想)
text
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UIFont *font = [UIFont fontWithName:@"Didot Italic" size:18];
NSLog(@"字體信息:\npointSize:%lf,\nascender:%lf,\ndescender:%lf,\ncapHeight:%lf,\nxHeight:%lf,\nlineHeight:%lf,\nleading:%lf",font.pointSize,font.ascender,font.descender,font.capHeight,font.xHeight,font.lineHeight,font.leading);
NSString *text = @"字體高度??";
CGSize size= [text boundingRectWithSize:CGSizeMake(300, CGFLOAT_MAX)
options:NSStringDrawingTruncatesLastVisibleLine
attributes:@{NSFontAttributeName:font}
context:nil].size;
NSLog(@"文本計算:%lf,%lf",size.width,size.height);
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(30, 0, size.width, size.height);
label.backgroundColor = [UIColor redColor];
label.numberOfLines = 0;
label.text = text;
label.font = font;
[self.view addSubview:label];
NSLog(@"label展示:%lf,%lf",label.frame.size.width,label.frame.size.height);
UILabel *label1 = [[UILabel alloc] init];
label1.frame = CGRectMake(30, 100, 300, 300);
label1.backgroundColor = [UIColor redColor];
label1.numberOfLines = 0;
label1.text = text;
label1.font = font;
[label1 sizeToFit];
[self.view addSubview:label1];
NSLog(@"label1展示:%lf,%lf",label1.frame.size.width,label1.frame.size.height);
self.label2 = [[UILabel alloc] init];
self.label2.backgroundColor = [UIColor redColor];
self.label2.numberOfLines = 0;
self.label2.text = text;
self.label2.font = font;
[self.view addSubview:self.label2];
[self.label2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(30);
make.top.mas_equalTo(200);
}];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
NSLog(@"label2展示:%lf,%lf",self.label2.frame.size.width,self.label2.frame.size.height);
}
字體信息:
pointSize:18.000000,
ascender:16.956000,
descender:-5.220000,
capHeight:12.942000,
xHeight:7.956000,
lineHeight:22.176000,
leading:0.450000
//iOS16.4 iPhone14
/**字體高度??
*/
文本計算:94.000000,22.176000
label展示:94.000000,22.176000
label1展示:94.000000,22.333333
label2展示:94.000000,22.333333
/**字體高度??\n字體高度??
*/
文本計算:94.000000,44.352000
label展示:94.000000,44.352000
label1展示:94.000000,44.666667
label2展示:94.000000,44.666667
/**字體高度3??\n字體高度3??\n字體高度3??
*/
文本計算:102.676000,66.528000
label展示:102.676000,66.528000
label1展示:103.000000,66.666667
label2展示:103.000000,66.666667
通過boundingRectWithSize...
方法獲取的文本高度為字體的lineHeight*行數(shù)
。是否可以通過高度計算除以字體的lineHeight
判斷是單行還是雙行鲤妥??拱雏?
富文本
UIFont *font = [UIFont fontWithName:@"Didot Italic" size:18];
NSLog(@"字體信息:\npointSize:%lf,\nascender:%lf,\ndescender:%lf,\ncapHeight:%lf,\nxHeight:%lf,\nlineHeight:%lf,\nleading:%lf",font.pointSize,font.ascender,font.descender,font.capHeight,font.xHeight,font.lineHeight,font.leading);
NSString *text = @"字體高度??";
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
style.lineBreakMode = NSLineBreakByWordWrapping;
style.alignment = NSTextAlignmentRight;
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:style forKey:NSParagraphStyleAttributeName];
[attributes setObject:@(-3) forKey:NSBaselineOffsetAttributeName];
[attributes setObject:font forKey:NSFontAttributeName];
NSAttributedString *att = [[NSAttributedString alloc] initWithString:text attributes:attributes];
CGSize size = [att boundingRectWithSize:CGSizeMake(300, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
字體信息:
pointSize:18.000000,
ascender:16.956000,
descender:-5.220000,
capHeight:12.942000,
xHeight:7.956000,
lineHeight:22.176000,
leading:0.450000
//iOS16.4 iPhone14
/**字體高度??
*/
文本計算:94.000000,25.176000
label展示:94.000000,25.176000
label1展示:94.000000,25.333333
label2展示:94.000000,25.333333
/**字體高度??\nwww
*/
文本計算:94.000000,50.352000
label展示:94.000000,50.352000
label1展示:94.000000,50.666667
label2展示:94.000000,50.666667
/**字體高度??\nwww\n123
*/
文本計算:94.000000,75.528000
label展示:94.000000,75.528000
label1展示:94.000000,75.666667
label2展示:94.000000,75.666667
富文本部分屬性設(shè)置會影響當行字體的高度棉安,但應(yīng)該也可以通過高度計算除以單行高度判斷文本行數(shù),如該實例中單行高度為lineHeight+NSBaselineOffsetAttributeName
铸抑。
注意:sizeToFit
和Masonry
會根據(jù)屏幕分辨率向上適配寬高贡耽。
參考文檔
iOS_UIFont的Attributes解析
iOS 對UIFont的新的理解
UILabel行間距l(xiāng)ineSpacing 行高lineHeight