利用CATextLayer渲染文本

本文介绍如何使用 iOS Core Text 和 CATextLayer 在视图中展示带样式的富文本,包括字体设置、颜色变化及下划线效果等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文总结自 iOS核心动画总结

该系列更多文章点击


#import "ViewController.h"
#import <CoreText/CoreText.h>

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIView *labelView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self showSomeText];
    [self showSomeTextWithAttributr];



}

- (void)showSomeTextWithAttributr {
    CATextLayer *textLayer = [CATextLayer layer];
    textLayer.frame = _labelView.bounds;
    textLayer.contentsScale = [UIScreen mainScreen].scale;
    [_labelView.layer addSublayer:textLayer];

    textLayer.alignmentMode = kCAAlignmentJustified;
    textLayer.wrapped = YES;

    UIFont *font = [UIFont systemFontOfSize:20];
    NSString *text = @"Lorem ipsum dolor sit amet, consectetur adipiscing  elit. Quisque massa arcu, eleifend vel varius in, facilisis pulvinar leo. Nunc quis nunc at mauris pharetra condimentum ut ac neque. Nuncelementum, libero ut porttitor dictum, diam odio congue lacus, velfringilla sapien diam at purus. Etiam suscipit pretium nunc sit amet lobortis";

    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:text];

    CFStringRef fontName = (__bridge CFStringRef)font.fontName;
    CGFloat fontSize = font.pointSize;
    CTFontRef fontRef = CTFontCreateWithName(fontName, fontSize, NULL);// CT

    NSDictionary *attr = @{(__bridge id)kCTForegroundColorAttributeName : (__bridge id)[UIColor redColor].CGColor,
                           (__bridge id)kCTFontAttributeName : (__bridge id) fontRef};

    [string setAttributes:attr range:NSMakeRange(0, text.length)];

    attr = @{(__bridge id)kCTForegroundColorAttributeName : (__bridge id)[UIColor yellowColor].CGColor,
             (__bridge id)kCTFontAttributeName : (__bridge id)fontRef,
             (__bridge id)kCTUnderlineStyleAttributeName : @(kCTUnderlineStyleThick)};

    [string setAttributes:attr range:NSMakeRange(4, 40)];

    CFRelease(fontRef);

    textLayer.string = string;



}

/// 利用layer显示文字,基本的
- (void)showSomeText{

    CATextLayer *textLayer = [CATextLayer layer];
    textLayer.frame = _labelView.bounds;
    [self.labelView.layer addSublayer:textLayer];

    textLayer.foregroundColor = [UIColor blueColor].CGColor;
    textLayer.alignmentMode = kCAAlignmentJustified;
    textLayer.truncationMode = kCATruncationEnd;
    textLayer.wrapped = YES; // 换行
    textLayer.contentsScale = [UIScreen mainScreen].scale;// 这个设置让根据当前屏幕进行渲染,防止像素化

    UIFont *font = [UIFont systemFontOfSize:15];
    CFStringRef fontName = (__bridge CFStringRef)font.fontName;
    CGFontRef fontRef = CGFontCreateWithFontName(fontName); // CF  这个属性可以是coreText的

    textLayer.font = fontRef;
    textLayer.fontSize = font.pointSize;
    CGFontRelease(fontRef);

    NSString *text = @"Lorem ipsum dolor sit amet, consectetur adipiscing  elit. Quisque massa arcu, eleifend vel varius in, facilisis pulvinar leo. Nunc quis nunc at mauris pharetra condimentum ut ac neque. Nunc elementum, libero ut porttitor dictum, diam odio congue lacus, vel fringilla sapien diam at purus. Etiam suscipit pretium nunc sit amet lobortis";
    textLayer.string = text;


}

文章中有部分的需要解释的稍后在编辑,有什么疑问也可以在评论留言

demo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值