UITextView with emoticons editing - ios

I am developing xmpp-client app. One of the features is sending smiles and user should have ability to edit its like usuall text. Emoticons editing in Viber App is best example of what i want to implement.
I already tried three ways to solve problem:
I create emoticon like usual UIImageView and place it as a subview on UITextView using current caret rect. I use 5 whitespaces as a text placeholder in text view. There are two problems: with placing emoticons on new line when inserting text in the middle(printing whitespace not make caret move to new line); when user placing caret using magnify glass, he can move caret through emoticon(through 5 whitespaces), as delegate method not called during this process.
I have tried EGOTextView. There are problems with caret position and resizing when new line should be added. And there are some rendering artifacts when using it one line size.
I also have tried using UIWebView. But there were great problems with resizing based on text size and other artifacts with speed of response when becoming first responder.
May be some one could give me advice of really working solution?

I'm not really into rects and ranges mechanism of UITextView/UITextInput, but I try to give you an (untested) advice that maybe could achieve the expected result.
Built-in iOS emojis are simple characters, so we can follow the same path by building a custom font (or extend an existing one).
We have two options:
If you want to target iOS 6.0 (that has native support for NSAttributedString in UIKit classes), you could try to build a custom font containing all the emoticons you need, and use it inside your NSAttributedString (attributed string can mix different fonts, font sizes, styles and so on).
You could do something similar with iOS 5, but since you can't use NSAttributedString inside UITextView (so you are limited to just one font for the entire text) you should use a font that combines together the actual characters and the custom emoticons: so you should extend the font you want to use for typing, by adding all the emoticons to it. I don't know if this could have licenses implications, anyway.
Otherwise, you could always go much more low-level, implementing your own custom textView using CoreText, but I think it would be a hard work.

Related

iOS how to insert UITextField inline within UITextView

Is there a way to insert text inputs inline inside a block of text? I need to allow users to input some data inline within text. I thought that I would be able to use NSMutableString with attachments, but it seems to support only UIImage as attachments.
This is an example of what I'm trying to achieve:
Thanks!
You should take a closer look to CoreText which gives you a very fine grained access to text layout. But anyway, this is a difficult task. You may create a HTML-Page with input fields and display it in a WKWebView, which should be much easier to implement.
If the text is static, create a custom UIView class, and position the UILabel and UITextView controls. This is the only possible approach.
If the text is dynamic, you can make use of Auto-layout concept along with the above approach.

Cursor issue while creating Custom keyboard with UIView

My requirement was to make a application specific keyboard i.e with a different theme and different key contents as compared to the default keyboard. But the way Apple states to implement keyboard requires user effort to add it through Settings->General->Keyboard which I don't want.
So, for that I tried to implement it using a UIView and making it the inputView for UITextView and it is working perfectly fine except for one issue. I added a functionality to add a linebreak in the textView. For that the code I used is
textView.text = "\(textView.text)\n"
Now, whenever I add a linebreak through keyboard, line is changed, but a new cursor appears to blink above the old cursor. I don't know how to get rid of that.
To explain the problem in a better way, here's a GIF image of the issue.
Please help.

Change NSAttribtuedString Formatting Starting at Index

I have a UITextView with attributes enabled, and I want to change the formatting (font/alignment/color/etc) within the NSAttributedString starting at a point. In other words, there is no specific part of the underlying NSAttributedString I want to change per se, I just want to make the next thing added into the UITextView have different formatting from what is in there so far.
For example, you might imagine a UITextVIew with a string inside it, and then the user enters a new line and taps a button to increase the font size. The user does not want to increase the font size of everything, just starting at that point.
Can this be done? Thanks.
If you need precise control over the dynamic style behavior of your UITextView (and if you are >= iOS 7), you should consider using Text Kit, and in particular subclassing NSTextStorage. There is a introduction video to Text Kit from WWDC 2013, and a great tutorial by Ray Wenderlich.

Add tap gesture recogniser on a specific word in a UITextView

I have a UITextView with some text. This text is an Attributed String(NSAttributedString). There are certain portions of the text that i have set to bold, and want to add a TapGestureRecogniser to those specific words only.
Till now, i have been using the textViewDidChangeSelection method of the UITextView delegate. But this is causing issues in other parts of the project.
Is there a more direct approach to this ?
You can only add a GestureRecognizer to a view, not to some words.
It's a quite complex task, there's no easy solution for it.
I can think in some approaches, for example:
Place a transparent view on top of the bold words to get the Tap.
Detect the Tap in all the UITextView, and then calculate based on the position of the touch and the position of the bold words if it hit one.
Both options requiere a lot of code and a lot of edge cases where it can fail.
As I said, it's a really complex situation, you may want to keep using textViewDidChangeSelection and fix the issues, we can help you.

How do you have two fonts in one UITextView (Xcode)?

I'm trying to make two font styes in one UITextView, how do I do this?
From the class reference:
This class does not support multiple styles for text. The font, color,
and text alignment attributes you specify always apply to the entire
contents of the text view. To display more complex styling in your
application, you need to use a UIWebView object and render your
content using HTML.
You cannot have two on the same page because it is not supported. Just use a webview and an HTML file
You can know use this by using the attributedText property of UITextView. This is available under iOS 6
UITextView supports just a single font, but there's a different topic on something similar:
Can I use multiple font style in UITextView?
If the text you want to draw is simple, I'd suggest subclassing UITextView or UIView, overwriting the drawRect function and work with some extra variables of your own. This only works if you have a very predictable system to the fonts though.
Another option is using multiple labels, which would probably need an ever more predictable setup.

Resources