小胖说事13--------NSTextAttachment富文本控件实现图文混排
来源:互联网 发布:淘宝购买记录保存多久 编辑:程序博客网 时间:2024/05/12 13:14
1.制作富文本,第一种的就是将一段文字中不通的字显示不同的颜色,大小等。
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
- 小胖说事13--------NSTextAttachment富文本控件实现图文混排
- NSTextAttachment富文本控件实现图文混排
- NSTextAttachment富文本控件实现图文混排
- NSTextAttachment富文本控件实现图文混排
- NSTextAttachment实现图文混排
- iOS NSTextAttachment - 图文混排
- 富文本-图文混排
- label富文本 以及图文混排
- Android 富文本编辑器 图文混排
- Android 富文本编辑器 图文混排
- iOS_富文本的图文混排
- iOS 富文本、图文混排
- iOS-Swift3富文本(UILable文本图文混排)
- ios知识点总结——富文本实现图文混排
- ios知识点总结——富文本实现图文混排
- ios知识点总结——富文本实现图文混排
- ios知识点总结——富文本实现图文混排
- 如何简单的实现一个富文本,图文混排编辑器
- ftp原理
- Tomcat取消自动重启的方法(开发期的热加载)
- VIM插件介绍
- Android AudioManager获取媒体焦点
- java.lang.IllegalStateException: commit already called 的处理办法
- 小胖说事13--------NSTextAttachment富文本控件实现图文混排
- Java面试之斐波纳契数列递归与非递归实现
- oracle中查看表是否被锁
- Android Toast 显示时间问题的探讨
- Oracle数据库简介
- grails的save方法总是没有成功save数据
- 实现需要进行两步都成功的操作才算成功
- BZOJ 1855 [Scoi2010]股票交易 单调队列优化DP
- 学会给你的笔记本电池换“芯”