YYLabel - 应用

1,251 阅读1分钟

富文本高亮

    CGFloat WIDTH = [UIScreen mainScreen].bounds.size.width;
    NSString *str = @"Some Text, blabla.";
    // 1. 创建一个属性文本
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:str];
    NSString *highlightStr = @"blabla";
    NSArray *array = [self rangeOfSubString:highlightStr inString:str];
    // 2. 为文本设置属性
    text.yy_font = [UIFont boldSystemFontOfSize:30];
    text.yy_color = [UIColor blueColor];
    text.yy_lineSpacing = 10;
    for (NSInteger i = 0; i < array.count; i++) {
        NSValue *value = array[i];
//        [text yy_setColor:[UIColor redColor] range:value.rangeValue];
        [text yy_setTextHighlightRange:value.rangeValue
                  color:[UIColor redColor]
        backgroundColor:[UIColor grayColor]
              tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){
                  NSLog(@"tap text range:...");
        }];
    }
    // 3. 赋值到 YYLabel 或 YYTextView
    YYLabel *label = [[YYLabel alloc] init];
    label.numberOfLines = 0;
    label.preferredMaxLayoutWidth = WIDTH-30;
    label.attributedText = text;
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(100);
    }];
//    label.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
//     NSLog(@"点击了");
//     };   
}

//获取一个字符在字符串中出现的所有位置 返回一个被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;
}

html 标签转换 string

- (void)setModel:(XAPublicWelfareModel *)model {

// 调用去除HTML标签的方法,直接赋值。
self.introLabel.text = [self filterHTML:model.content];
}

//去除标签的方法
- (NSString *)filterHTML:(NSString *)html
{
    NSScanner *scanner = [NSScanner scannerWithString:html];
    NSString *text = nil;
    while ([scanner isAtEnd] == NO) {
        [scanner scanUpToString:@"<" intoString:nil];
        [scanner scanUpToString:@">" intoString:&text];
        html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""];
        //去除空格&nbsp;
        html = [html stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@""];
    }
    return html;
}      

YYlabel 高度自适应

//商品介绍
    NSString *introStr = @"";
    self.proIntroductLB.frame = CGRectMake(5, introductH + 10, 80, 12); //【商品介绍】
    if (aproDetailModel.sdescription.length > 0) {
        introStr = aproDetailModel.sdescription;
    }
    CGFloat introHeight = [self getMessageHeight:introStr];
    self.proIntroductLB.frame = CGRectMake(85, introductH+8, ScreenWidth-100, introHeight);

/**
 *  获取lb的高度(默认字体13,行间距8,lb 宽 ScreenWidth-100)
 *  @param mess lb.text
 *  @param lb (YYLabel *)label
 *  @return lb的高度
 */
- (CGFloat)getMessageHeight:(NSString *)mess
{
    NSMutableAttributedString *introText = [[NSMutableAttributedString alloc] initWithString:mess];
    introText.yy_font = FontSet(13);
    introText.yy_lineSpacing = 8;
    lb.attributedText = introText;
    CGSize introSize = CGSizeMake(ScreenWidth-100, CGFLOAT_MAX);
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:introSize text:introText];
    CGFloat introHeight = layout.textBoundingSize.height;
    return introHeight;
}