iOS富文本用法之UILabel怎么加载图片和超链接

7 篇文章 0 订阅

经常会看到一些场景是一行文字里面夹杂着图片文字超链接,那在的iOS版里面显示文字的空间的的UILabel怎么达到此要求呢?下面标记一下用原生方法和很经典的三方库YYKit下的yytext中中两种方法的对比。

/*!
 @brief 原生富文本
 */
-(void)primitAttributeStr {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, self.view.bounds.size.width, 100)];
    NSMutableAttributedString *attaStr = [[NSMutableAttributedString alloc] initWithString:@"富文本"];
    //下滑线
    NSMutableAttributedString *underlineStr = [[NSMutableAttributedString alloc] initWithString:@"下滑线"];
    [underlineStr addAttributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),
                                  NSUnderlineColorAttributeName: [UIColor redColor]
                                  } range:NSMakeRange(0, 3)];
    [attaStr appendAttributedString:underlineStr];
    //删除线
    NSMutableAttributedString *throughlineStr = [[NSMutableAttributedString alloc] initWithString:@"删除线"];
    [throughlineStr addAttributes:@{NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle),
                                    NSStrikethroughColorAttributeName: [UIColor orangeColor]
                                    } range:NSMakeRange(0, 3)];
    [attaStr appendAttributedString:throughlineStr];
    //超链接
    NSString *urlStr = @"http://www.baidu.com";
    NSMutableAttributedString *linkStr = [[NSMutableAttributedString alloc] initWithString:urlStr];
    [linkStr addAttributes:@{NSLinkAttributeName: [NSURL URLWithString:urlStr]} range:NSMakeRange(0, urlStr.length)];
    [attaStr appendAttributedString:linkStr];
    //图片
    NSTextAttachment *imgAttach =  [[NSTextAttachment alloc] init];
    imgAttach.image = [UIImage imageNamed:@"dribbble64_imageio"];
    imgAttach.bounds = CGRectMake(0, 0, 30, 30);
    NSAttributedString *attachStr = [NSAttributedString attributedStringWithAttachment:imgAttach];
    [attaStr appendAttributedString:attachStr];
    
    label.attributedText = attaStr;
    label.numberOfLines = 0;
    [self.view addSubview:label];
}
/*!
 @brief YYText富文本
 */
-(void)yy_attributeStr {
    YYLabel *label = [[YYLabel alloc] initWithFrame:CGRectMake(0, 200, self.view.bounds.size.width, 100)];
    NSMutableAttributedString *attriStr = [[NSMutableAttributedString alloc] initWithString:@"YY富文本"];
    attriStr.yy_font = [UIFont systemFontOfSize:16];
    
    NSMutableAttributedString *underLine = [[NSMutableAttributedString alloc] initWithString:@"下滑线"];
    underLine.yy_font = [UIFont systemFontOfSize:17];
    underLine.yy_color = [UIColor redColor];
    underLine.yy_underlineStyle = NSUnderlineStyleSingle;
    underLine.yy_underlineColor = [UIColor redColor];
    [attriStr appendAttributedString:underLine];
    
    NSMutableAttributedString *deleteLine = [[NSMutableAttributedString alloc] initWithString:@"删除线"];
    deleteLine.yy_color = [UIColor blueColor];
    deleteLine.yy_font = [UIFont systemFontOfSize:18];
    deleteLine.yy_textStrikethrough = [YYTextDecoration decorationWithStyle:YYTextLineStyleSingle width:@1 color:[UIColor magentaColor]];
    [attriStr appendAttributedString:deleteLine];
    
    NSMutableAttributedString *link = [[NSMutableAttributedString alloc] initWithString:@"超链接"];
    link.yy_font = [UIFont systemFontOfSize:19];
    link.yy_underlineStyle = NSUnderlineStyleSingle;
    link.yy_underlineColor = [UIColor purpleColor];
    [link yy_setTextHighlightRange:link.yy_rangeOfAll color:[UIColor orangeColor] backgroundColor:[UIColor lightGrayColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        NSLog(@"tap:%@", [text.string substringWithRange:range]);
    }];
    [attriStr appendAttributedString:link];
    
    UIImage *img = [UIImage imageNamed:@"dribbble64_imageio"];
    img = [UIImage imageWithCGImage:img.CGImage scale:2 orientation:UIImageOrientationUp];
    NSMutableAttributedString *imgAttach = [NSMutableAttributedString yy_attachmentStringWithContent:img contentMode:UIViewContentModeCenter attachmentSize:img.size alignToFont:[UIFont systemFontOfSize:16] alignment:YYTextVerticalAlignmentCenter];
    [attriStr appendAttributedString:imgAttach];
    
    label.attributedText = attriStr;
    [self.view addSubview:label];
}

对比:

如图1所示,代码量:原生和yytext中中其实代码量差不多,不过,功能上肯定yytext中中肯定更胜一筹,比如yytext中中有很多类别,方便使用,再比如下面的第2点;

2,超链接:原生富文本下的超链接没有点击效果,不能跳转,只是会有超链接的样式(下滑线和变色),其实没卵用,如果要达到点击超链接可跳转,请查看另一篇文章:iOS 点击文字识别超链接并实现跳转,且此方法必须文本里面包含网址;

而yytext中中设置的超链接是有点击回调的,可设置跳转和点击效果,且网址可不用显示出来,在回调里处理即可,相对原生的跳转方法简单多了;

3,图片:yytext中中对图片可设置旋转,以及垂直方向上的居中还是上下对齐,比原生更灵活。

所以,如果项目里面对富文本效果要求不高,可直接用原生的,代码量和设置方法都比较简单;反之用yytext中中更灵活。

 

相关:iOS UIlabel怎么加载html字符串 富文本的用法

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值