YYText官方文档 (官方文档写的也很清楚,容易上手)
DWCoreTextLabel带有选择文本效果的LabelView
一:YYText分为三大模块:
1、String模块:提供YYText属性,可以设置更多丰富的扩展富文本属性功能。
1》YYTextAttribute :文本属性
YYTextBackedStringAttributeName 》YYTextBackedString :附件携带原始文本
YYTextBindingAttributeName 》 YYTextBinding :将一系列文本绑定在一起,就像单个字符一样
YYTextShadowAttributeName 》YYTextShadow : 向文本范围添加阴影
YYTextInnerShadowAttributeName 》YYTextShadow :内部阴影添加到文本范围
YYTextUnderlineAttributeName 》YYTextDecoration :可向文本范围添加下划线
YYTextStrikethroughAttributeName 》 YYTextDecoration :删除线
YYTextBorderAttributeName 》YYTextBorder :向文本范围添加 封面边框 或 封面颜色
YYTextBackgroundBorderAttributeName 》YYTextBorder :将背景边框 或 背景色 添加到文本范围
YYTextBlockBorderAttributeName 》YYTextBorder :可向一行或多行文本添加代码块边框
YYTextAttachmentAttributeName 》YYTextAttachment :可将附件添加到文本。(图片/view/layer)
YYTextHighlightAttributeName 》YYTextHighlight :可向文本范围添加 “可触摸”的高亮显示状态
YYTextGlyphTransformAttributeName :添加图示符
2》YYTextParser :表情解析、YYTextSimpleMarkdownParser:Markdown解析(简单解析,全面解析用其他工具,文档里有链接)
2、Utility模块:NSAttributedString+YYText.h 富文本扩展、NSParagraphStyle+YYText.h段落功能扩展
1》NSAttributedString+YYText.h 系统常规属性 的获取 和 设置
可获取的字符串属性:
yy_font:字体
yy_kern:字符串间距
yy_color:字体颜色(字符前景色)
yy_backgroundColor:字符串背景颜色
yy_strokeWidth:字符笔画宽度
yy_strokeColor:字符串笔画颜色(系统字符的前景色)
yy_shadow:阴影
yy_strikethroughStyle:删除线样式、yy_strikethroughColor:删除线颜色
yy_underlineStyle:下划线样式、yy_underlineColor:下划线颜色
yy_baselineOffset:相对于基线的偏移量(以点为单位)
yy_language:指定文本语言
获取段落的属性:
yy_paragraphStyle:段落样式
yy_alignment:字符串对齐方式
yy_lineBreakMode:字符串转行模式
yy_lineSpacing:行间距
yy_paragraphSpacing:段间距
yy_paragraphSpacingBefore:段落顶部与其文本内容开头之间的距离
yy_firstLineHeadIndent:第一行的缩进
yy_headIndent:除第一行以外的接收器行的缩进
yy_tailIndent:尾部缩进
yy_minimumLineHeight:接收器的行最小高度
yy_maximumLineHeight:接收器的行最大高度
2》获取YYText属性
yy_textShadow:文字阴影 YYTextShadow
yy_textInnerShadow:文字内部阴影 YYTextShadow
yy_textUnderline:下划线 YYTextDecoration
yy_textStrikethrough:删除线 YYTextDecoration
yy_textBorder:文字边框 YYTextBorder
yy_textBackgroundBorder:文字背景边框 YYTextBorder
yy_textGlyphTransform:文字变形 CGAffineTransform
3》截取字符串:- (nullable NSString *)yy_plainTextForRange:(NSRange)range;
- (NSRange)yy_rangeOfAll; //获取字符串 范围
4》创建文字附件attachment
+ (NSMutableAttributedString *)yy_attachmentStringWithContent:(nullable id)content contentMode:(UIViewContentMode)contentMode width:(CGFloat)width ascent:(CGFloat)ascent descent:(CGFloat)descent;
+ (NSMutableAttributedString*)yy_attachmentStringWithContent:(nullableid)contentcontentMode:(UIViewContentMode)contentModeattachmentSize:(CGSize)attachmentSizealignToFont:(UIFont*)fontalignment:(YYTextVerticalAlignment)alignment;
+ (nullable NSMutableAttributedString *)yy_attachmentStringWithEmojiImage:(UIImage *)image fontSize:(CGFloat)fontSize;
5》可变富文本:设置 属性字典
- (void)yy_setAttributes:(nullableNSDictionaryid> *)attributes; //字典
- (void)setYy_attributes:(nullableNSDictionaryid> *)attributes; //字典
- (void)yy_setAttribute:(NSString*)namevalue:(nullableid)value; //单个富文本属性
- (void)yy_setAttribute:(NSString*)namevalue:(nullableid)valuerange:(NSRange)range; //单个富文本属性
- (void)yy_removeAttributesInRange:(NSRange)range; //移除富文本属性
6》可变富文本设置单个富文本—>属性 快速API 、可变富文本设置单个富文本—>段落属性 快速API
- (void)yy_setFont:(nullable UIFont *)font range:(NSRange)range;
- (void)yy_setColor:(nullable UIColor *)color range:(NSRange)range; 。。。。等等
- (void)yy_setAlignment:(NSTextAlignment)alignment range:(NSRange)range;。。。等等
7》可变富文本设置单个富文本—>YYText属性 快速API
- (void)yy_setTextShadow:(nullable YYTextShadow *)textShadow range:(NSRange)range; //阴影
- (void)yy_setTextInnerShadow:(nullable YYTextShadow *)textInnerShadow range:(NSRange)range; //文本内阴影
- (void)yy_setTextUnderline:(nullable YYTextDecoration *)textUnderline range:(NSRange)range; //下划线
- (void)yy_setTextStrikethrough:(nullable YYTextDecoration *)textStrikethrough range:(NSRange)range; //删除线
- (void)yy_setTextBorder:(nullable YYTextBorder *)textBorder range:(NSRange)range; //边框
- (void)yy_setTextBackgroundBorder:(nullable YYTextBorder *)textBackgroundBorder range:(NSRange)range; //背景边框
- (void)yy_setTextGlyphTransform:(CGAffineTransform)textGlyphTransform range:(NSRange)range; //文字变形
- (void)yy_setTextAttachment:(nullable YYTextAttachment *)textAttachment range:(NSRange)range; //附件
- (void)yy_setTextHighlight:(nullable YYTextHighlight *)textHighlight range:(NSRange)range; //高亮
。。。。。等等 系统属性 && YYText属性 快速设置
8》文本高亮显示的简便方法
![](https://img.haomeiwen.com/i2872842/7b52d7fccc062711.png)
9》一些扩展方法
- (void)yy_insertString:(NSString *)string atIndex:(NSUInteger)location;
- (void)yy_appendString:(NSString *)string;
- (void)yy_setClearColorToJoinedEmoji;
+ (NSArray<NSString *> *)yy_allDiscontinuousAttributeKeys;
10、文本输入框上方的系统菜单UIMenuController的使用注意
1、显示菜单 子view的 父类View :必须实现以下三个方法:
1》- (BOOL)canBecomeFirstResponder; // 必须能成为第一响应者
2》- (BOOL)canPerformAction:(SEL)action withSender:(id)sender; // 控制菜单按钮的显示
3》- (void)cut:(UIMenuController *)menu; // 系统方法 或者- (void)hahaha:(UIMenuController *)menu; // 自定义按钮方法
![](https://img.haomeiwen.com/i2872842/46c3251b034d92a5.png)
![](https://img.haomeiwen.com/i2872842/2aff0c2888f47b8f.png)
网友评论