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

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 + 1, 0);

    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(0, -4, 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(0, attributedString.length)];

        }];
    }

}

@end
时间: 2024-08-01 22:44:47

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

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

DJComposePageView.m - (void)deleteInputEmotion { // 发通知 [[NSNotificationCenter defaultCenter] postNotificationName:DJEmotionDidDeletedNotification object:nil]; } DJComposeViewController.m // 注册监听删除表情通知 [[NSNotificationCenter defaultCenter] addObserve

新浪微博客户端(48)-添加删除按钮到表情键盘

DJEmotionPageView.m - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { UIButton *deleteBtn = [[UIButton alloc] init]; [deleteBtn setImage:[UIImage imageNamed:@"compose_emotion_delete"] forState:UIContro

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

这是一篇好文章,我转来收藏,技术的最高境界是分享. 最近在搞android 新浪微博客户端,有一些心得分享弄android客户端表情功能可以用以下思路1.首页把新浪的表情下载到本地一文件夹种,表情图片的命名要用新浪微博表情原来的命名比如 新浪的害羞表情是shame.gif 那么你在本地也得命名为shame.gif,命名相同主要是为了能够匹配表情对应的code.2.把本地的表情都放进android的资源文件里----drawable下面3.访问新浪的表情接口(新浪返回的数据类型有json和xml两

新浪微博客户端(53)-记录用户最近点击表情

设计思路:每当用户点击一个表情,就将该表情(DJEmotion)存储到沙盒,当用户切换到"最近"栏目时,从沙盒中取出用户最近点击的表情列表,进行显示. 1. 保存DJEmotion DJEmotion.h #import <Foundation/Foundation.h> // 因为要保存最近使用的表情到沙盒,所以该对象须实现NSCoding协议 @interface DJEmotion : NSObject <NSCoding> /** Emotion 中文字

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

经过一个星期的折腾,终于做完了这个Android 聊天表情输入.表情翻页带效果.下拉刷新聊天记录.这只是一个单独聊天表情的输入,以及聊天的效果实现.因为我没有写服务器,所以没有双方聊天的效果.主要是聊天中表情的选择,发送.表情翻页带有不同的效果.我在主要代码中都写了注释.下面看代码实现.附上本文源码,代码较多. 下载地址:点击 一.先看实现的效果图 二.调用接口以及设置MainActivity package com.example.activity; import java.util.Arra

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

开始接触学习android已经有3个礼拜了,一直都是对着android的sdk文档写Tutorials从Hello World到Notepad Tutorial算是初步入门了吧,刚好最近对微博感兴趣就打算开发个android版本的新浪微博客户端作为练手项目,并且以随笔的方式详细的记录开发的全过程.本人对java语言以及eclipse Ide都是初次应用基本上属于边学边用,做移动设备上的东西也是第一次,总的来说属于无基础.无经验.无天赋的纯三无人员,还请广大同学们多多给予指点. 开发第一件事情,那

模拟可编辑div输入域同时输入文字表情

近期遇到了模拟可编辑div输入域同时输入文字表情的需求,本来还觉得很好做,但是在具体实施的过程中遇到了一点问题. 第一个比较简单的问题是表情和对应字符的映射关系,这部分比较好做,没有用富文本框也没有用编辑器,做了表情和相应字符的对应关系只有就可以实现这个需求. 第二个问题在解决过程中就比较棘手了,因为是自己模拟输入域,所以对于在文字中加入表情后光标的定位及后续的输入是有要求的,就是得符合正常的输入习惯.这个问题的核心就是对于node节点的操作和光标对象的熟悉程度及其内部属性和方法的使用.对于前端

10、使用TCP协议完成一个客户端一个服务器。客户端从键盘输入读取一个字符串,发送到服务器。 服务器接收客户端发送的字符串,反转之后发回客户端。客户端接收并打印。

/**10.使用TCP协议完成一个客户端一个服务器.客户端从键盘输入读取一个字符串,发送到服务器. 服务器接收客户端发送的字符串,反转之后发回客户端.客户端接收并打印. * 客户端*/ import java.io.*; import java.net.*; public class Test10_Client { public static void main(String[] args) throws Exception { Socket s = new Socket("192.168.0.

新浪微博客户端(58)-处理点击微博内容中的关键字

DJStatus.m // 创建一个用于包含特殊文本的集合 NSMutableArray *specialTextArray = [NSMutableArray array]; // 取出数组中的文本块进行拼接 for (DJStatusPart *part in statusParts) { NSAttributedString *subString = nil; if (part.isSpecial) { // 判断是否是特殊文本(若是特殊文本,则进行特殊处理,超链接:变色,表情文本:更换成