CoreText

1. 基础知识

在这里插入图片描述

2. 示例

self.view.backgroundColor = [UIColor darkGrayColor];
    UIFont *font15 = [UIFont systemFontOfSize:15];
    UIFont *font30 = [UIFont systemFontOfSize:30];
    NSDictionary *dic1 = @{NSFontAttributeName:font15,
                           NSForegroundColorAttributeName:[UIColor blackColor],
                           NSBackgroundColorAttributeName:[UIColor yellowColor],
                           NSBaselineOffsetAttributeName:@0,
//                           NSBaselineOffsetAttributeName:@((font30.lineHeight - font15.lineHeight)/2 + ((font30.descender - font15.descender)))
    };
    NSDictionary *dic2 = @{NSFontAttributeName:font30,
                           NSForegroundColorAttributeName:[UIColor redColor],
                           NSBackgroundColorAttributeName:[UIColor yellowColor],
                           NSBaselineOffsetAttributeName:@0
    };
    NSMutableAttributedString *attributeStr1 =  [[NSMutableAttributedString alloc] initWithString:@"a" attributes:dic1];
    NSAttributedString *attributeStr2 =  [[NSAttributedString alloc] initWithString:@"Aa" attributes:dic2];
    [attributeStr1 appendAttributedString:attributeStr2];
    
    // - 图片
    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
    attachment.image = [UIImage imageNamed:@"ab"];
    attachment.bounds = CGRectMake(0,0,font30.ascender,font30.ascender);
    [attributeStr1 appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]];
    
    
    CGRect rect = [attributeStr1 boundingRectWithSize:CGSizeMake(600, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:NULL];
    NSLog(@"lineHeight : %f %f",font15.lineHeight, font30.lineHeight);
    NSLog(@"pointSize : %f %f",font15.pointSize, font30.pointSize);
    NSLog(@"descender : %f %f",font15.descender, font30.descender);
    NSLog(@"ascender : %f %f",font15.ascender, font30.ascender);
    NSLog(@"leading : %f %f",font15.leading, font30.leading);
    
    NSLog(@"descender + ascender + leading = %f, %f",font15.ascender - font15.descender + font15.leading, font30.ascender - font30.descender + font30.leading);
    NSLog(@"boundingRectWithSizeHeight : %f",rect.size.height);
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 600, 100)];
    label.attributedText = attributeStr1;
    label.numberOfLines = 0;
    [self.view addSubview:label];
    
2021-11-23 16:38:37.268589+0800 Demo_03[18590:4777570] lineHeight : 17.900391 35.800781
2021-11-23 16:38:37.268675+0800 Demo_03[18590:4777570] pointSize : 15.000000 30.000000
2021-11-23 16:38:37.268705+0800 Demo_03[18590:4777570] descender : -3.618164 -7.236328
2021-11-23 16:38:37.268728+0800 Demo_03[18590:4777570] ascender : 14.282227 28.564453
2021-11-23 16:38:37.268753+0800 Demo_03[18590:4777570] leading : 0.000000 0.000000
2021-11-23 16:38:37.268776+0800 Demo_03[18590:4777570] descender + ascender + leading = 17.900391, 35.800781
2021-11-23 16:38:37.268800+0800 Demo_03[18590:4777570] boundingRectWithSizeHeight : 35.800781

3. 原理

在这里插入图片描述

4. 方法

  1. 默认样式
    在这里插入图片描述

  2. 让两个字体的文字居中对齐
    在这里插入图片描述

	NSBaselineOffsetAttributeName:@((font30.lineHeight - font15.lineHeight)/2 + ((font30.descender - font15.descender)))
  1. 让图片居中对齐
    在这里插入图片描述
	attachment.bounds = CGRectMake(0,font30.descender,font30.lineHeight,font30.lineHeight);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值