YYText使用篇(二)

版本記錄

版本號 時間
V1.0 2017.06.05

前言

YYText是一個專門處理文字的框架,有了它處理文字變得非常方便,這一篇我繼續介紹YYText的使用方法,希望對大家能有所幫助。大家還可以參考:
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.創建一個屬性文本
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:@"舊時月色,算幾番照我,梅邊吹笛?喚起玉人,不管清寒與攀摘.何遜而今漸老,都忘卻,春風詞筆.但怪得,竹外疏花,春冷入瑤席.江國,正寂寂.嘆寄與路遙,夜雪初積.翠尊易泣,紅萼無言耿相憶.常記曾攜手處,千樹壓.梅湖寒碧,又片片吹盡也,何時得見? "];
    
    //2.為文本設置屬性
    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

看結果圖

屬性文本

二、YYText高亮狀態簡單應用

YYText的高亮狀態采用下面的方法:

/**
 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";
    
    //高亮狀態
    [self highlightState];
}

#pragma mark - Object Private Function

//高亮狀態
- (void)highlightState
{
    //1.創建一個屬性文本
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:@"舊時月色,算幾番照我,梅邊吹笛?喚起玉人,不管清寒與攀摘.何遜而今漸老,都忘卻,春風詞筆.但怪得,竹外疏花,春冷入瑤席.江國,正寂寂.嘆寄與路遙,夜雪初積.翠尊易泣,紅萼無言耿相憶.常記曾攜手處,千樹壓.梅湖寒碧,又片片吹盡也,何時得見? "];
    
    //2.為文本設置屬性
    attributeStr.yy_font = [UIFont boldSystemFontOfSize:18.0];
    attributeStr.yy_lineSpacing = 20.0;
    
    //3.設置文本的高亮屬性
    [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(@"我被點擊了");
    }
                           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圖

高亮狀態

下面看控制臺輸出

2017-06-05 23:38:29.387 YYTextDemo[2964:76722] 我被點擊了
2017-06-05 23:38:30.718 YYTextDemo[2964:76722] 我被點擊了
2017-06-05 23:38:31.221 YYTextDemo[2964:76722] 我被點擊了
2017-06-05 23:38:31.686 YYTextDemo[2964:76722] 我被點擊了


三、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 lightTextColor];
    
    self.title = @"YYText";

    //高亮狀態的詳細設置
    [self highlightStateDetailSetting];
}

#pragma mark - Object Private Function

//高亮狀態詳細設置
- (void)highlightStateDetailSetting
{
    //1.創建一個屬性文本
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:@"舊時月色,算幾番照我,梅邊吹笛?喚起玉人,不管清寒與攀摘.何遜而今漸老,都忘卻,春風詞筆.但怪得,竹外疏花,春冷入瑤席.江國,正寂寂.嘆寄與路遙,夜雪初積.翠尊易泣,紅萼無言耿相憶.常記曾攜手處,千樹壓.梅湖寒碧,又片片吹盡也,何時得見? "];
    
    //2.為文本設置屬性
    attributeStr.yy_font = [UIFont boldSystemFontOfSize:18.0];
    attributeStr.yy_lineSpacing = 20.0;
    
    //3.創建高亮屬性
    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:...");
        // 你也可以把事件回調放到 YYLabel 和 YYTextView 來處理。
    };
    
    //4.設置文本的高亮屬性
    [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結果

高亮狀態設置

看輸出

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的使用方法,包括基本方法還有就是高亮狀態,未完,待續~~~~

大慶紅旗泡
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容