NSTextAttachment富文本控件实现图文混排

1.制作富文本,第一种的就是将一段文字中不通的字显示不同的颜色,大小等。

iOS富文本NSMutableAttributedString,NSAttributedString的使用 - snowyshell - snowyshell的博客

UILabel *testLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 320, 30)];

   testLabel.textAlignment = NSTextAlignmentCenter;

   NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@"今天天气不错呀"];

   [AttributedStr addAttribute:NSFontAttributeName

                         value:[UIFont systemFontOfSize:16.0]

                         range:NSMakeRange(2, 2)];

   [AttributedStr addAttribute:NSForegroundColorAttributeName

                         value:[UIColor redColor]

                         range:NSMakeRange(2, 2)];

   testLabel.attributedText = AttributedStr;

   [self.view addSubview:testLabel];
2.制作富文本,第二种就是将图片和文字混排

UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 150, 300, 21)];
    lable.textColor = [UIColor redColor];
    [self.view addSubview:lable];
    
    //富文本
    NSString *message = @"我是郝高明,外号小胖,哈哈~";
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:message attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
    
    NSTextAttachment *attachment = [[NSTextAttachment alloc]initWithData:nil ofType:nil];
    UIImage *image = [UIImage imageNamed:@"80.png"];
    attachment.image = image;
    attachment.bounds = CGRectMake(0, 0, 20, 20);
    
    NSAttributedString *text = [NSAttributedString attributedStringWithAttachment:attachment];
    [str insertAttributedString:text atIndex:5];
    
    lable.attributedText = str;

3.制作富文本,你可能会有一些定制的需求,比如图片的高度不想每个都要设置一边,你可以写一个继承,然后将
-(CGRect)attachmentBoundsForTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:(NSUInteger)charIndex
重写。

//
//  myTextAttachment.h
//  11
//
//  Created by 郝高明 on 15/6/8.
//  Copyright (c) 2015年 郝高明. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface myTextAttachment : NSTextAttachment

@end

//
//  myTextAttachment.m
//  11
//
//  Created by 郝高明 on 15/6/8.
//  Copyright (c) 2015年 郝高明. All rights reserved.
//

#import "myTextAttachment.h"

@implementation myTextAttachment

-(CGRect)attachmentBoundsForTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:(NSUInteger)charIndex
{
    return CGRectMake( 0 , 0 , lineFrag.size.height , lineFrag.size.height);
}
@end

然后呢,程序这样写:
UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 150, 300, 21)];
    lable.textColor = [UIColor redColor];
    [self.view addSubview:lable];
    
    //富文本
    NSString *message = @"我是郝高明,外号小胖,哈哈~";
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:message attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
    
    myTextAttachment *attachment = [[myTextAttachment alloc]init];
//    NSTextAttachment *attachment = [[NSTextAttachment alloc]initWithData:nil ofType:nil];
    UIImage *image = [UIImage imageNamed:@"80.png"];
    attachment.image = image;
//    attachment.bounds = CGRectMake(0, 0, 20, 20);
    
    NSAttributedString *text = [NSAttributedString attributedStringWithAttachment:attachment];
    [str insertAttributedString:text atIndex:5];
    
    lable.attributedText = str;

4.制作富文本,可能你需要替换掉一段文字中得特殊字符,比如:
NSString *message = @"我是郝高明[icon],外号小胖,哈哈~";
把这段话的[icon]替换成一个图片,那么这个应该怎么做呢?我们就用到了这个函数
- (void)replaceCharactersInRange:(NSRange)range withAttributedString:(NSAttributedString *)attrString;
这是一个替换函数,怎么用呢?
UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 150, 300, 21)];
    lable.textColor = [UIColor redColor];
    [self.view addSubview:lable];
    
    //富文本
    NSString *message = @"我是郝高明[icon],外号小胖,哈哈~";
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:message attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
    
    myTextAttachment *attachment = [[myTextAttachment alloc]init];
    UIImage *image = [UIImage imageNamed:@"80.png"];
    attachment.image = image;
    
    NSAttributedString *text = [NSAttributedString attributedStringWithAttachment:attachment];
    NSRange range = [[str string]rangeOfString:@"[icon]"];
    [str replaceCharactersInRange:range withAttributedString:text];
    
    lable.attributedText = str;
效果和上图的效果是一样的。

补充一点知识:

1.为某一范围内文字设置多个属性

- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;

为某一范围内文字添加某个属性

- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;

为某一范围内文字添加多个属性

- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;

移除某范围内的某个属性

- (void)removeAttribute:(NSString *)name range:(NSRange)range;

2.     常见的属性及说明

NSFontAttributeName 字体

NSParagraphStyleAttributeName 段落格式 

NSForegroundColorAttributeName 字体颜色

NSBackgroundColorAttributeName  背景颜色

NSStrikethroughStyleAttributeName删除线格式

NSUnderlineStyleAttributeName     下划线格式

NSStrokeColorAttributeName       删除线颜色

NSStrokeWidthAttributeName删除线宽度

NSShadowAttributeName 阴影

更多方法和属性说明详见苹果官方说明文档:

链接地址


源码下载:http://download.csdn.net/detail/haogaoming123/8784033

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值