IOS图文混排

在iOS6之前,苹果系统的图文混排技术使用coreText:如果需要使用可以使用第三方框架(纯C语言)
iOS6使用 NSAttributedString、NSMutableAttributedString
iOS7 使用textKit

方法一:
NSMutableAttributedString *string = [[NSMutableAttributedStringalloc]initWithString:@"yz谁都会发生地方"];
    设置某个距离文字的颜色
    [string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor]range:NSMakeRange(0, 2)];
    [string addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20]range:NSMakeRange(0, 2)];
设置下划线
    [string addAttribute:NSUnderlineStyleAttributeNamevalue:@(NSUnderlineStyleDouble) range:NSMakeRange(1, 3)];
添加图片
    NSTextAttachment *attach = [[NSTextAttachment alloc]init];
    attach.image = [UIImage imageNamed:@"placehold"];
可以根据字体的大小设置图片的大小,width和height为字体的size
    attach.bounds = CGRectMake(0, 0, 20, 20);
将图片转为NSAttributedString
    NSAttributedString *attachString = [NSAttributedStringattributedStringWithAttachment:attach];
将NSAttributedString拼接在string后
[注意:NSMutableAttributedString 只能拼接NSMutableAttributedString、NSAttributedString]
[string appendAttributedString:attachString];
self.textView.attributedText = string;

方法二:
使用webView和html语言以及css样式 进行图文混排  但是系统消耗较大,且webView有内存泄露
//webView图文混排
    NSString *html = @"<font color='red'></font><img src='http://'>";
UIWebView *webView = [[UIWebView alloc]initWithFrame:self.view.bounds];
[webView loadHTMLString:html baseURL:nil];
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值