I have been struggling with this. User needs to enter text and/or emoticons in a TextView. I got an emoticon keyboard with my own images to enter emoticons. Problem is I need to keep a symbol (e.g. "(smile)" for the emoticon within the text while AT THE SAME TIME showing the emoticon picture on top of the symbol.
So user would see "Hello [the picture]" while the TextView.text property would return "Hello (smile)".
On Android you can use Spanned strings which allow you to cover part of your text with an image. Thus on Android I managed to achieve my objective without problem.
On iOS, I thought Attributed Strings were a similar concept to Spanned but so far all I have been able to do is entirely replace the emoticon's code with the picture (using NSTextAttachment). Is there a way to achieve my objective without having to maintain one attributed string containing pictures and one separate string containing codes?
You can use this method, Hope it will work for you.
- (NSAttributedString*) parseEmoticons:(NSAttributedString*)text {
text = [text stringByReplacingOccurrencesOfString:#":-)" withString:#"😄"];
text = [text stringByReplacingOccurrencesOfString:#";P" withString:#"😜"];
text = [text stringByReplacingOccurrencesOfString:#"B-)" withString:#"😎"];
text = [text stringByReplacingOccurrencesOfString:#";-P" withString:#"😜"];
return text;
}
Having failed to find a more elegant solution, I resorted to maintaining one attributedstring containing the emoticon picture, and one regular string to hold the emoticon codes. So my attString is for instance "Hello [Smiling picture]" while my string is "Hello %101%". If you are interested in building a chatting app as I am, here is the pseudo code:
In emoticon keyboard:
{
Insert picture into attributed string at location loc;
Call textView shouldChangeTextInRange:(loc,0) replacementText:"%101";
}
In the view controller at shouldChangeTextInRange:(loc,length) replacementText:text:
{
Parse regular string to jump over emoticon codes already there to find the location matching loc;
Replace text (for instance %101%) in regular string} at the identified location.
}
Note: shouldChangeTextInRange is also called for regular keyboard entries including delete.
Related
I have a multiline array which populates a tableview.
I now also want to use the array for a UITextView to display the string as it's currently being displayed.
I have it displaying everything as I want except for one of the symbols
in the array which does not display as it should.
I feel like all I need is the right string format?
textView.text = String(format:"%#", array)
The code above is how I have it ALMOST working but the "x" symbol from the array is being substituted with "\U00d7"
The "x" does not appear to be an actual letter, (thus the "\U00d7") not an iOS one anyway.
Before adding the format "%#", the "x" symbol was being displayed as an x.
I want it to be displayed as I have entered it into the array rather than having to always change the "x" symbol for an iOS "X" symbol.
Using String(format:_:) is not a good idea in this case. %# does not work with utf8 encoding.
Try textView.text = array.joined() instead.
I am trying to create a custom keyboard using the app keyboard extension. I am happy with the layout but the output is depended on the UITextField's font.
Is there a way to force a different font (use special characters?) while using the keyboard ?
Thank you
It depends.
Text field (or any other view that draws text) uses 2 informations on how to show some text. One is the sequence of characters called String and the other one is how the string should be represented. The second one is then split it things like fonts, colors, line height, line breaking and wrapping...
So the keyboard alone is not enough to for instance present a certain part of word using different fonts. You need at least a bit of access to the item that represents the text. So if you have no access to your text field then the answer is; No, you can not fore a different font when using different keyboard.
If you do have the access then the answer should lie in NSAttributedString. It is a string you can assign to most items under attributedText. This class wraps your raw string and can add many properties to parts of text you want to change. That includes using a different font.
Another approach would be using HTML tags. Again you will need to process this using for instance NSAttributedString or display it with another element like web view.
I would try it with using NSAttributedString. Hook up to delegate and implement textField(: shouldChangeCharactersIn: replacementString:. The implementation itself may still not be easy though.
I'm trying to replace some text that was selected inside a UITextView with some NSAttributedString but only the following method is available:
textView.replace(UITextRange, withText: String)
As you can see, replacing text only accepts a String and I cannot find way to replace it with an NSAttributedString.
One thing I could do is to store the whole UITextView attributedText and then perform the desired changes on a NSMutableAttributedString and then I can replace the UITextView.attributedText to be the one of the NSMutableAttributedString, but this comes with some issues for me.
If the text is already long with some NSStorage and NSAttachments this will be way more expensive.
Is there any workaround?
Because you can't intermix String and NSAttributedString, there's unfortunately no workaround that will let both co-exist in a text field.
But you should be able to use replaceCharacters(in:with:):
existingAttributedString.replaceCharacters(in: range, with: replacementAttributedString)
I am currently writing my first app on ios using swift 3. I have a plist that has a list of different pieces of information and each is a long paragraph with sections throughout it. for each section, I want to bold the text for the title or at least have the option to format it a certain way rather than just display all the text. I currently have a simple table view that displays text in a text view once tapped. I cannot figure out how to read the paragraph into a string, and compare parts of the string and bold that specific text.
For example, If I had a string that was read from the plist and said:
"My name is #Bob and I like to #dance."
How could I change "#Bob" to "Bob" and "#dance" to "dance" without hard coding it?
#IBOutlet var paragraphTextView: UITextView!
.
.
.
if let text = paragraph["Text"] {
paragraphTextView.text = text
}
The simplest solution is to use some HTML markup in the text in your plist file.
Instead of the plain text:
My name is #Bob and I like to #dance.
Use:
My name is <b>Bob</b> and I like to <b>dance</b>.
This gives you far more flexibility. You can add bold, underline, and italic simply by using the appropriate <b></b>, <u></u>, and <i></i> tags as needed. It also allows you to markup more general ranges than single words.
Once the text has the proper markup, you create an NSAttributedString from the marked up string and then set that to the text viewsattributedTextproperty instead of using thetext` property.
For a good example of how to create an NSAttributedString from HTML text, see HTML Format in UITextView
I have Objective C code that continually updates a set of numerical values. I need to display these values on the screen. That's it! I can convert numerical values into a text string, no problem. But how do I display this string in a UI element? Do I use a text box or a text field or a text view, or a...? I cannot find examples to show how to pass a string from code into the UI. I assume I need to set up a text thingy, and then periodically refresh the contents of that text thingy when the values change?
I assume the answer is simple but is just obscured by a smokescreen of technical jargon.
Thanks!
From the UI perspective you might want something like a
UITextView - multi-line text input
UITextField - single-line text input
UILabel - just text
For your purpose of just printing text, you should use UILabel, since you dont want / need any kind of input. You can access its text using:
// yourLabel is your current UILabel* you want to output yourValue to
yourLabel.text = yourValue;
Of course that yourValue needs to be converted to NSString before.
To actually get hold of the UILabel, you need to connect it from the Interface Builder as an IBOutlet. For tutorials on that topic, take a look at tutorials like Interface Tutorials by Ray Wenderlich or youtube or just google Interface Builder tutorial.