UITextView的attributedText含有NSTextAttachment对象、表情包和普通文本的遍历与组装h5字符串

简介: UITextView的attributedText含有NSTextAttachment对象、表情包和普通文本的遍历与组装h5字符串

UITextView的attributedText含有NSTextAttachment对象、表情包和普通文本的可以遍历。注意:表情包和普通文本被遍历时相似单又被分开。

表情包的插入与移动光标

        NSString *str = getNotNilString([emojiStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]);
        [self.inputTextView.textStorage insertAttributedString:[[NSAttributedString alloc] initWithString:str] atIndex:self.inputTextView.selectedRange.location];
        self.inputTextView.selectedRange = NSMakeRange(self.inputTextView.selectedRange.location+str.length, 0);

使用enumerateAttributesInRange遍历

        NSAttributedString *attributedText = self.inputTextView.attributedText;
        self.model.Body = @"";
        if(!isCommonUnitEmpty(self.inputTextView.text))
        {
            [attributedText enumerateAttributesInRange:NSMakeRange(0, attributedText.length) options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:
             ^(NSDictionary *attributes, NSRange range, BOOL *stop) {
                @strongify(self);
                DYTextAttachment *attachment = [attributes objectForKey:@"NSAttachment"];
                if(attachment)
                {
                    NSLog(@"attributes:%@, range.location:%d,range.length:%d,attachment.description:%@ attachment:%@,", attributes, range.location,range.length,attachment.description, attachment);
                    if([attachment isKindOfClass:[DYTextAttachment class]] && !isCommonUnitEmptyString(attachment.dataFilePath))
                    {
                        if(attachment.dyFileType == DYFileTypeImage)
                        {
                            self.model.Body = [NSString stringWithFormat:@"%@<img src=\"%@\"/>", self.model.Body, attachment.dataFilePath];
                        }
                        else if(attachment.dyFileType == DYFileTypeVideo)
                        {
                            self.model.Body = [NSString stringWithFormat:@"%@<p><video src=\"%@\" controls=\"controls\" style=\"max-width:100%%\"></video></p>", self.model.Body, attachment.dataFilePath];
                        }
                    }
                }
                else
                {
                    NSLog(@"attributes:%@, range.location:%d,range.length:%d, [self.inputTextView.text substringWithRange:range]:%@", attributes, range.location,range.length, [self.inputTextView.text substringWithRange:range]);
                    NSString *inputStr = [self.inputTextView.text substringWithRange:range];
                    if(!isCommonUnitEmptyString(inputStr))
                    {
                        self.model.Body = [NSString stringWithFormat:@"%@%@", self.model.Body, inputStr];
                    }
                }
            }];
            
        }
目录
相关文章
|
人工智能 自然语言处理 Java
为什么越来越多企业用阿里云发短信?稳定、合规、高到达率的背后
短信仍是企业触达用户的“最后一公里”,阿里云短信服务凭借合规报备、99%高到达率、失败不计费及透明计价等优势,成为电商、物流、政务等行业首选。开发友好,10分钟快速接入,助力企业稳定高效通信。
231 0
|
自然语言处理 iOS开发
iOS 国际化(多语言)如何指定默认语言
iOS 国际化(多语言)如何指定默认语言
627 0
|
7月前
|
缓存 小程序 视频直播
基于uni-app+vite5+vue3实战短视频+直播+聊天app应用
基于uniapp+vue3+vite5从0-1实战搭建仿抖音/微信直播带货商城。集短视频+聊天+直播功能于一体。实现全屏沉浸式切换短视频/直播,支持编译运行到h5+小程序端+app端。
483 4
|
Dart 前端开发 IDE
鸿蒙Flutter实战:14-现有Flutter 项目支持鸿蒙 II
本文介绍了如何将现有 Flutter 项目适配鸿蒙系统,详细步骤包括安装 FVM、使用 FVM 安装 Flutter SDK、搭建开发环境、创建项目架构和壳工程等。
1273 5
鸿蒙Flutter实战:14-现有Flutter 项目支持鸿蒙 II
|
机器学习/深度学习 自然语言处理 搜索推荐
深度学习之分类网络
深度学习的分类网络(Classification Networks)是用于将输入数据分配到预定义类别的神经网络。它们广泛应用于图像分类、文本分类、语音识别等任务。以下是对深度学习分类网络的详细介绍,包括其基本概念、主要架构、常见模型、应用场景、优缺点及未来发展方向。
1219 4
|
存储 数据安全/隐私保护 iOS开发
应用在App Store上被拒重新提交审核流程指南
该文本是关于iOS应用发布的步骤说明
464 2
|
监控 Java API
深入解析 Spring Cloud Sentinel:分布式系统流量控制与熔断降级的全面指南
深入解析 Spring Cloud Sentinel:分布式系统流量控制与熔断降级的全面指南
530 0
深入解析 Spring Cloud Sentinel:分布式系统流量控制与熔断降级的全面指南
|
iOS开发
如何识别手机是否有灵动岛(dynamic island)
如何识别手机是否有灵动岛(dynamic island)
912 0
使用序列化和反序列化函数archivedDataWithRootObject和unarchivedObjectOfClasses的使用和遇到问题及解决方案
使用序列化和反序列化函数archivedDataWithRootObject和unarchivedObjectOfClasses的使用和遇到问题及解决方案
537 0
|
算法 JavaScript 开发者
通过UIActivityViewController分享多图片及如何把分享时微信图标放置到第一位
通过UIActivityViewController分享多图片及如何把分享时微信图标放置到第一位
322 0