Reputation: 195
HI SO Developers and members,
I am developing an IOS app and i am integrating data from respective Web application to my application ,those pretty good web application developers are implementing emojies in chat serrvice by their own custom images such as .png. so i am getting the conversation messages like (message + .png image path )and these paths are at various locations in the text and at multiple times.so i have to display the .png images in the UITextview is this practically possible to do so,any suggestions are highly appreciated and thanks a lot for your time for this.
Input: HI!(some path of image as string)good morning(some path of image as string)its really a worth seeking
Output: HI()good morning() its really a worth seeking
Note:assume () this is a png image that i should show as emoji.
Upvotes: 2
Views: 1212
Reputation: 251
To identify the substrings that need to be replaced by images you could use a regular expression. Ray Wenderlich provides a decent tutorial on regex in iOS (swift or objective-c). For an SO example of regex extracting URLs from a string, which may be similar to what you need to implement, see: Using NSRegularExpression to extract URLs on the iPhone.
You could then loop through the matches in order to build up your message containing images. How you build up your message with the images depends on how you plan to access the images.
If you don't wish to download and store the images, either on the device or in memory, then an alternative solution to the UITextView
would be to add the appropriate html tags to your message string (namely img src
) and use a UIWebView
(docs). E.g.
let exampleMessageString = "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"
let htmlMessageString = convertMessageStringToHtml(exampleMessageString)
// where convertMessageStringToHtml implements your regex and returns something like:
// Text before image <img src=\"http://www.androidicons.com/assets/images/ai-mdpi-black/ic_action_achievement.png\" /> text in between images <img src=\"http://www.androidicons.com/assets/images/ai-mdpi-black/ic_action_armchair.png\" /> text after images
let webView = UIWebView(frame: CGRectMake(x, y, width, height))
webView.loadHTMLString(htmlMessageString, baseURL: nil)
A WebView
may not offer you the level of control you desire however. In which case you can use an attributed string (docs) to display the message with images in your UITextView
. You could still loop through your regex in the same way, but instead of building a html string you'd create attributed strings. There are plenty of examples of how to use attributed strings in other SO answers:
iOS 7 TextKit - How to insert images inline with text?
How to add image and text in UITextView in IOS?
If you need some help downloading images see: iOS download and save image inside app
Upvotes: 2