YYText

2023-05-16

特性

  • API 兼容 UILabel 和 UITextView
  • 支持高性能的异步排版和渲染
  • 扩展了 CoreText 的属性以支持更多文字效果
  • 支持 UIImage、UIView、CALayer 作为图文混排元素
  • 支持添加自定义样式的、可点击的文本高亮范围
  • 支持自定义文本解析 (内置简单的 Markdown/表情解析)
  • 支持文本容器路径、内部留空路径的控制
  • 支持文字竖排版,可用于编辑和显示中日韩文本
  • 支持图片和富文本的复制粘贴
  • 文本编辑时,支持富文本占位符
  • 支持自定义键盘视图
  • 撤销和重做次数的控制
  • 富文本的序列化与反序列化支持
  • 支持多语言,支持 VoiceOver
  • 支持 Interface Builder
  • 全部代码都有文档注释

架构

YYText 和 TextKit 架构对比

文本属性

YYText 原生支持的属性

DemoAttribute NameClass
TextAttachmentYYTextAttachment
TextHighlightYYTextHighlight
TextBindingYYTextBinding
TextShadow
TextInnerShadow
YYTextShadow
TextBorderYYTextBorder
TextBackgroundBorderYYTextBorder
TextBlockBorderYYTextBorder
TextGlyphTransformNSValue(CGAffineTransform)
TextUnderlineYYTextDecoration
TextStrickthroughYYTextDecoration
TextBackedStringYYTextBackedString

YYText 支持的 CoreText 属性

DemoAttribute NameClass
Font UIFont(CTFontRef)
Kern NSNumber
StrokeWidth NSNumber 
StrokeColor CGColorRef 
Shadow NSShadow 
Ligature NSNumber 
VerticalGlyphForm NSNumber(BOOL) 
WritingDirection NSArray(NSNumber) 
RunDelegate CTRunDelegateRef 
TextAlignment NSParagraphStyle 
(NSTextAlignment) 
LineBreakMode NSParagraphStyle 
(NSLineBreakMode) 
LineSpacing NSParagraphStyle 
(CGFloat) 
ParagraphSpacing 
ParagraphSpacingBefore 
NSParagraphStyle 
(CGFloat) 
FirstLineHeadIndent NSParagraphStyle 
(CGFloat) 
HeadIndent NSParagraphStyle 
(CGFloat) 
TailIndent NSParagraphStyle 
(CGFloat) 
MinimumLineHeight NSParagraphStyle 
(CGFloat) 
MaximumLineHeight NSParagraphStyle 
(CGFloat) 
LineHeightMultiple NSParagraphStyle 
(CGFloat) 
BaseWritingDirection NSParagraphStyle 
(NSWritingDirection) 
DefaultTabInterval 
TabStops 
NSParagraphStyle 
CGFloat/NSArray(NSTextTab)

用法

基本用法

// YYLabel (和 UILabel 用法一致)
YYLabel *label = [YYLabel new];
label.frame = ...
label.font = ...
label.textColor = ...
label.textAlignment = ...
label.lineBreakMode = ...
label.numberOfLines = ...
label.text = ...

// YYTextView (和 UITextView 用法一致)
YYTextView *textView = [YYTextView new];
textView.frame = ...
textView.font = ...
textView.textColor = ...
textView.dataDetectorTypes = ...
textView.placeHolderText = ...
textView.placeHolderTextColor = ...
textView.delegate = ...

属性文本

// 1. 创建一个属性文本
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"Some Text, blabla..."];

// 2. 为文本设置属性
text.yy_font = [UIFont boldSystemFontOfSize:30];
text.yy_color = [UIColor blueColor];
[text yy_setColor:[UIColor redColor] range:NSmakeRange(0, 4)];
text.yy_lineSpacing = 10;

// 3. 赋值到 YYLabel 或 YYTextView
YYLabel *label = [YYLabel new];
label.frame = ...
label.attributedString = text;

YYTextView *textView = [YYTextView new];
textView.frame = ...
textView.attributedString = text;

文本高亮

你可以用一些已经封装好的简便方法来设置文本高亮:

[text yy_setTextHighlightRange:range
                        color:[UIColor blueColor]
              backgroundColor:[UIColor grayColor]
                    tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){ 
                        NSLog(@"tap text range:..."); 
                    }];

或者用更复杂的办法来调节文本高亮的细节:

// 1. 创建一个"高亮"属性,当用户点击了高亮区域的文本时,"高亮"属性会替换掉原本的属性
YYTextBorder *border = [YYTextBorder borderWithFillColor:[UIColor grayColor] cornerRadius:3];

YYTextHighlight *highlight = [YYTextHighlight new];
[highlight setColor:[UIColor whiteColor]];
[highlight setBackgroundBorder:highlightBorder];
highlight.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
  NSLog(@"tap text range:..."); 
  // 你也可以把事件回调放到 YYLabel 和 YYTextView 来处理。
};

// 2. 把"高亮"属性设置到某个文本范围
[attributedText yy_setTextHighlight:highlight range:highlightRange];

// 3. 把属性文本设置到 YYLabel 或 YYTextView
YYLabel *label = ...
label.attributedText = attributedText

YYTextView *textView = ...
textView.attributedText = ...

// 4. 接受事件回调
label.highlightTapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
    NSLog(@"tap text range:...");
};
label.highlightLongPressAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
    NSLog(@"long press text range:...");
};

@UITextViewDelegate
- (void)textView:(YYTextView *)textView didTapHighlight:(YYTextHighlight *)highlight inRange:(NSRange)characterRange rect:(CGRect)rect {
    NSLog(@"tap text range:...");
}
- (void)textView:(YYTextView *)textView didLongPressHighlight:(YYTextHighlight *)highlight inRange:(NSRange)characterRange rect:(CGRect)rect {
    NSLog(@"long press text range:...");
}

图文混排

NSMutableAttributedString *text = [NSMutableAttributedString new];
UIFont *font = [UIFont systemFontOfSize:16];
NSMutableAttributedString *attachment = nil;

// 嵌入 UIImage
UIImage *image = [UIImage imageNamed:@"dribbble64_imageio"];
attachment = [NSMutableAttributedString yy_attachmentStringWithContent:image contentMode:UIViewContentModeCenter attachmentSize:image.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
[text appendAttributedString: attachment];

// 嵌入 UIView
UISwitch *switcher = [UISwitch new];
[switcher sizeToFit];
attachment = [NSMutableAttributedString yy_attachmentStringWithContent:switcher contentMode:UIViewContentModeBottom attachmentSize:switcher.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
[text appendAttributedString: attachment];

// 嵌入 CALayer
CASharpLayer *layer = [CASharpLayer layer];
layer.path = ...
attachment = [NSMutableAttributedString yy_attachmentStringWithContent:layer contentMode:UIViewContentModeBottom attachmentSize:switcher.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
[text appendAttributedString: attachment];

文本布局计算

NSAttributedString *text = ...
CGSize size = CGSizeMake(100, CGFLOAT_MAX);
YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:size text:text];

// 获取文本显示位置和大小
layout.textBoundingRect; // get bounding rect
layout.textBoundingSize; // get bounding size

 // 查询文本排版结果
[layout lineIndexForPoint:CGPointMake(10,10)];
[layout closestLineIndexForPoint:CGPointMake(10,10)];
[layout closestPositionToPoint:CGPointMake(10,10)];
[layout textRangeAtPoint:CGPointMake(10,10)];
[layout rectForRange:[YYTextRange rangeWithRange:NSMakeRange(10,2)]];
[layout selectionRectsForRange:[YYTextRange rangeWithRange:NSMakeRange(10,2)]];

// 显示文本排版结果
YYLabel *label = [YYLabel new];
label.size = layout.textBoundingSize;
label.textLayout = layout;

文本行位置调整

// 由于中文、英文、Emoji 等字体高度不一致,或者富文本中出现了不同字号的字体,
// 可能会造成每行文字的高度不一致。这里可以添加一个修改器来实现固定行高,或者自定义文本行位置。

// 简单的方法:
// 1. 创建一个文本行位置修改类,实现 `YYTextLinePositionModifier` 协议。
// 2. 设置到 Label 或 TextView。

YYTextLinePositionSimpleModifier *modifier = [YYTextLinePositionSimpleModifier new];
modifier.fixedLineHeight = 24;

YYLabel *label = [YYLabel new];
label.linePositionModifier = modifier;

// 完全控制:
YYTextLinePositionSimpleModifier *modifier = [YYTextLinePositionSimpleModifier new];
modifier.fixedLineHeight = 24;

YYTextContainer *container = [YYTextContainer new];
container.size = CGSizeMake(100, CGFLOAT_MAX);
container.linePositionModifier = modifier;

YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:text];
YYLabel *label = [YYLabel new];
label.size = layout.textBoundingSize;
label.textLayout = layout;

异步排版和渲染

// 如果你在显示字符串时有性能问题,可以这样开启异步模式:
YYLabel *label = ...
label.displaysAsynchronously = YES;

// 如果需要获得最高的性能,你可以在后台线程用 `YYTextLayout` 进行预排版: 
YYLabel *label = [YYLabel new];
label.displaysAsynchronously = YES; //开启异步绘制
label.ignoreCommonProperties = YES; //忽略除了 textLayout 之外的其他属性

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // 创建属性字符串
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"Some Text"];
    text.yy_font = [UIFont systemFontOfSize:16];
    text.yy_color = [UIColor grayColor];
    [text yy_setColor:[UIColor redColor] range:NSMakeRange(0, 4)];

    // 创建文本容器
    YYTextContainer *container = [YYTextContainer new];
    container.size = CGSizeMake(100, CGFLOAT_MAX);
    container.maximumNumberOfRows = 0;

    // 生成排版结果
    YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:text];

    dispatch_async(dispatch_get_main_queue(), ^{
        label.size = layout.textBoundingSize;
        label.textLayout = layout;
    });
});

文本容器控制

YYLabel *label = ...
label.textContainerPath = [UIBezierPath bezierPathWith...];
label.exclusionPaths =  @[[UIBezierPath bezierPathWith...];,...];
label.textContainerInset = UIEdgeInsetsMake(...);
label.verticalForm = YES/NO;

YYTextView *textView = ...
textView.exclusionPaths =   @[[UIBezierPath bezierPathWith...];,...];
textView.textContainerInset = UIEdgeInsetsMake(...);
textView.verticalForm = YES/NO;

文本解析

// 1. 创建一个解析器

// 内置简单的表情解析
YYTextSimpleEmoticonParser *parser = [YYTextSimpleEmoticonParser new];
NSMutableDictionary *mapper = [NSMutableDictionary new];
mapper[@":smile:"] = [UIImage imageNamed:@"smile.png"];
mapper[@":cool:"] = [UIImage imageNamed:@"cool.png"];
mapper[@":cry:"] = [UIImage imageNamed:@"cry.png"];
mapper[@":wink:"] = [UIImage imageNamed:@"wink.png"];
parser.emoticonMapper = mapper;

// 内置简单的 markdown 解析
YYTextSimpleMarkdownParser *parser = [YYTextSimpleMarkdownParser new];
[parser setColorWithDarkTheme];

// 实现 `YYTextParser` 协议的自定义解析器
MyCustomParser *parser = ... 

// 2. 把解析器添加到 YYLabel 或 YYTextView
YYLabel *label = ...
label.textParser = parser;

YYTextView *textView = ...
textView.textParser = parser;

更多示

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

YYText 的相关文章

随机推荐

  • 端口扫描器设计实现(Python)

    一 个人感悟 通过本次实验 学习了扫描器设计的基本原理 并动手设计了一个开放端口扫描器 具体原理 1 编写前端GUI 2 学习Socket编程 使用Socket编程的connect方法返回0 为连接成功 实现端口扫描器 改进的地方 如果se
  • Maven配置文件settings.xml详解

    一 配置分级 首先需要了解的是Maven配置文件以及配置方式的分级 xff0c 才能做到我们自己在日常开发配置中灵活使用 根据Maven官方资料 xff0c Maven将配置文件分为两级 xff1a 用户层级全局层级 Maven在运行的时候
  • 移动端视觉SLAM学习笔记 (五) 参数设置

    考虑到后续的各项参数 xff0c 这里先把配置文件和参数设置界面做好 xff0c 后面增加的参数就都可以添加进来了 Android本身参数设置的框架 xff0c 包括配置文件和设置界面 xff0c 也就是PreferenceFragment
  • Lambda — Stream API常用工具类

    最近发现代码里面大量处理集合的lambda表达式 xff0c 所以想着封装个常用工具类 xff0c 用来快速调用 xff0c 直接贴代码 只需传入集合 xff0c Function xff0c Predicate xff0c Compara
  • Java获取当前的UTC时间

    Java获取当前的UTC时间 Java获取当前的UTC时间Calendar与TimeZone结论 xff1a 因此想要获取UTC时间可以修改系统默认时区或转换时区修改系统默认时区SimpleDateFormat 转换时区 Instant J
  • springboot+security报错405方法不支持解决方法

    在使用 Spring Boot 和 Spring Security 进行 Web 开发的过程中 xff0c 有时候会遇到 HTTP 错误码为 405 的错误 HTTP 错误码 405 表示 方法不允许 xff0c 也就是客户端请求使用的 H
  • pip 安装faiss 不能用,使用conda安装并使用faiss成功

    pip 安装faiss 安装过程没有报错 xff0c import时候必报错 xff01 xff01 xff01 1 安装cpu版本faiss pip default time 61 1000 install i https pypi tu
  • busybox 网络相关

    busybox 瑞士军刀 包含很多linux unix 平台的有用utilities 比如sh ls cat grep touch chmod chown cp mv vi stat等等等 包括系统常用工具 编辑器 shell 文件操作 文
  • linux glibc2.12到2.14升级

    linux centos glibc 2 12 到 2 14 有些软件可能要求系统的 Glibc 高于某个版本才可以正常运行 如果您的 Glibc 低于要求的版本 xff0c 为了运行这些软件 xff0c 您就不得不升级您的 Glibc 了
  • Ubuntu 开机自定义脚本启动(最全版)

    一 背景 同伴在频繁更新系统环境 xff0c 需要经常使用reboot命令重启 xff0c 但每次重启后端Jar都会停止 xff0c 每次重启都需要手动启动Web后端Jar包 针对此种情况 xff0c 想到了采用开机自动启动Jar包的方法来
  • pycharm 中让python程序暂停的几种方法

    1 input 这种方法不用包含模块 xff0c 因此这也是最常用的一种暂停手段 Python2中的raw input 和input 语句在Python3中已经被合并到input 中 2 os system 34 pause 34 这种方法
  • 关于 Win32API 中的 LoadLibrary

    有人说 反射反射 xff0c 程序员的法宝 这句话一点也没有错 如果能够动态的加载类和方法 xff0c 就等于彻底的解除了类和类 xff0c 方法和方法的耦合 当然 xff0c 这样编译器就无法帮你检查出其中的错误了 xff0c 还是有一定
  • ViewPage+RadioGroup实现Tab切换

    以前写过一篇也是Tab切换的文章 xff0c 当时做安卓一年左右 xff0c 单纯觉得这样能实现功能 xff0c 但是没有注意到性能和代码简洁性的问题 xff0c 文章为 http blog csdn net nzzl54 article
  • 学科前沿技术(高性能计算机)

    高性能计算机 xff1a 二十世纪八 九十年代是高性能计算机丰富多彩的时期 xff0c 各种体系结构的系统争奇斗妍 进入二十一世纪 xff0c 高性能计算机产业逐渐成熟 xff0c 用户面大大扩大 xff0c 技术也不断聚焦 工业界认为 x
  • 异常行为分析模型设计

    本文针对异常访问现状及问题进行简要描述 xff0c 在此基础上提出基于一元线性回归的最小二乘法异常访问分析模型 xff0c 通过该模型解决了异常访问中时间与访问间相关性问题 异常访问是指网络行为偏离正常范围的访问情况 异常访问包含多种场景
  • CSS 参考手册

    CSS 参考手册 CSS 参考手册 CSS 选择器 W3School 的 CSS 参考手册定期通过所有主流浏览器进行测试 CSS 属性 CSS 属性组 xff1a 动画背景边框和轮廓盒 xff08 框 xff09 颜色内容分页媒体定位可伸缩
  • C语言求n的阶乘(n!)

    从键盘输入一个数 xff0c 求出这个数的阶乘 xff0c 即 n 算法思想 首先要清楚阶乘定义 xff0c 所谓 n 的阶乘 xff0c 就是从 1 开始乘以比前一个数大 1 的数 xff0c 一直乘到 n xff0c 用公式表示就是 x
  • Oracle grant all privileges to user

    测试流程 xff1a 解锁HR用户 SQL gt alter user hr account unlock identified by hr User altered 创建一个test用户 SYS 64 orcl11g gt CREATE
  • 《自己动手写Docker》书摘之一: Linux Namespace

    Linux Namespace 介绍 我们经常听到说Docker 是一个使用了Linux Namespace 和 Cgroups 的虚拟化工具 xff0c 但是什么是Linux Namespace 它在Docker内是怎么被使用的 xff0
  • YYText

    特性 API 兼容 UILabel 和 UITextView支持高性能的异步排版和渲染扩展了 CoreText 的属性以支持更多文字效果支持 UIImage UIView CALayer 作为图文混排元素支持添加自定义样式的 可点击的文本高