UITextView+Extension.h

  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface UITextView (Extension)
  4.  
  5. /** 插入属性文本 */
  6. - (NSMutableAttributedString *)insertAttributeText:(NSAttributedString *)insertAttrText;
  7.  
  8. /** 插入属性文本和要设置的block内容 */
  9. - (NSMutableAttributedString *)insertAttributeText:(NSAttributedString *)insertAttrText settingsBlock:(void (^)(NSMutableAttributedString * attributedString)) settingsBlock;
  10.  
  11. @end

UITextView+Extension.m

  1. #import "UITextView+Extension.h"
  2.  
  3. @implementation UITextView (Extension)
  4.  
  5. - (NSMutableAttributedString *)insertAttributeText:(NSAttributedString *)insertAttrText {
  6.  
  7. return [self insertAttributeText:insertAttrText settingsBlock:nil];
  8.  
  9. }
  10.  
  11. - (NSMutableAttributedString *)insertAttributeText:(NSAttributedString *)insertAttrText settingsBlock:(void (^)(NSMutableAttributedString *))settingsBlock {
  12.  
  13. // 以textView的原本内容为基础构造一个attrStr
  14. NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
  15.  
  16. // 将附件文本插入到光标所在的位置
  17. NSUInteger cursorLocation = self.selectedRange.location;
  18. [attrStr insertAttributedString:insertAttrText atIndex:cursorLocation];
  19.  
  20. // 如果传入了block,就调用block
  21. if(settingsBlock) {
  22. settingsBlock(attrStr);
  23. }
  24.  
  25. // 更新当前textView内容
  26. self.attributedText = attrStr;
  27.  
  28. // 修正当前光标位置(将光标移动到插入表情末尾,默认光标会跳转到所有文本最后)
  29. self.selectedRange = NSMakeRange(cursorLocation + , );
  30.  
  31. return attrStr;
  32.  
  33. }
  34.  
  35. @end

DJEmotionTextView.m

  1. #import "DJEmotionTextView.h"
  2. #import "DJEmotion.h"
  3.  
  4. @implementation DJEmotionTextView
  5.  
  6. - (void)insertEmotion:(DJEmotion *)emotion {
  7.  
  8. // 插入表情
  9. if (emotion.code) { // Emoji表情
  10. [self insertText:[NSString emojiWithStringCode:emotion.code]];
  11. } else if (emotion.png) { // 表情图片
  12. NSString *emotionName = emotion.png;
  13. NSString *imagePath;
  14. if ([emotionName hasPrefix:@"d_"] || [emotionName hasPrefix:@"f_"] ||
  15. [emotionName hasPrefix:@"h_"] || [emotionName hasPrefix:@"l_"] || [emotionName hasPrefix:@"o_"] || [emotionName hasPrefix:@"w_"]) {
  16. imagePath = [NSString stringWithFormat:@"EmotionIcons/default/%@",emotion.png]; // 默认表情路径
  17. } else if ([emotionName hasPrefix:@"lxh_"]) {
  18. imagePath = [NSString stringWithFormat:@"EmotionIcons/lxh/%@",emotion.png]; // 浪小花表情路径
  19. }
  20.  
  21. // 构造表情附件
  22. NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
  23. attachment.image = [UIImage imageNamed:imagePath];
  24. CGFloat attachmentWH = self.font.lineHeight;
  25. attachment.bounds = CGRectMake(, -, attachmentWH, attachmentWH);
  26. NSAttributedString *attachStr = [NSAttributedString attributedStringWithAttachment:attachment];
  27.  
  28. // 插入属性文本
  29. [self insertAttributeText:attachStr settingsBlock:^(NSMutableAttributedString *attributedString) {
  30.  
  31. // 设置当前attrStr的字体与textView原有字体大小一致,因为attrStr的字体无法通过textview.font属性来设置
  32. [attributedString addAttribute:NSFontAttributeName value:self.font range:NSMakeRange(, attributedString.length)];
  33.  
  34. }];
  35. }
  36.  
  37. }
  38.  
  39. @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. iOS极光推送 点击推送消息跳转页面

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launc ...

  2. 【BZOJ-2503】相框 并查集 + 分类讨论

    2503: 相框 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 71  Solved: 31[Submit][Status][Discuss] Desc ...

  3. BZOJ1912 [Apio2010]patrol 巡逻

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...

  4. ES5/ES6的区别研究(ECMAScript)

    我所理解的概念应该是语法的区别和特性的区别 这里是ECMAScript的解析http://baike.baidu.com/item/ECMAScript 参考: (ES6)http://es6.rua ...

  5. Ubuntu下root密码认证错误

    1.默认root密码是随机的,即每次开机都有一个新的root密码.我们可以在终端输入命令 sudo passwd root,然后输入当前用户的密码,再输入要设置root的密码,我们一般设置成两个都一样 ...

  6. Andorid视觉新冲击-Material design语言

    [写在前面] google在2014年 I/O大会上推出了一种新的设计设计语言—Material design,这种设计语言语言旨在为手机.平板电脑.台式机和“其他平台”提供更一致.更广泛的“外观和感 ...

  7. Bzoj1189 [HNOI2007]紧急疏散evacuate

    1189: [HNOI2007]紧急疏散evacuate Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2293  Solved: 715 Descr ...

  8. [NOIP2013] 提高组 洛谷P1969 积木大赛

    题目描述 春春幼儿园举办了一年一度的“积木大赛”.今年比赛的内容是搭建一座宽度为n的大厦,大厦可以看成由n块宽度为1的积木组成,第i块积木的最终高度需要是hi. 在搭建开始之前,没有任何积木(可以看成 ...

  9. c/c++中#和##链接符号的用法

    #include <stdio.h> #include <stdlib.h> /* 英语原文: In function-like macros, a # operator be ...

  10. 使用enum建立简单的状态机

    Overview The enum in Java is more powerful than many other languages which can lead to surprising us ...