YYText简单使用(附效果图)

239 阅读3分钟

YYText官方文档 (官方文档写的也很清楚,容易上手)

YYKit更多好用的组件

- (void)test1
{

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

 
    YYLabel* label=[[YYLabel alloc]initWithFrame:CGRectMake(30, 50, SCREEN_WIDTH-60, SCREEN_HEIGHT-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:5] 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];

    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;
}

image.png

实现指定文字高亮

- (void)test2 {

    

    CGFloat WIDTH = [UIScreen mainScreen].bounds.size.width;

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

    // 1. 创建一个"高亮"属性,当用户点击了高亮区域的文本时,"高亮"属性会替换掉原本的属性

    YYTextBorder *border = [YYTextBorder borderWithFillColor:[UIColor yellowColor] cornerRadius:3];

    

    YYTextHighlight *highlight = [YYTextHighlight highlightWithBackgroundColor:[UIColor redColor]];

    [highlight setColor:[UIColor greenColor]];

    [highlight setBackgroundBorder:border];

  


    // 1. 创建一个属性文本

    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:str];

    text.yy_font = [UIFont boldSystemFontOfSize:14];

    text.yy_color = [UIColor blueColor];

    text.yy_lineSpacing = 10;

    NSString *highlightStr = @"我";

    NSArray *array = [self rangeOfSubString:highlightStr inString:str];

    for (NSInteger i = 0; i < array.count; i++) {

        NSValue *value = array[i];

        // 2. 把"高亮"属性设置到某个文本范围

        [text yy_setTextHighlight:highlight range:value.rangeValue];

        [text yy_setColor:[UIColor redColor] range:value.rangeValue];

    }

    

    NSString *highlightTwoStr = @"一";

    NSArray *array2 = [self rangeOfSubString:highlightTwoStr inString:str];

    for (NSInteger i = 0; i < array2.count; i++) {

        NSValue *value = array2[i];

        // 2. 把"高亮"属性设置到某个文本范围

        [text yy_setTextHighlight:highlight range:value.rangeValue];

        [text yy_setColor:[UIColor orangeColor] range:value.rangeValue];

    }

    

    // 3. 赋值到 YYLabel 或 YYTextView

    YYLabel *label = [[YYLabel alloc] initWithFrame:CGRectMake(30, 50, SCREEN_WIDTH-60, SCREEN_HEIGHT-100)];

    label.numberOfLines = 0;

    label.preferredMaxLayoutWidth = WIDTH-30;

    label.attributedText = text;

    [self.view addSubview:label];
    
label.highlightTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {

        NSLog(@"textTapAction===>%lu  location==>%d",(unsigned long)range.location,range.length);

        NSString *subStr = [str substringWithRange:range];

        NSLog(@"获取高亮点击的文字--->%@",subStr);

    };

}

  


//获取一个字符在字符串中出现的所有位置 返回一个被NSValue包装的NSRange数组

- (NSArray *)rangeOfSubString:(NSString *)subStr inString:(NSString *)string {

    if (subStr == nil && [subStr isEqualToString:@""]) {

        return nil;

    }

    NSMutableArray *rangeArray = [NSMutableArray array];

    NSString *string1 = [string stringByAppendingString:subStr];

    NSString *temp;

    for (int i = 0; i < string.length; i ++) {

        temp = [string1 substringWithRange:NSMakeRange(i, subStr.length)];

        if ([temp isEqualToString:subStr]) {

            NSRange range = {i,subStr.length};

            [rangeArray addObject:[NSValue valueWithRange:range]];

        }

    }

    return rangeArray;

}

image.png