Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8698379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:43:06+00:00 2026-06-13T01:43:06+00:00

How can I programmatically copy formatted text (e.g. italic ) from a UITextView, so

  • 0

How can I programmatically copy formatted text (e.g. italic) from a UITextView, so that when pasted into another program (e.g. Mail) the formatting will be retained? I’m assuming the correct approach is to copy an NSAttributedString to the pasteboard. Right now, I am able to copy a regular string to the pasteboard via the following:

NSString *noteTitleAndBody = [NSString stringWithFormat:@"%@\n%@", [document title], [document body]];
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = noteTitleAndBody;

I noticed that if I select and copy text from my UITextView with the standard text selection Copy menu item, it works as desired. But I need to access this via a button I’ve created.

I thought perhaps I could just call the UIResponderStandardEditActions Copy method. Using the following, the paste into Mail did retain the formatting, but my app also crashed as a result.

NSMutableAttributedString *noteTitleAndBody = [[NSMutableAttributedString alloc] initWithString:[document title]];
[noteTitleAndBody appendAttributedString:[document body]];
[noteTitleAndBody copy:nil];

Examples of the correct way to do this would be greatly appreciated.

PS – I am aware that there are existing threads related to NSAttributedString and the pasteboard, but they seem to either be for Cocoa, don’t reference the UIResponderStandardEditActions Copy method, or predate iOS 6 where many of the UITextView attributes became available.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:43:07+00:00Added an answer on June 13, 2026 at 1:43 am

    The following will work for whatever textView is currently first responder:

    [UIApplication sharedApplication] sendAction:@selector(copy:) to:nil from:self forEvent:nil];
    

    Since my copy button is only accessible when my textView had resigned firstResponder, I had to do a little more:

    [self.noteTextView select:self];
    self.noteTextView.selectedRange = NSMakeRange(0, [self.noteTextView.text length]);
    [[UIApplication sharedApplication] sendAction:@selector(copy:) to:nil from:self forEvent:nil];
    [self.noteTextView resignFirstResponder];
    

    Thanks to the following posts which pointed me in the right direction:

    Perform copy/cut from UIResponderStandardEditActions

    can I call a select method from UIResponderStandardEditActions?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering how can I programmatically copy all the discussion items from one
How can I programmatically determine sites of a SharePoint 2007 installation that use custom
How can I programmatically get the phone number of the device that is running
If I know that a particular Text column exists in a table, how can
I know that the UIMenuController can show a popover containing Cut, Copy, Paste, Select/All,
One of our customers has a problem that we cannot reproduce. We programmatically copy
I want to copy the whole output of a console application programmatically into clipboard
We are trying to copy a BO Query Service from one Universe to another.
Is there a way to merge two exe files into one, programmatically, so that
Possible Duplicate: How to programmatically copy/paste in windows phone? how can i use clipboard

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.