小胖说事13--------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 阴影

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

https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableAttributedString_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40003689


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

posted on 2017-06-25 10:16  wgwyanfs  阅读(247)  评论(0编辑  收藏  举报

导航