小胖说事13--------NSTextAttachment富文本控件实现图文混排
2015-06-08 13:46
302 查看
1.***富文本,第一种的就是将一段文字中不通的字显示不同的颜色,大小等。
3.***富文本,你可能会有一些定制的需求,比如图片的高度不想每个都要设置一边,你可以写一个继承,然后将
然后呢,程序这样写:
4.***富文本,可能你需要替换掉一段文字中得特殊字符,比如:
补充一点知识:
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
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
相关文章推荐
- Java并发编程初级-未使用Java并发包的时候
- Can't connect to local MySQL server through socket
- 让IE6支持css3,让 IE7、IE8 都支持CSS3
- 灵活控制Docker容器中启动的进程
- Notice: Use of undefined constant MCRYPT_RIJNDAEL_128 - assumed 'MCRYPT_RIJNDAEL_128' in
- kvc VS kvo
- MAVEN项目中JSP页面使用JSTL标签
- 一次寻找邻居单词列表的算法优化
- MySql Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts' 解决方法
- 用户 'sa' 登录失败。 (Microsoft SQL Server,错误: 18456)
- javascript/js 正则表达式格式化金额
- LIGHT OJ 1278 Sum of Consecutive Integers(奇因子的个数)
- Linux 进程相关
- android使用c通过jni回调java
- 618, 大神手机的差异化“价格战”
- 呼吸信号的调研
- mysql数据文件迁移到新硬盘分区的方法
- Http 状态码对照表
- 需要牢记的各个业内术语
- macbook安装win7