UITextView+Extension.h

#import <UIKit/UIKit.h>

@interface UITextView (Extension)

/** 插入属性文本 */
- (NSMutableAttributedString *)insertAttributeText:(NSAttributedString *)insertAttrText; /** 插入属性文本和要设置的block内容 */
- (NSMutableAttributedString *)insertAttributeText:(NSAttributedString *)insertAttrText settingsBlock:(void (^)(NSMutableAttributedString * attributedString)) settingsBlock; @end

UITextView+Extension.m

#import "UITextView+Extension.h"

@implementation UITextView (Extension)

- (NSMutableAttributedString *)insertAttributeText:(NSAttributedString *)insertAttrText {

    return [self insertAttributeText:insertAttrText settingsBlock:nil];

}

- (NSMutableAttributedString *)insertAttributeText:(NSAttributedString *)insertAttrText settingsBlock:(void (^)(NSMutableAttributedString *))settingsBlock {

    // 以textView的原本内容为基础构造一个attrStr
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText]; // 将附件文本插入到光标所在的位置
NSUInteger cursorLocation = self.selectedRange.location;
[attrStr insertAttributedString:insertAttrText atIndex:cursorLocation]; // 如果传入了block,就调用block
if(settingsBlock) {
settingsBlock(attrStr);
} // 更新当前textView内容
self.attributedText = attrStr; // 修正当前光标位置(将光标移动到插入表情末尾,默认光标会跳转到所有文本最后)
self.selectedRange = NSMakeRange(cursorLocation + , ); return attrStr; } @end

DJEmotionTextView.m

#import "DJEmotionTextView.h"
#import "DJEmotion.h" @implementation DJEmotionTextView - (void)insertEmotion:(DJEmotion *)emotion { // 插入表情
if (emotion.code) { // Emoji表情
[self insertText:[NSString emojiWithStringCode:emotion.code]];
} else if (emotion.png) { // 表情图片
NSString *emotionName = emotion.png;
NSString *imagePath;
if ([emotionName hasPrefix:@"d_"] || [emotionName hasPrefix:@"f_"] ||
[emotionName hasPrefix:@"h_"] || [emotionName hasPrefix:@"l_"] || [emotionName hasPrefix:@"o_"] || [emotionName hasPrefix:@"w_"]) {
imagePath = [NSString stringWithFormat:@"EmotionIcons/default/%@",emotion.png]; // 默认表情路径
} else if ([emotionName hasPrefix:@"lxh_"]) {
imagePath = [NSString stringWithFormat:@"EmotionIcons/lxh/%@",emotion.png]; // 浪小花表情路径
} // 构造表情附件
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:imagePath];
CGFloat attachmentWH = self.font.lineHeight;
attachment.bounds = CGRectMake(, -, attachmentWH, attachmentWH);
NSAttributedString *attachStr = [NSAttributedString attributedStringWithAttachment:attachment]; // 插入属性文本
[self insertAttributeText:attachStr settingsBlock:^(NSMutableAttributedString *attributedString) { // 设置当前attrStr的字体与textView原有字体大小一致,因为attrStr的字体无法通过textview.font属性来设置
[attributedString addAttribute:NSFontAttributeName value:self.font range:NSMakeRange(, attributedString.length)]; }];
} } @end

新浪微博客户端(50)-解决输入Emotion表情逐渐变小的问题的更多相关文章

  1. vue-cli3.0结合lib-flexible、px2rem实现移动端适配,完美解决第三方ui库样式变小问题

    公司最近做的一个移动端项目从搭框架到前端开发由我独立完成,以前做移动端适配用的媒体查询,这次想用点别的适配方案,然后就采用了vue-cli3.0结合lib-flexible.px2rem实现移动端适配 ...

  2. 新浪微博客户端(49)-删除输入的Emotion表情

    DJComposePageView.m - (void)deleteInputEmotion { // 发通知 [[NSNotificationCenter defaultCenter] postNo ...

  3. android 新浪微博客户端的表情功能的实现

    这是一篇好文章,我转来收藏,技术的最高境界是分享. 最近在搞android 新浪微博客户端,有一些心得分享弄android客户端表情功能可以用以下思路1.首页把新浪的表情下载到本地一文件夹种,表情图片 ...

  4. android开发新浪微博客户端 完整攻略 [新手必读]

    开始接触学习android已经有3个礼拜了,一直都是对着android的sdk文档写Tutorials从Hello World到Notepad Tutorial算是初步入门了吧,刚好最近对微博感兴趣就 ...

  5. Android 聊天表情输入、表情翻页带效果、下拉刷新聊天记录

    经过一个星期的折腾,最终做完了这个Android 聊天表情输入.表情翻页带效果.下拉刷新聊天记录.这仅仅是一个单独聊天表情的输入,以及聊天的效果实现.由于我没有写server,所以没有两方聊天的效果. ...

  6. Aisen仿新浪微博客户端项目源码

    新浪目前已经限制了第三方微博的很多API接口,加上平常时间不够,所以后续可能不会面向产品的去维护Aisen,不过也有了一些新的方向,例如引入最新Android-support-library,在一个完 ...

  7. Android应用--新浪微博客户端新特性滚动视图和启动界面实现

    新浪微博客户端新特性滚动视图和启动界面实现 2013年8月20日新浪微博客户端开发之启动界面实现 前言: 使用过新浪微博客户端的童鞋都清楚,客户端每一次升级之后第一次启动界面就会有新特性的介绍,用户通 ...

  8. 新浪微博客户端开发之OAuth认证篇

    新浪微博客户端开发之OAuth认证篇 2013年7月29日新浪微博客户端开发 OAuth2.0授权机制我在这里就不浪费口舌了,有很多大牛都发表过相关的文章解释OAuth2.0认证的流程,我就随便找了一 ...

  9. tcp程序设计--客户端获取服务器输入输出流

    tcp程序设计--客户端获取服务器输入输出流 思路: 第一步:实例化一个ServerSocket对象(服务器套接字),用来等待网络上的请求(也就是等待来连接的套接字) 第二步:调用accept()方法 ...

随机推荐

  1. 源代码管理工具(上)-SVN基本使用

    ------------------------------------------------------SVN简介和搭建 ------------------------------------- ...

  2. 58. Android一些开发习惯总结

    作者:漫步 链接:https://www.zhihu.com/question/27227425/answer/35973793 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...

  3. CODE[VS] 1346 HelloWorld编译器

    1346 HelloWorld编译器  时间限制: 1 s  空间限制: 1000 KB  题目等级 : 黄金 Gold     题目描述 Description 请编程判断一个负责打印HelloWo ...

  4. eclipse安装插件的方法(简单、ERMaster插件安装)

    ERMaster插件:https://sourceforge.net/projects/ermaster/ 链接:http://pan.baidu.com/s/1o7UWLMa 密码:wkax 可以编 ...

  5. NuGet多个项目依赖的公共组件如何打包

    会有这样一种情况:在同一个解决方案下面,类库A是独立的,类库B是依赖于类库A的:类似这样: 所以在使用类库B时必须引入类库A的东西,这时如果作为nuget包打包发布,有如下的解决思路: 1.在整个解决 ...

  6. Bzoj2683 简单题 [CDQ分治]

    Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 1071  Solved: 428 Description 你有一个N*N的棋盘,每个格子内有一个整数, ...

  7. 正则表达式re模块

    正则表达式模块re 1. 正则简介 就其本质而言,正则表达式(或 RE)是一种小型的.高度专业化的编程语言, (在Python中)它内嵌在Python中,并通过 re 模块实现.正则表达式模式被 编译 ...

  8. codevs 1432 总数统计

    1432 总数统计 时间限制: 1 s空间限制: 128000 KB题目等级 : 钻石 Diamond   题目描述 Description 给出n个数,统计两两之和小于k的方案数之和. 输入描述 I ...

  9. JS方法集

    数组方法集 Angela.array = { //# 数组方法 // index, 返回位置! 不存在则返回 -1: index: function (t, arr) { //# 返回当前值所在数组的 ...

  10. UOJ#246. 【UER #7】套路

    题目传送门 官方题解传送门 一句话题意的话就是给定一个序列,从中找出至少$k$个连续的元素形成子序列,使得子序列中任意两个元素差值的最小值于其长度-1的乘积最大. 题目中给出了$ 1 \leq a_i ...