【问题标题】:Backbarbuttonitem letterpress effectBackbarbuttonitem 凸版效果
【发布时间】:2013-10-06 11:22:39
【问题描述】:

如何在 iOS 7 的笔记应用程序中重新创建应用于 backbarbuttonitem 的凸版效果?我尝试了以下方法:

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];

但它说我不能使用 NSAttributedString 代替 NSString。

【问题讨论】:

    标签: iphone ipad ios7 letterpress


    【解决方案1】:

    对于 iOS 5 及更高版本,您可以使用 UIAppearance 功能更改多个 UI 组件(如 UITabBar、UIBarButton 等)的文本颜色、字体、色调颜色等。

    对于 UIBarButtonItem,请检查以下两个选项。


    选项 1:对于任何 UIBarButtonItem:

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

    选项 2:仅适用于 UINavigationBar 的 UIBarButtonItem:

    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];
    

    注意:您可以在 AppDelegate 的 .m 文件中添加这些行(两个选项之一)。

    【讨论】:

    • 无效:NSDictionary *backBarButtonItemTextAttributes = @{[NSValue valueWithUIOffset:UIOffsetMake(0.0, -1.0)] : UITextAttributeTextShadowOffset, UITextAttributeTextShadowColor : [UIColor blackColor]}; [self.navigationItem.backBarButtonItem setTitleTextAttributes:backBarButtonItemTextAttributes forState:UIControlStateNormal];
    • 答案清楚地表明.. 你必须访问 UIBarButton 的 Appearance 对象.. 所以不要使用[self.navigationItem.backBarButtonItem setTitleTextAttributes:backBarButtonItemTextAttributes forState:UIControlStateNormal];,而是使用[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:aButtonAttribute forState: UIControlStateNormal];
    • 仍然没有应用属性。
    • 您尝试在 AppDelegate 中添加这些行吗?
    • 另外.. 你在代码中的键值是错误的。您提到过:[NSValue valueWithUIOffset:UIOffsetMake(0.0, -1.0)] : UITextAttributeTextShadowOffset ..... 但是 Key 是 UITextAttributeTextShadowOffset ------- 格式是:@{key: object}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-24
    相关资源
    最近更新 更多