Search code examples
iosswiftsmsmfmessagecomposeviewcontroller

MFMessageComposeViewController show with message body highlighted for editing


We can create and show a text composer like so:

let controller = MFMessageComposeViewController()
controller.body = messageText
controller.recipients = numbers
controller.messageComposeDelegate = self
self.present(controller, animated: true, completion: nil)

Is it possible to present an MFMessageComposeViewController with body highlighted so that the user can just start typing to enter a new message if they do not like the default message we have provided?

I looked through the docs but did not find any options for this.


Solution

  • No. This is not possible.

    The interface for MFMessageComposeViewController does not provide any properties or functions to influence the way it presents the message. Subclassing is no alternative either as the MFMessageComposeViewController doesn't expose any of its view components.

    (Just to be 100% correct: You could of course present the MFMessageComposeViewController, then once it's visible traverse its whole view hierarchy until you find a view of class UITextView whose text or attributedText property equals the body text you provided and then set that text view's selectedRange to the full range of the text. While this will probably work (→ untested) I totally discourage you to do that. Encapsulation is there for a reason and if the MFMessageComposeViewController doesn't provide an interface to select text inside its text view it means that you're not supposed to do that.)