版本記錄
版本號(hào) | 時(shí)間 |
---|---|
V1.0 | 2017.06.05 |
前言
YYText是一個(gè)專門處理文字的框架,有了它處理文字變得非常方便案铺,這一篇我繼續(xù)介紹YYText的使用方法闸氮,希望對(duì)大家能有所幫助。大家還可以參考:
1.YYText使用篇(一)
一钥星、YYText基本屬性
這里先介紹YYText的基本屬性用法,直接看代碼满着。
#import "JJTextVC.h"
#import "YYText.h"
@interface JJTextVC ()
@end
@implementation JJTextVC
#pragma mark - Override Base Function
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"YYText";
//屬性文本
[self attributeStr];
}
#pragma mark - Object Private Function
//屬性文本
- (void)attributeStr
{
//1.創(chuàng)建一個(gè)屬性文本
NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:@"舊時(shí)月色,算幾番照我,梅邊吹笛?喚起玉人,不管清寒與攀摘.何遜而今漸老,都忘卻,春風(fēng)詞筆.但怪得,竹外疏花,春冷入瑤席.江國,正寂寂.嘆寄與路遙,夜雪初積.翠尊易泣,紅萼無言耿相憶.常記曾攜手處,千樹壓.梅湖寒碧,又片片吹盡也,何時(shí)得見? "];
//2.為文本設(shè)置屬性
attributeStr.yy_font = [UIFont boldSystemFontOfSize:18.0];
[attributeStr yy_setColor:[UIColor blueColor] range:NSMakeRange(0, 50)];
attributeStr.yy_lineSpacing = 20.0;
//3.賦值給YYLabel
YYLabel *yyLabel = [[YYLabel alloc] init];
yyLabel.attributedText = attributeStr;
yyLabel.numberOfLines = 0;
yyLabel.frame = CGRectMake(0.0, 64.0, self.view.bounds.size.width, 500.0);
[self.view addSubview:yyLabel];
}
@end
看結(jié)果圖
屬性文本
二谦炒、YYText高亮狀態(tài)簡(jiǎn)單應(yīng)用
YYText的高亮狀態(tài)采用下面的方法:
/**
Convenience method to set text highlight
@param range text range
@param color text color (pass nil to ignore)
@param backgroundColor text background color when highlight
@param userInfo user information dictionary (pass nil to ignore)
@param tapAction tap action when user tap the highlight (pass nil to ignore)
@param longPressAction long press action when user long press the highlight (pass nil to ignore)
*/
- (void)yy_setTextHighlightRange:(NSRange)range
color:(nullable UIColor *)color
backgroundColor:(nullable UIColor *)backgroundColor
userInfo:(nullable NSDictionary *)userInfo
tapAction:(nullable YYTextAction)tapAction
longPressAction:(nullable YYTextAction)longPressAction;
下面我們看代碼
#import "JJTextVC.h"
#import "YYText.h"
@interface JJTextVC ()
@end
@implementation JJTextVC
#pragma mark - Override Base Function
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor lightTextColor];
self.title = @"YYText";
//高亮狀態(tài)
[self highlightState];
}
#pragma mark - Object Private Function
//高亮狀態(tài)
- (void)highlightState
{
//1.創(chuàng)建一個(gè)屬性文本
NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:@"舊時(shí)月色,算幾番照我,梅邊吹笛?喚起玉人,不管清寒與攀摘.何遜而今漸老,都忘卻,春風(fēng)詞筆.但怪得,竹外疏花,春冷入瑤席.江國,正寂寂.嘆寄與路遙,夜雪初積.翠尊易泣,紅萼無言耿相憶.常記曾攜手處,千樹壓.梅湖寒碧,又片片吹盡也,何時(shí)得見? "];
//2.為文本設(shè)置屬性
attributeStr.yy_font = [UIFont boldSystemFontOfSize:18.0];
attributeStr.yy_lineSpacing = 20.0;
//3.設(shè)置文本的高亮屬性
[attributeStr yy_setTextHighlightRange:NSMakeRange(0, 50)
color:[UIColor redColor]
backgroundColor:[UIColor yellowColor]
userInfo:nil
tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
NSLog(@"我被點(diǎn)擊了");
}
longPressAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
}];
//4.賦值給YYLabel
YYLabel *yyLabel = [[YYLabel alloc] init];
yyLabel.attributedText = attributeStr;
yyLabel.numberOfLines = 0;
yyLabel.frame = CGRectMake(0.0, 64.0, self.view.bounds.size.width, 500.0);
[self.view addSubview:yyLabel];
}
@end
下面我們看gif圖
高亮狀態(tài)
下面看控制臺(tái)輸出
2017-06-05 23:38:29.387 YYTextDemo[2964:76722] 我被點(diǎn)擊了
2017-06-05 23:38:30.718 YYTextDemo[2964:76722] 我被點(diǎn)擊了
2017-06-05 23:38:31.221 YYTextDemo[2964:76722] 我被點(diǎn)擊了
2017-06-05 23:38:31.686 YYTextDemo[2964:76722] 我被點(diǎn)擊了
三、YYText高亮狀態(tài)詳細(xì)應(yīng)用
下面詳細(xì)設(shè)置YYText的高亮狀態(tài)风喇,可以看代碼宁改。
#import "JJTextVC.h"
#import "YYText.h"
@interface JJTextVC ()
@end
@implementation JJTextVC
#pragma mark - Override Base Function
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor lightTextColor];
self.title = @"YYText";
//高亮狀態(tài)的詳細(xì)設(shè)置
[self highlightStateDetailSetting];
}
#pragma mark - Object Private Function
//高亮狀態(tài)詳細(xì)設(shè)置
- (void)highlightStateDetailSetting
{
//1.創(chuàng)建一個(gè)屬性文本
NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:@"舊時(shí)月色,算幾番照我,梅邊吹笛?喚起玉人,不管清寒與攀摘.何遜而今漸老,都忘卻,春風(fēng)詞筆.但怪得,竹外疏花,春冷入瑤席.江國,正寂寂.嘆寄與路遙,夜雪初積.翠尊易泣,紅萼無言耿相憶.常記曾攜手處,千樹壓.梅湖寒碧,又片片吹盡也,何時(shí)得見? "];
//2.為文本設(shè)置屬性
attributeStr.yy_font = [UIFont boldSystemFontOfSize:18.0];
attributeStr.yy_lineSpacing = 20.0;
//3.創(chuàng)建高亮屬性
YYTextBorder *border = [YYTextBorder borderWithFillColor:[UIColor magentaColor] cornerRadius:4];
YYTextHighlight *highlight = [[YYTextHighlight alloc] init];
[highlight setColor:[UIColor blueColor]];
[highlight setBackgroundBorder:border];
highlight.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
NSLog(@"tap text range:...");
// 你也可以把事件回調(diào)放到 YYLabel 和 YYTextView 來處理。
};
//4.設(shè)置文本的高亮屬性
[attributeStr yy_setTextHighlight:highlight range:NSMakeRange(0, 50)];
//5.賦值給YYLabel
YYLabel *yyLabel = [[YYLabel alloc] init];
yyLabel.attributedText = attributeStr;
yyLabel.numberOfLines = 0;
yyLabel.frame = CGRectMake(0.0, 64.0, self.view.bounds.size.width, 500.0);
[self.view addSubview:yyLabel];
}
@end
下面看gif結(jié)果
高亮狀態(tài)設(shè)置
看輸出
2017-06-05 23:58:04.457 YYTextDemo[3258:91471] tap text range:...
2017-06-05 23:58:14.652 YYTextDemo[3258:91471] tap text range:...
2017-06-05 23:59:48.807 YYTextDemo[3258:91471] tap text range:...
2017-06-06 00:00:35.790 YYTextDemo[3258:91471] tap text range:...
2017-06-06 00:00:51.180 YYTextDemo[3258:91471] tap text range:...
后記
這里主要講述了三種YYText的使用方法魂莫,包括基本方法還有就是高亮狀態(tài)透且,未完,待續(xù)~~~~
大慶紅旗泡