美文网首页selector
快速掌握YYText

快速掌握YYText

作者: 进阶的蚊子 | 来源:发表于2017-10-30 16:42 被阅读771次

在开发中我们经常会需要用到富文本,如果每次使用都去创建设置的话比较繁琐,这个时候一个好的框架能帮我们省去不少时间.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很强大,还有图文混排等高大上的功能,等楼主抽空再研究讲解

相关文章

  • 快速掌握YYText

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

  • YYText的探索学习

    整理YYText不错文章 慢慢学习 使用篇 快速掌握YYTextYYText 库学习总结 iOS tableVie...

  • YYText源码分析

    YYText 简单介绍 YYText 是YYKit中的一个富文本显示,编辑组件,拥有YYLabel,YYText...

  • YYKit之YYText、YYLabel使用总结(更新)

    1.索引 YYText YYLabel 更新 YYText IQKeyboardManager支持 布局问题 如果...

  • YYText使用

    NSMutableString + YYText实例YYText+add tag制作文字tag 文字附加attac...

  • YYText 源码解析

    YYText [%5Bhttps://github.com/ibireme/YYText%5D(https://g...

  • YYText使用

    YYText官方文档[https://github.com/ibireme/YYText](官方文档写的也很清楚,...

  • YYtext

    http://www.jianshu.com/p/60aee32ade55?nomobile=yes

  • YYText

    1.YYKit之YYText2.CoreText实现图文混排和点击事件3.认识 TextKit4.CoreText...

  • YYText

    1.里面主要两个控件: YYTextView 和 YYLabel 现在主要是YYTextview的简单使用 YYT...

网友评论

    本文标题:快速掌握YYText

    本文链接:https://www.haomeiwen.com/subject/kvkppxtx.html