Backbarbuttonitem letterpress effect

≯℡__Kan透↙ 提交于 2019-12-11 03:56:51

问题


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:


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



来源:https://stackoverflow.com/questions/19208327/backbarbuttonitem-letterpress-effect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!