iOS 富文本简单使用

UITextView *upText = [[UITextView alloc] init];
        upText.font = [UIFont systemFontOfSize:14.f];
        upText.backgroundColor = [UIColor clearColor];
        upText.textColor = [UIColor whiteColor];
        upText.textAlignment = NSTextAlignmentLeft;
        upText.text = LocalString(@"1.Turn on your Robot\n2.Unlock the keyboard;\n3.Press the button for 5seconds,till the LED light flashing(fast)\n4.Press the button");
        [_oneModelImage addSubview:upText];
        [upText mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(yAutoFit(200), yAutoFit(100)));
            make.centerX.equalTo(self.oneModelImage.mas_centerX);
            make.top.mas_equalTo(upImage.mas_bottom).offset(yAutoFit(5));
        }];
        
        _oneModelImage.layer.borderWidth = 1.0;
        _oneModelImage.layer.borderColor = [UIColor whiteColor].CGColor;
        _oneModelImage.layer.cornerRadius = 10.f/HScale;
        
        //2.初始化富文本对象
         NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:upText.text];
        //2.1修改富文本中的不同文字的样式
        [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, attributedString.length)];//字体颜色
        [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, attributedString.length)];//字体大小
        //3.初始化NSTextAttachment对象
        NSTextAttachment *attchmentOne = [[NSTextAttachment alloc]init];
        attchmentOne.bounds = CGRectMake(0, 0,yAutoFit(20), yAutoFit(10));//设置frame
        attchmentOne.image = [UIImage imageNamed:@"img_model1_help_up_text"];//设置图片

        //4.创建带有图片的富文本
        NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:(NSTextAttachment *)(attchmentOne)];
        [attributedString insertAttributedString:string atIndex:63];//插入到第几个下标
        //[attributedString appendAttributedString:string];   //添加到尾部

        //5.用label的attributedText属性来使用富文本
        upText.attributedText = attributedString;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值