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];
                    }
                }
            }];
            
        }
目录
相关文章
|
自然语言处理 iOS开发
iOS 国际化(多语言)如何指定默认语言
iOS 国际化(多语言)如何指定默认语言
694 0
|
人工智能 自然语言处理 Java
为什么越来越多企业用阿里云发短信?稳定、合规、高到达率的背后
短信仍是企业触达用户的“最后一公里”,阿里云短信服务凭借合规报备、99%高到达率、失败不计费及透明计价等优势,成为电商、物流、政务等行业首选。开发友好,10分钟快速接入,助力企业稳定高效通信。
500 0
|
10月前
|
缓存 小程序 视频直播
基于uni-app+vite5+vue3实战短视频+直播+聊天app应用
基于uniapp+vue3+vite5从0-1实战搭建仿抖音/微信直播带货商城。集短视频+聊天+直播功能于一体。实现全屏沉浸式切换短视频/直播,支持编译运行到h5+小程序端+app端。
593 4
|
10月前
|
Python
Python 办公实战:用 python-docx 自动生成 Word 文档
本文详解如何使用 python-docx 库实现 Word 文档自动化生成,涵盖环境搭建、文档创建、格式设置、表格与图片处理、模板填充、批量生成及性能优化等实战技巧,助你高效完成办公场景中的文档自动化任务。
2351 1
|
Dart 前端开发 IDE
鸿蒙Flutter实战:14-现有Flutter 项目支持鸿蒙 II
本文介绍了如何将现有 Flutter 项目适配鸿蒙系统,详细步骤包括安装 FVM、使用 FVM 安装 Flutter SDK、搭建开发环境、创建项目架构和壳工程等。
1439 6
鸿蒙Flutter实战:14-现有Flutter 项目支持鸿蒙 II
|
JavaScript 前端开发 开发者
flat、flatmap与map的用法区别
本文介绍了 JavaScript 数组方法 `flat()`、`flatMap()` 和 `map()` 的用法及区别。`flat()` 可按指定深度递归展平数组,参数为深度,默认一层;`flatMap()` 结合了 `map()` 和 `flat()` 功能,返回一维数组,长度可能不同于原数组;而 `map()` 返回与原数组长度一致的新数组。通过多个代码示例展示了三者的功能和差异,帮助开发者更好地理解和使用这些方法。
1515 0
|
存储 数据安全/隐私保护 iOS开发
应用在App Store上被拒重新提交审核流程指南
该文本是关于iOS应用发布的步骤说明
624 2
|
JavaScript 前端开发
js的join函数
js的join函数
358 1
|
iOS开发
如何识别手机是否有灵动岛(dynamic island)
如何识别手机是否有灵动岛(dynamic island)
1095 0
使用序列化和反序列化函数archivedDataWithRootObject和unarchivedObjectOfClasses的使用和遇到问题及解决方案
使用序列化和反序列化函数archivedDataWithRootObject和unarchivedObjectOfClasses的使用和遇到问题及解决方案
605 0

热门文章

最新文章