美文网首页
NSAttributedString的妙用

NSAttributedString的妙用

作者: SmoothV | 来源:发表于2018-10-19 09:39 被阅读11次

    一.给文字上面加删除线

    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(20, 50, 200, 50)]; 
    [self.view addSubview:label]; label.text = @"10.00"; 
    label.textAlignment = NSTextAlignmentCenter; 
    label.textColor = [UIColor redColor]; 
    // 横线的颜色跟随label字体颜色改变 
    NSMutableAttributedString *newPrice = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"¥%@",label.text]]; 
    [newPrice addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, newPrice.length)]; 
    label.attributedText = newPrice;
    

    效果:


    给文字上面加删除线

    二.UILabel实现图文混排

        NSMutableAttributedString *attri =  [[NSMutableAttributedString alloc] initWithString:@"连接前请先按Fn+F4切换到蓝牙连接手机/平板"];
        NSTextAttachment *attch = [[NSTextAttachment alloc] init];
        // 表情图片
        attch.image = [UIImage imageNamed:@"pop_pic_tip.png"];
         //设置图片大小
        attch.bounds = CGRectMake(0, 0, 17*(64/21.0),17);
        // 创建带有图片的富文本
        NSAttributedString *imgString = [NSAttributedString attributedStringWithAttachment:attch];
        [attri appendAttributedString:imgString];
        
        NSAttributedString *string = [[NSAttributedString alloc]initWithString:@"闪烁进入待连接状态" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]}];
        [attri appendAttributedString:string];
        
        self.label01 = [[UILabel alloc]init];
        [self.contentView addSubview:self.label01];
        self.label01.numberOfLines = 0;
        [self.label01 setTextColor:[UIColor colorWithRGB:0x2f3641]];
        self.label01.attributedText = attri;
        [self.label01 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView.mas_left).mas_offset(16);
            make.right.mas_equalTo(self.contentView.mas_right).mas_offset(-16);
            make.top.mas_equalTo(self.imgView.mas_bottom).mas_offset(15);
        }];
    

    效果:


    UILabel的图文混排

    相关文章

      网友评论

          本文标题:NSAttributedString的妙用

          本文链接:https://www.haomeiwen.com/subject/vrzezftx.html