0

I am working on a chat application. When I copy text from anywhere and paste it into my text field, I want the text to be stripped from whatever font, size, color, etc. So it's in a default/plain-text format. Then I want to set my own, text style before sending the text.

Tried using NSPlainTextDocumentType but that didn't work.

UIFont *font = [UIFont fontWithName:@"GothamRounded-Bold" size:14];
text = [self.messagesTextView.text setFont:font];
text = NSPlainTextDocumentType;

Expect the text to be converted to plain-text format and then have the GothamRounded-Bold size:14 style added to it.

Nirav Kotecha
  • 2,493
  • 1
  • 12
  • 26
  • Can you provide a little more information? Is your text field set to Attributed Text or Plain Text? Are you trying to paste programmatically, or using the default Paste from the popup menu bar? – DonMag Apr 04 '19 at 12:17
  • It's set to Plain Text. I'm trying to paste using the default popup menu bar. So for e.g lets say I find a paragraph of text on google, I'll copy that to the phone's clipboard, go to my chat app, paste the text in the text field. But when I paste it, the font might be cursive with a size of 13px. I want to remove that styling so it has the same font and size as the rest of my chat app, so when I click the send button, the text is uniform and looks constant. – TheRealAnt Apr 04 '19 at 12:58
  • I found a similar question, but it's more specified for HTML, might help in someway... https://stackoverflow.com/questions/19226634/converting-html-text-into-plain-text-using-objective-c – TheRealAnt Apr 04 '19 at 13:26

1 Answers1

0

I wonder if you're running into this problem...

By default, pasting formatted / rich text into a text field will not keep the formatting. It will be pasted as plain-text, and will be displayed with the font you've set for your field.

Try a test. Copy and paste this next line into your text field:

This should lose formatting.

Then try copy/paste with this line:

should formatting.

The first test line uses html tags. If you inspect the source, it should look like this:

<strong>This</strong> should <strong><em>lose</em></strong> formatting.

The second test line, however, uses unicode characters, and if you inspect the source it will look just like it looks here in this answer.

If that is what you're seeing, there is no (easy) way to "remove" the formatting, because there is no formatting.

It's much the same as if the user pastes an emoji, such as - - into your text field.

DonMag
  • 69,424
  • 5
  • 50
  • 86
  • should formatting. - Is exactly what I was trying to make plain-text. If I had to change the unicode characters, would that prevent emoji's from being displayed? So basically only text and nothing else would be displayed in the field, even if a user pasted an emoji, that would be converted to text only? – TheRealAnt Apr 04 '19 at 15:46
  • I think you'd have to jump through some pretty big hoops (or, small hoops, because they're more difficult to get through). There are a bunch of full Latin alphabet unicode chars starting at U+1D400. So, how would you decide to convert U+1D400 - - to an "A" but *not* convert U+1F600 - - because it's a smiley face? Maybe with a bunch of tables, or Unicode range lookups, but... tough task. – DonMag Apr 04 '19 at 16:06
  • Haha, I see what you're saying..That's a pretty big job, doubt I'll need to go that far for my needs, although it's nice to know it could be done (with a lot of time). Thanks a lot, you've really helped me! – TheRealAnt Apr 04 '19 at 16:25