小胖说事13--------NSTextAttachment富文本控件实现图文混排

摘要:
比方图片的高度不想每一个都要设置一边。你能够写一个继承。////myTextAttachment.h//11////Createdby郝高明on15/6/8.//Copyright2015年郝高明.Allrightsreserved.//#import@interfacemyTextAttachment:NSTextAttachment@end////myTextAttachment.m//11////Createdby郝高明on15/6/8.//Copyright2015年郝高明.Allrightsreserved.//#import"myTextAttachment.h"@implementationmyTextAttachment-attachmentBoundsForTextContainer:textContainerproposedLineFragment:lineFragglyphPosition:positioncharacterIndex:charIndex{returnCGRectMake;}@end然后呢,程序这样写:UILabel*lable=[[UILabelalloc]initWithFrame:CGRectMake];lable.textColor=[UIColorredColor];[self.viewaddSubview:lable];//富文本NSString*message=@"我是郝高明,绰号小胖。哈哈~";NSMutableAttributedString*str=[[NSMutableAttributedStringalloc]initWithString:messageattributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:16]}];myTextAttachment*attachment=[[myTextAttachmentalloc]init];//NSTextAttachment*attachment=[[NSTextAttachmentalloc]initWithData:nilofType:nil];UIImage*image=[UIImageimageNamed:@"80.png"];attachment.image=image;//attachment.bounds=CGRectMake;NSAttributedString*text=[NSAttributedStringattributedStringWithAttachment:attachment];[strinsertAttributedString:textatIndex:5];lable.attributedText=str;4.制作富文本,可能你须要替换掉一段文字中得特殊字符,比方:NSString*message=@"我是郝高明[icon],绰号小胖。哈哈~";把这段话的[icon]替换成一个图片。那么这个应该怎么做呢?我们就用到了这个函数-replaceCharactersInRange:rangewithAttributedString:attrString;这是一个替换函数。

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.制作富文本,另外一种就是将图片和文字混排

小胖说事13--------NSTextAttachment富文本控件实现图文混排第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

免责声明:文章转载自《小胖说事13--------NSTextAttachment富文本控件实现图文混排》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Kali Linux安装Oracle12c中配置实例参数和修改容器数据库(CDB)及可插拔数据库(PDB)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

ios -富文本和尺寸

/** *计算文本的宽高 方法 2 * *@param str 需要计算的文本 *@param font文本显示的字体 *@param maxSize 文本显示的范围 * *@return 文本占用的真实宽高 */ + (CGSize)sizeMethod2WithString:(NSString *)str font:(UIFont *)font max...