Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIActivityViewController - how to change the cancel button text

Does anybody know how to change the standard cancel button text value of UIActivityViewController? By default it's "Cancel". But I want to change it e.g. to be in different language.

How to do it? Thanks!

like image 599
Sid Avatar asked Mar 08 '13 12:03

Sid


3 Answers

Update: The code below only worked on iOS7.

Like Rikkles said, you don't need to customise the text to make it localized. But just in case someone ever has a good reason to do this, here's the code:

NSArray *activityItems = @[@"Test"];

UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];

NSAttributedString *as = [[NSAttributedString alloc] initWithString:@"Custom Text" attributes:@{NSForegroundColorAttributeName:[UIColor greenColor]}];
[[UIButton appearanceWhenContainedIn:[UIActivityViewController class], nil] setAttributedTitle:as forState:UIControlStateNormal];

[self presentViewController:activityController animated:YES completion:nil];

enter image description here

like image 94
Breno Gazzola Avatar answered Oct 02 '22 06:10

Breno Gazzola


You can't. However, the Cancel button is localized by default. Incidentally, you can create your own controller if that's what you want. And you can fill it with UIActivity objects. Note that when you look at that class, when the docs talk about an activityController, it's any UIViewController.

like image 37
Rikkles Avatar answered Oct 02 '22 06:10

Rikkles


As others point out, the button is localized by the system, you can't change it. At least on iOS 11. But I was struggling with that as well and the button kept its Cancel title even after changing the system language.

This post helped me to successfully localize the Cancel button of the share sheet and worked on a UIDatePicker in countdown timer mode that resisted previous system localization attempts as well: Why UIActivityViewController doesn't display localized cancel string?

Add this row into your info.plist:

enter image description here

like image 23
Martin Koles Avatar answered Oct 02 '22 07:10

Martin Koles