How to Insert NSAttributedString using custom keyboard extension? - ios

I want to write text in some custom fonts using keyboard Extension as these apps (1,2,3,4) are doing. I know how we can insert normal string in document proxy.
[self.textDocumentProxy insertText:mystring];
I have tried to insert NSAttributedString using above approach but I can't see any way to insert NSAttributedString to document proxy.
Some one can guide what will the best way to get rid of this issue? any suggestion will be appreciated.

It is not possible to insert attributed strings (or otherwise rich content) using the text document proxy.
The keyboards you have linked are not using custom fonts. They use (or abuse) Unicode symbols such as Enclosed Alphanumerics and Enclosed Alphanumeric Supplement.
In other instances, different alphabet symbols with visual similarity to latin symbols are used to create "funky" text, like here.
Last, some keyboard extensions, like the image keyboards, use the pasteboard to copy the rich content, and the user is responsible to paste it where he seems fit.

The apps you are referring to don't use NSAttributedString or custom fonts. They simply replace letters with similar-looking Unicode characters. You can see these characters in any OS X app inside Edit -> Special Characters menu.

Related

Escaping or replacing curly/smart quotes in Twitter widget: Tweet text intent

I'm using selected/highlighted text by the user to generate the intent/tweet?text= content for the Twitter widget's "Tweet text" function.
It works great except when it encounters either opening or closing double/single curly quotes within the element's content: ‘ ’ “ ” (‘ ’ “ ”). When any of those characters is included in the selected text, the entire Tweet text dialog window is blank.
I've tried various javascript methods to search/replace the ASCII codes or the Unicode versions of those but to no avail.
The escape(text) method is already being used before the text gets to the Twitter widget, so I'm confused as to why it's choking. In the location bar it shows that these characters are being converted to their Unicode versions like u201C.
What could be causing the Tweet text box to fail on these characters even though it seems to be properly converting them anyway?
I discovered that one needs to search/replace the literal entities themselves, not ASCII codes nor their Unicode counterparts.
So the solution for using selected text in a Twitter widget text intent is:
text = text.replace(/“/g, "\'").replace(/”/g, "\'").replace(/‘/g, "\'").replace(/’/g, "\'");
The entities don't need to be escaped.

Is there a way to convert text stored in a textview text storage as HTML characters?

For example, I have a mini RTF editor that consist of a textview and I change the sizes of text in the text storage. Is there a way I can get these values as HTML? Or would I have to parse it manually?
There is no one to one conversion from the RTF spec to the HTML spec. You will either need to parse/convert yourself, or use a third party HTML - RTF converter.
Since your ultimate goal is to convert the RTF content to PDF, you might like to consider an RTF to PDF Converter.

Removing HTML url tags in iOS

I am writing an iOS app that downloads some data from a server that's not under my control. I am not using custom data detectors. The strings in the returned JSON still contain their HTML url tags, and I want to remove them because I want to display the strings in a UITextView, and these kind of strings
<strong>Instagram</strong> / <strong>Behance</strong>
<strong>Live Now</strong>
What I really want is this:
Instagram Behance
Live Now
What is the best way to go about this?
Should I strip the url tags from the text using regex?
Would I lose the link "descriptions" (in the above example, "Instagram" and "Behance") when I do that?
Would this be way easier using a UIWebView?
If this would be too hard/impossible, it'd be okay to only have the urls, without their descriptions.
Thank you!
Should I strip the url tags from the text using regex?
No. HTML is too complex to be properly parsed using a RegEx. You'll need an XML parser.
Would I lose the link "descriptions" (in the above example, "Instagram" and "Behance") when I do that?
You wouldn't have to using an XML parser. Using a RegEx, you might, especially if you can't control exactly what's returned.
Would this be way easier using a UIWebView?
Yep. That's what I would do, unless you have a good reason not to.

RTF file to TXT/CSV file in objective-c?

I have RTF files containing that sort of content:
long_text_description_1 number1a number1b number1c
long_text_description_2 number2a number2b number2c
long_text_description_3 number3c
long_text_description_4 number4a number4b number4c
…
I need to extract the plain raw text without the colours, fonts and other formatting thing.
The only thing I need to keep are the most basic row/column information, ideally I would like a CSV file.
The file I get contain all the formatting:
{\cs18\lang1033\langfe1033\f0\b\i0\ul0\strike0\scaps0\fs15\afs15\charscalex100\expndtw0\cf1\dn0 number1a}
What is the best way to remove all rtf information while only keeping the row information?
Trying to figure out myself many many regular expressions sound dangerous unless there is a complete understanding of the RTF format.
What I could find on the Internet mostly focused on using Windows languages & libraries unavailable in iOS.
All rtf tags are in the form \xxx.
Try using a regular expression like "\\S+" and remove all matches or replace with nothing.
For your example, you'll end up with { number1a} This will remove any backslash followed by any characters.

Formatting NSString for superscript and subscript

I am writing a utility app for some coworkers. The app is essentially a custom notepad, with buttons that represent the shorthand they use to transcribe a task. All of the buttons add a string to arrays that I have set up to hold the transcript, and I add the strings to the row arrays like this.
[currentRow addObject:#"("];
Some of the shorthand needs to be written in subscript, and some in superscript. There are not Unicode characters for all of the characters that I need, so I have been trying to sort through the code around Attributed Strings,but I'm not quite getting it. Does anyone have advice on this or some sample code?
Also, after this transcript is printed to the screen during transcription, I send it to an email message body.. so I assume I'll need to worry about formatting there as well. I am currently using plain text, but the email could be HTML. Thanks!
If you display the text in a WebView you can use html tags to set superscript. It also has the advantage to run on older iOS versions and you can reuse the text in your mail.
NSString *myText=#"This text contains <sub>subscript</sub> and <sup>superscript</sup> text.";
[self.myWebView loadHTMLString:myText baseURL:nil];

Resources