Backbarbuttonitem letterpress effect

542 Views Asked by At

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.

1

There are 1 best solutions below

8
On

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..