快速掌握YYText

在开发中我们经常会需要用到富文本,如果每次使用都去创建设置的话比较繁琐,这个时候一个好的框架能帮我们省去不少时间.YYText 就是一个使用比较成熟的框架,现在我们就开始学习YYText.

安装

UIKit
CoreFoundation
CoreText
QuartzCore
Accelerate
MobileCoreServices

内容

9.png

打开YYText.h

#import <YYText/YYLabel.h>
#import <YYText/YYTextView.h>
#import <YYText/YYTextAttribute.h>
#import <YYText/YYTextArchiver.h>
#import <YYText/YYTextParser.h>
#import <YYText/YYTextRunDelegate.h>
#import <YYText/YYTextRubyAnnotation.h>
#import <YYText/YYTextLayout.h>
#import <YYText/YYTextLine.h>
#import <YYText/YYTextInput.h>
#import <YYText/YYTextDebugOption.h>
#import <YYText/YYTextKeyboardManager.h>
#import <YYText/YYTextUtilities.h>
#import <YYText/NSAttributedString+YYText.h>
#import <YYText/NSParagraphStyle+YYText.h>
#import <YYText/UIPasteboard+YYText.h>

我们主要用到的都在YYLabel 和YYTextView
二者用法类似 我们主要研究YYLabel

@interface YYLabel : UIView <NSCoding>
@property (nullable, nonatomic, copy) IBInspectable NSString *text;
@property (null_resettable, nonatomic, strong) IBInspectable UIColor *textColor;
@property (nullable, nonatomic, strong) IBInspectable NSString *fontName_;
@property (nonatomic) IBInspectable CGFloat fontSize_;
@property (nonatomic) IBInspectable BOOL fontIsBold_;
@property (nonatomic) IBInspectable NSUInteger numberOfLines;
@property (nonatomic) IBInspectable NSInteger lineBreakMode;
@property (nonatomic) IBInspectable CGFloat preferredMaxLayoutWidth;
@property (nonatomic, getter=isVerticalForm) IBInspectable BOOL verticalForm;
@property (nonatomic) IBInspectable NSInteger textAlignment;
@property (nonatomic) IBInspectable NSInteger textVerticalAlignment;
@property (nullable, nonatomic, strong) IBInspectable UIColor *shadowColor;
@property (nonatomic) IBInspectable CGPoint shadowOffset;
@property (nonatomic) IBInspectable CGFloat shadowBlurRadius;
@property (nullable, nonatomic, copy) IBInspectable NSAttributedString *attributedText;
@property (nonatomic) IBInspectable CGFloat insetTop_;
@property (nonatomic) IBInspectable CGFloat insetBottom_;
@property (nonatomic) IBInspectable CGFloat insetLeft_;
@property (nonatomic) IBInspectable CGFloat insetRight_;
@property (nonatomic) IBInspectable BOOL debugEnabled_;

@property (null_resettable, nonatomic, strong) UIFont *font;
@property (nullable, nonatomic, copy) NSAttributedString *truncationToken;
@property (nullable, nonatomic, strong) id<YYTextParser> textParser;
@property (nullable, nonatomic, strong) YYTextLayout *textLayout;
@property (nullable, nonatomic, copy) UIBezierPath *textContainerPath;
@property (nullable, nonatomic, copy) NSArray<UIBezierPath*> *exclusionPaths;
@property (nonatomic) UIEdgeInsets textContainerInset;
@property (nullable, nonatomic, copy) id<YYTextLinePositionModifier> linePositionModifier;
@property (nonnull, nonatomic, copy) YYTextDebugOption *debugOption;
@property (nullable, nonatomic, copy) YYTextAction textTapAction;
@property (nullable, nonatomic, copy) YYTextAction textLongPressAction;
@property (nullable, nonatomic, copy) YYTextAction highlightTapAction;
@property (nullable, nonatomic, copy) YYTextAction highlightLongPressAction;
@property (nonatomic) BOOL displaysAsynchronously;
@property (nonatomic) BOOL clearContentsBeforeAsynchronouslyDisplay;
@property (nonatomic) BOOL fadeOnAsynchronouslyDisplay;
@property (nonatomic) BOOL fadeOnHighlight;
@property (nonatomic) BOOL ignoreCommonProperties;
@end

丛类里面我们可以看到声明了各种属性,通过设置不同的属性达到要求,这个具体问题的时候我们讲解.

使用和测试

  • 1 基本用法
YYLabel *label = [YYLabel new];
label.frame = ...
label.font = ...
label.textColor = ...
label.textAlignment = ...
label.lineBreakMode = ...
label.numberOfLines = ...
label.text = ...
  • 2 属性文本
    先上代码
#import <YYText/YYText.h>

NSString* YuJian=  @"听见 冬天的离开  我在某年某月醒过来  我想我等我期待 未来却不能理智安排 -- 阴天 傍晚车窗外 未来有一个人在等待 向左向右向前看  爱要拐几个弯才来 我遇见谁会有怎样的对白  我等的人他在多远的未来  我听见风来自地铁和人海 我排著队拿著爱的号码牌 我往前飞飞过一片时间海 我们也常在爱情里受伤害 我遇见谁会有怎样的对白  我等的人他在多远的未来  我听见风来自地铁和人海 我排著队拿著爱的号码牌 我往前飞飞过一片时间海 我们也常在爱情里受伤害 我看著路梦的入口有点窄 我遇见你是最美丽的意外 @终有一天我的谜底会揭开";
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

#define kScreenWidth [UIScreen mainScreen].bounds.size.width

文字属性

/**
 文字属性
 */
-(void)test1
{
    
    YYLabel* label=[[YYLabel alloc]initWithFrame:CGRectMake(30, 50, kScreenWidth-60, kScreenHeight-100)];
    [self.view addSubview:label];
    label.numberOfLines=0;
    
    NSMutableAttributedString* atext=[[NSMutableAttributedString alloc]initWithString:YuJian];
    //设置字体大小
    [atext yy_setFont:[UIFont systemFontOfSize:20] range:atext.yy_rangeOfAll];
    
    //局部不同颜色
    NSRange range0=[[atext string]rangeOfString:@"冬天的离开"];
    [atext yy_setColor:[UIColor blueColor] range:range0];
    //设置行间距
    atext.yy_lineSpacing=10;
    
    //设置下划线
    NSRange range1=[[atext string]rangeOfString:@"我等的人" ];
    YYTextDecoration* deco=[YYTextDecoration decorationWithStyle:(YYTextLineStyleSingle) width:[NSNumber numberWithInt:1] color:[UIColor redColor]];
    [atext yy_setTextUnderline:deco range:range1];
    
    //阴影
    
    NSRange range2=[[atext string]rangeOfString:@"傍晚车窗外" options:(nil)];
    NSShadow*  shadow=[[NSShadow alloc]init];
    [shadow setShadowColor:[UIColor redColor]];
    [shadow setShadowBlurRadius:1];
    [shadow setShadowOffset:CGSizeMake(2, 2)];
    [atext yy_setShadow:shadow range:range2];
    //文本内阴影
    NSRange range3=[[atext string]rangeOfString:@"我在某年某月醒过来"];
    YYTextShadow* dow=[YYTextShadow new];
    dow.color=[UIColor yellowColor];
    dow.offset=CGSizeMake(0, 2);
    dow.radius=1;
               
    [atext yy_setTextShadow:dow range:range3];
    
    label.attributedText=atext;
    
    
    
    
}

效果

5D58A48D-E994-42C6-9B32-922F8249DF8E.png

高亮和点击事件

//高亮文本和点击事件
-(void)test2
{
    YYLabel* label=[[YYLabel alloc]initWithFrame:CGRectMake(30, 50, kScreenWidth-60, kScreenHeight-100)];
    [self.view addSubview:label];
    label.numberOfLines=0;
    
    NSMutableAttributedString* atext=[[NSMutableAttributedString alloc]initWithString:YuJian];
    //设置字体大小
    [atext yy_setFont:[UIFont systemFontOfSize:20] range:atext.yy_rangeOfAll];
    NSRange range4=[[atext string] rangeOfString:@"向左向右向前看"];
    [atext yy_setTextHighlightRange:range4 color:[UIColor redColor] backgroundColor:[UIColor grayColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        NSString* str=text.string;
       
        NSLog(@"你点击了 %@ ---range %ld", [str substringWithRange:range] ,range.length);
        
    }];
    
    
  
    
    
    label.attributedText=atext;
    
}

效果

57E94F5E-8FE4-4F57-B1E9-1942AB8745BC.png
E8B95751-DAF1-4BBA-9BDC-FFD842B39009.png

简单的用法就是这些了,YYText很强大,还有图文混排等高大上的功能,等楼主抽空再研究讲解

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容