yytext table html,快速掌握YYText

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

安装

1 CocoasPod 安装

pod 'YYText'

UIKit

CoreFoundation

CoreText

QuartzCore

Accelerate

MobileCoreServices

内容

fc64c7549c95

9.png

打开YYText.h

#import

#import

#import

#import

#import

#import

#import

#import

#import

#import

#import

#import

#import

#import

#import

#import

我们主要用到的都在YYLabel 和YYTextView

二者用法类似 我们主要研究YYLabel

@interface YYLabel : UIView

@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 textParser;

@property (nullable, nonatomic, strong) YYTextLayout *textLayout;

@property (nullable, nonatomic, copy) UIBezierPath *textContainerPath;

@property (nullable, nonatomic, copy) NSArray *exclusionPaths;

@property (nonatomic) UIEdgeInsets textContainerInset;

@property (nullable, nonatomic, copy) id 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

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;

}

效果

fc64c7549c95

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;

}

效果

fc64c7549c95

57E94F5E-8FE4-4F57-B1E9-1942AB8745BC.png

fc64c7549c95

E8B95751-DAF1-4BBA-9BDC-FFD842B39009.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值