惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
News and Events Feed by Topic
P
Privacy International News Feed
Apple Machine Learning Research
Apple Machine Learning Research
A
Arctic Wolf
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Threat Research - Cisco Blogs
Spread Privacy
Spread Privacy
宝玉的分享
宝玉的分享
P
Proofpoint News Feed
P
Privacy & Cybersecurity Law Blog
GbyAI
GbyAI
The Hacker News
The Hacker News
K
Kaspersky official blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Cyber Attacks, Cyber Crime and Cyber Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
A
About on SuperTechFans
S
Secure Thoughts
G
Google Developers Blog
博客园 - 司徒正美
Forbes - Security
Forbes - Security
T
The Exploit Database - CXSecurity.com
Latest news
Latest news
V2EX - 技术
V2EX - 技术
I
InfoQ
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Security Latest
Security Latest
AI
AI
T
Tailwind CSS Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cloudbric
Cloudbric
V
Vulnerabilities – Threatpost
Schneier on Security
Schneier on Security
P
Proofpoint News Feed
博客园 - 聂微东
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Schneier on Security
博客园 - 【当耐特】
NISL@THU
NISL@THU
Y
Y Combinator Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
L
Lohrmann on Cybersecurity
Help Net Security
Help Net Security
The Cloudflare Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More

土土哥的Blog

好久没有更新博客=。= 阿里巴巴国际无线技术部 - 招人啦~求iOS、Android、Java 转-阿里巴巴国际无线技术部 - 在这里遇见最好的自己 反编译分析并模拟实现methodSignatureForSelector方法 反编译分析Xcode8的Bug, release下连续两次调用有二级指针参数的空方法会Crash 有趣的Autolayout示例5-Masonry实现 在对象dealloc的后期执行Task-开源库TTGDeallocTaskHelper 用QuartzCode快速实现一个收藏动画 开源项目-拼图验证控件TTGPuzzleVerify的实现 Swift开源Mac App - BingWallPaper 有趣的Autolayout示例4-Masonry实现 翻译-为什么objc_msgSend必须用汇编实现 API返回结果设计经验与总结 结合访问Out Parameters出现EXC_BAD_ACCESS的例子,反编译汇编解读__autoreleasing 总结一些iOS项目中组织代码的方法 对组件化与模块化的思考与总结 开源项目-TTGTagCollectionView 有趣的Autolayout示例3-Masonry实现 Swift开源项目: TTGEmojiRate的实现 Swift写的库-TTGSnackbar 有趣的Autolayout示例2-Masonry实现 解决iOS项目的版本兼容问题-结合宏、Category和Runtime 用Runtime的手段填充任意NSObject对象的nil属性 有趣的Autolayout示例-Masonry实现 RPC框架Thrift例子-PHP调用C++后端程序 GCD使用经验与技巧浅谈 为GCD队列绑定NSObject类型上下文数据-利用__bridge_retained(transfer)转移内存管理权 Enum-枚举的正确使用-Effective-Objective-C-读书笔记-Item-5 @autoreleasepool-内存的分配与释放 有关宏定义的经验与技巧-简化代码-增强Log Effective-Objective-C-读书笔记-Item-4-如何正确定义常量 UITextView编辑时插入自定义表情-简单的图文混编 关于评论不见了=。= Entity和Model的不同-关于代码的数据层 一次审核被拒的经历-关于iCloud到底应该备份什么数据 Block类型变量-缓存Http请求与回调 提升UITableView性能-复杂页面的优化 NSString的Copy与内存分配 利用NSProxy实现消息转发-模块化的网络接口层设计-原创 Effective-Objective-C-读书笔记-Item-3 Effective-Objective-C-读书笔记-Item-2 Effective-Objective-C-读书笔记-Item-1 iOS项目的目录结构-原创 Android开源库-LinkTextView-原创 第一篇Blog
UITextView编辑时插入自定义表情-续-自定义表情图片的大小
土土哥 · 2015-04-21 · via 土土哥的Blog

更新

  • 2015-11-27:增加了利用NSTextAttachment的bounds属性修改大小的方法。

前言

本文是基于UITextView编辑时插入自定义表情-简单的图文混编写的,主要实现了在UITextView插入表情图片时,自定义表情大小的功能。

Github

本文代码工程地址:https://github.com/zekunyan/UITextViewDIYEmojiExample

Gif示例图

Gif示例图

关键点

首先要明确我们要什么,很简单,就是指定NSTextAttachment在被绘制时的大小

所以,按照这个思路,就可以去找找NSTextAttachment类的相关方法,看能不能通过继承或其他的方式改变大小。

方法1 - NSTextAttachmentContainer

NSTextAttachment实现了“NSTextAttachmentContainer”这个Protocol,而这个Protocol里面有如下方法:

1
2
3
4
- (CGRect)attachmentBoundsForTextContainer:(NSTextContainer *)textContainer
proposedLineFragment:(CGRect)lineFrag
glyphPosition:(CGPoint)position
characterIndex:(NSUInteger)charIndex

再看看其解释:

1
Returns the layout bounds of the text attachment to the layout manager. (required)

也就是说,在绘制NSTextAttachment的内容的时候,内容的“Bounds”是通过这个方法获得的。所以,可以重写这个方法,来达到我们的目的。

方法2 - 直接修改bounds

NSTextAttachment还有一个属性@property(nonatomic) CGRect bounds,其解释如下:

1
Defines the layout bounds of the receiver's graphical representation in the text coordinate system.

所以,修改这个也可以达到目的。

实现方法1 - 继续扩展NSTextAttachment类

保存自定义Size

根据之前的定义,我们在自定义的类“EmojiTextAttachment”中再加一个保存大小的属性,如下:

1
2
3
4
5
6
7
8
9


@interface EmojiTextAttachment : NSTextAttachment

@property(strong, nonatomic) NSString *emojiTag;


@property(assign, nonatomic) CGSize emojiSize;
@end

有了“emojiSize”这个属性,我们就可以在自由的指定每个NSTextAttachment的大小。

重写

接下来就是重写方法,不多说,见代码:

1
2
3
4
5
6
7
8
9
10


@implementation EmojiTextAttachment


- (CGRect)attachmentBoundsForTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:(NSUInteger)charIndex {

return CGRectMake(0, 0, _emojiSize.width, _emojiSize.height);
}
@end

实现方法2

直接在创建表情NSTextAttachment的时候设置bounds属性即可。

使用

增加了emojiSize属性后,我们就可以在创建表情,甚至创建后,自由的调整每个NSTextAttachment的大小。

总结

一共只增加了十几行代码,但是效果还是不错哒~

参考