菜鸟教程小白 发表于 2022-12-12 14:28:37

ios - 动态地将图像添加到不同位置的uitextview


                                            <p><p>HI SO 开发人员和成员,</p>

<p>我正在开发一个 IOS 应用程序,我正在将来自各个 Web 应用程序的数据集成到我的应用程序中,那些非常优秀的 Web 应用程序开发人员正在通过他们自己的自定义图像(例如 .png)在聊天服务中实现表情符号。所以我得到了像(消息+ .png图像路径)这样的对话消息,这些路径在文本中的不同位置并且多次出现。所以我必须在UITextview中显示.png图像这实际上是可能的吗,任何建议都非常感谢,非常感谢您为此付出的时间。</p>

<p>输入:你好!(图像的一些路径作为字符串)早上好(图像的一些路径作为字符串)它真的值得寻找</p>

<p>输出:HI()good morning() 真的很值得寻找</p>

<p>注意:假设 () 这是一个 png 图像,我应该显示为表情符号。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>要识别需要被图像替换的子字符串,您可以使用正则表达式。 Ray Wenderlich 提供了一个关于 iOS 正则表达式的不错的教程(<a href="http://www.raywenderlich.com/86205/nsregularexpression-swift-tutorial" rel="noreferrer noopener nofollow">swift</a> 或 <a href="http://www.raywenderlich.com/30288/nsregularexpression-tutorial-and-cheat-sheet" rel="noreferrer noopener nofollow">objective-c</a>)。有关从字符串中提取 URL 的正则表达式的 SO 示例,可能类似于您需要实现的内容,请参阅:<a href="https://stackoverflow.com/questions/9587458/using-nsregularexpression-to-extract-urls-on-the-iphone" rel="noreferrer noopener nofollow">Using NSRegularExpression to extract URLs on the iPhone</a> .</p>

<p>然后您可以遍历匹配项以构建包含图像的消息。如何使用图像构建信息取决于您计划如何访问图像。</p>

<p>如果您不希望在设备上或内存中下载和存储图像,则 <code>UITextView</code> 的替代解决方案是将适当的 html 标记添加到您的消息字符串(即 <code>img src</code>)并使用 <code>UIWebView</code> ( <a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWebView_Class/" rel="noreferrer noopener nofollow">docs</a> )。例如</p>

<pre><code>let exampleMessageString = &#34;Text before image http://www.androidicons.com/assets/images/ai-mdpi-black/ic_action_achievement.png text in between images http://www.androidicons.com/assets/images/ai-mdpi-black/ic_action_armchair.png text after images&#34;
let htmlMessageString = convertMessageStringToHtml(exampleMessageString)
// where convertMessageStringToHtml implements your regex and returns something like:
// Text before image &lt;img src=\&#34;http://www.androidicons.com/assets/images/ai-mdpi-black/ic_action_achievement.png\&#34; /&gt; text in between images &lt;img src=\&#34;http://www.androidicons.com/assets/images/ai-mdpi-black/ic_action_armchair.png\&#34; /&gt; text after images

let webView = UIWebView(frame: CGRectMake(x, y, width, height))
webView.loadHTMLString(htmlMessageString, baseURL: nil)
</code></pre>

<p><code>WebView</code> 可能无法为您提供所需的控制级别。在这种情况下,您可以使用属性字符串 (<a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/NSAttributedString_UIKit_Additions/index.html" rel="noreferrer noopener nofollow">docs</a>) 在 <code>UITextView</code> 中显示带有图像的消息。您仍然可以以相同的方式循环通过您的正则表达式,但不是构建一个 html 字符串,而是创建属性字符串。在其他 SO 答案中有很多关于如何使用属性字符串的示例:</p>

<p> <a href="https://stackoverflow.com/questions/20930462/ios-7-textkit-how-to-insert-images-inline-with-text/20930656#20930656" rel="noreferrer noopener nofollow">iOS 7 TextKit - How to insert images inline with text?</a> </p>

<p> <a href="https://stackoverflow.com/questions/24010035/how-to-add-image-and-text-in-uitextview-in-ios" rel="noreferrer noopener nofollow">How to add image and text in UITextView in IOS?</a> </p>

<p>如果您在下载图片时需要帮助,请参阅:<a href="https://stackoverflow.com/questions/6238139/ios-download-and-save-image-inside-app" rel="noreferrer noopener nofollow">iOS download and save image inside app</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 动态地将图像添加到不同位置的uitextview,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/28803748/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/28803748/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 动态地将图像添加到不同位置的uitextview