iOS_富文本的图文混排

之前写过,改变字体颜色字号等功能,这次咱们来来看看,NSMutableAttributedString 的延伸使用,富文本编辑,效果与图文混排类似。

直接上代码不多说了:

//
//  ViewController.m
//  AAAA
//
//  Created by a111 on 16/7/12.
//  Copyright © 2016年 司小文. All rights reserved.
//

#import "ViewController.h"

@interface ViewController (){
    UILabel *_lab_AAAA;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self makeUI];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)makeUI{
    _lab_AAAA = [[UILabel alloc] init];
    _lab_AAAA.numberOfLines = 0;
    _lab_AAAA.text = @"任天堂开发的AR手游《精灵宝可梦GO》可谓引起了一时轰动,该游戏于上周在美国、澳大利亚和新西兰地区上架,这让其它地区苦等的粉丝非常失望,由于太过火爆,该公司还对IP进行了封锁,中国区目前仍无法玩。但好消息是其它地区的用户不用等太久了,现据《华尔街日报》报道,《精灵宝可梦GO》将于“近几天内”登陆欧洲和亚洲地区,值得一提的是,该消息并非来自任天堂官方,而是了解游戏计划的消息人士。由于游戏大热,也促进了任天堂股价一度飙涨23%。希望任天堂不要辜负玩家们的热情,能为游戏提供更好的支持。";
    [self setupEmojiAndLink];
    [self.view addSubview:_lab_AAAA];
    
    CGRect shopLabRect = [_lab_AAAA.attributedText boundingRectWithSize:CGSizeMake(self.view.frame.size.width-20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
    
    _lab_AAAA.frame = CGRectMake(10, 100,shopLabRect.size.width, 300);
}


/**
 *  向文本中添加表情等
 */
- (void)setupEmojiAndLink
{
    NSMutableAttributedString * mutStr = [_lab_AAAA.attributedText mutableCopy];
    
    //添加表情
    for ( int i = 0; i< mutStr.length/50 ; i++) {
        //创建一个图片
        UIImage * image1 =  [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpeg",arc4random()%3+1]];
        NSTextAttachment * attachment1 = [[NSTextAttachment alloc] init];
        attachment1.bounds = CGRectMake(0, -1, 30, 30);
        attachment1.image = image1;
        NSAttributedString * attachStr1 = [NSAttributedString attributedStringWithAttachment:attachment1];
        //最后一个参数为图片在证据话的位置下标。
        [mutStr insertAttributedString:attachStr1 atIndex:50*i];
    }
    
    _lab_AAAA.attributedText = [mutStr copy];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end








效果图如下:



感谢学习,学以致用更感谢~


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值