2

How can I recreate the letterpress-like effect applied to the backbarbuttonitem in the notes app in ios 7? I tried the following:

NSShadow *textShadow = [[NSShadow alloc] init];
textShadow.shadowOffset = CGSizeMake(0.0, -1.0);
textShadow.shadowColor = [UIColor blackColor];
NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:@"Back" attributes:@{NSForegroundColorAttributeName : [UIColor orangeColor], NSShadowAttributeName : textShadow}];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:attributedTitle style:UIBarButtonItemStylePlain target:nil action:nil];

But it says I can't use NSAttributedString in place of an NSString.

Wain
  • 118,658
  • 15
  • 128
  • 151
pedroremedios
  • 763
  • 1
  • 11
  • 39

1 Answers1

2

For iOS 5 and above, you can use the UIAppearance functionality to change the Text Color, Font, Tint Color etc of multiple UI Components like UITabBar, UIBarButton, etc.

for UIBarButtonItem, check the below two options.


Option1: For ANY UIBarButtonItem:

NSDictionary *aButtonAttribute = [NSDictionary dictionaryWithObjectsAndKeys:
                                [UIColor darkGrayColor], UITextAttributeTextColor,
                                [UIColor whiteColor], UITextAttributeTextShadowColor,
                                [NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset, nil];
[[UIBarButtonItem appearance] setTitleTextAttributes:aButtonAttribute forState:UIControlStateNormal];

Option 2: For UIBarButtonItem of a UINavigationBar ONLY:

NSDictionary *aButtonAttribute = [NSDictionary dictionaryWithObjectsAndKeys:
                            [UIColor darkGrayColor], UITextAttributeTextColor,
                            [UIColor whiteColor], UITextAttributeTextShadowColor,
                            [NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset, nil];

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:aButtonAttribute forState: UIControlStateNormal];

NOTE: You can add these lines (either of the 2 options) in the .m file of AppDelegate..

Roshit
  • 1,589
  • 1
  • 15
  • 37
  • Didn't work: `NSDictionary *backBarButtonItemTextAttributes = @{[NSValue valueWithUIOffset:UIOffsetMake(0.0, -1.0)] : UITextAttributeTextShadowOffset, UITextAttributeTextShadowColor : [UIColor blackColor]}; [self.navigationItem.backBarButtonItem setTitleTextAttributes:backBarButtonItemTextAttributes forState:UIControlStateNormal];` – pedroremedios Oct 06 '13 at 13:07
  • the Answer clearly says.. u have to access the Appearance object of a UIBarButton.. so instead of `[self.navigationItem.backBarButtonItem setTitleTextAttributes:backBarButtonItemTextAttributes forState:UIControlStateNormal];`, use `[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:aButtonAttribute forState: UIControlStateNormal];` – Roshit Oct 06 '13 at 14:33
  • Still didn't apply the attributes. – pedroremedios Oct 06 '13 at 14:43
  • did u try adding those lines in AppDelegate.? – Roshit Oct 06 '13 at 14:44
  • 1
    Also.. the Key-Value u have in the code is wrong. You have mentioned: `[NSValue valueWithUIOffset:UIOffsetMake(0.0, -1.0)] : UITextAttributeTextShadowOffset` ..... but Key is `UITextAttributeTextShadowOffset` ------- Format is: `@{key: object}` – Roshit Oct 06 '13 at 14:54
  • Ok that worked but the back arrow graphic doesn't have the shadow applied. – pedroremedios Oct 06 '13 at 15:18
  • Shadow of Text or Shadow of the Button Object? – Roshit Oct 06 '13 at 15:21
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/38688/discussion-between-pedroremedios-and-roshit) – pedroremedios Oct 06 '13 at 15:23