iOS开发攻城狮的集散地iOS 应用层

iOS 给部分文字加下划线四种方法

2018-07-27  本文已影响79人  笑破天

本人了解到四种方法:label加载AttStr、label加载HTML、label加lineView、YYLabel加下划线。结论是最好的开胃菜,先说结论。

结论:加载AttStr时下划线距离文字实体太近,效果不太理想;加载HTML的效果会好点,但据说会闪屏;加lineView是万能的,可以自己调节下划线的距离;YYLabel加下划线,方便快捷,性能还不错。

一、label加载AttStr
    NSString * sum_str = @"label加载attstr:我的测试";
    NSString * red_str = @"我的测试";
    NSMutableAttributedString * att_str2 = [[NSMutableAttributedString alloc] initWithString:sum_str];
    [att_str2 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[sum_str rangeOfString:red_str]];
    [att_str2 addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:[sum_str rangeOfString:red_str]];
    UILabel * myLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 200, 40)];
    myLabel2.backgroundColor = [UIColor orangeColor];
    myLabel2.font = [UIFont systemFontOfSize:14];
    myLabel2.attributedText = att_str2;
    [self.view addSubview:myLabel2];
二、label加载HTML
    NSString * htmlString = @"<p style='font-size:14px'>label加载HTML:\n\n<span style='color:#ff0000;font-size:14px'><u>我的测试</u></span></p>";
    NSAttributedString * att_str1 = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    UILabel * myLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
    myLabel.backgroundColor = [UIColor orangeColor];
    //myLabel.font = [UIFont systemFontOfSize:14];
    myLabel.attributedText = att_str1;
    [self.view addSubview:myLabel];
三、label加lineView
    NSString * sum_str3 = @"label加lineView:我的测试";
    NSString * red_str3 = @"我的测试";
    UILabel * myLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(100, 300, 200, 40)];
    myLabel3.backgroundColor = [UIColor orangeColor];
    myLabel3.font = [UIFont systemFontOfSize:14];
    myLabel3.text = sum_str3;
    NSMutableAttributedString * att_str3 = [[NSMutableAttributedString alloc] initWithString:sum_str3];
    [att_str3 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[sum_str3 rangeOfString:red_str3]];
    myLabel3.attributedText = att_str3;
    [self.view addSubview:myLabel3];
    UIView *line_view = [[UIView alloc]initWithFrame:CGRectMake(100+112, 300+40-12, 56, 1)];
    line_view.backgroundColor = [UIColor redColor];
    [self.view addSubview:line_view];
四、YYLabel加下划线
    NSMutableAttributedString *text = [[NSMutableAttributedString 
    alloc]initWithString:@"YYLabel加下划线:"];
    NSMutableAttributedString *one = [[NSMutableAttributedString alloc]     
    initWithString:@"我的测试"];
    one.font = [UIFont systemFontOfSize:14];
    one.underlineStyle = NSUnderlineStyleSingle;
    [one setTextHighlightRange:one.rangeOfAll
                         color:[UIColor redColor]
               backgroundColor:[UIColor colorWithWhite:0.000 alpha:0.220]
                     tapAction:^(UIView *containerView, NSAttributedString *text,         
    NSRange range, CGRect rect) {
                         NSLog(@"Tap: %@", [text.string substringWithRange:range]);
                     }];
    [text appendAttributedString:one];
    YYLabel *label = [YYLabel new];
    label.backgroundColor = [UIColor orangeColor];
    label.frame = CGRectMake(100, 400, 200, 40);
    label.attributedText = text;
    [self.view addSubview:label];

效果图如下:


效果图
上一篇 下一篇

猜你喜欢

热点阅读