首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在UIBarItem中使用setTitleTextAttributes:forState?

如何在UIBarItem中使用setTitleTextAttributes:forState?
EN

Stack Overflow用户
提问于 2011-10-18 16:26:25
回答 5查看 56.3K关注 0票数 37

如何在setTitleTextAttributes:forState:中使用UIBarItem中的iOS

如何设置NSDictionary?不能让它工作,而文档对此并不十分清楚。

来自文档的

代码语言:javascript
复制
setTitleTextAttributes:forState:

为给定的控件状态设置标题的文本属性:

代码语言:javascript
复制
- (void)setTitleTextAttributes:(NSDictionary *)attributes 
                      forState:(UIControlState)state

参数:

属性:包含文本属性的键值对的字典.可以使用NSString UIKit添加引用中列出的键指定字体、文本颜色、文本阴影颜色和文本阴影偏移量。

state:要为其设置标题文本属性的控件状态。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2011-10-25 13:07:44

示例代码:

代码语言:javascript
复制
[[UIBarItem appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:220.0/255.0 green:104.0/255.0 blue:1.0/255.0 alpha:1.0], UITextAttributeTextColor, 
[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0], UITextAttributeTextShadowColor, 
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset, 
[UIFont fontWithName:@"AmericanTypewriter" size:0.0], UITextAttributeFont, nil] 
forState:UIControlStateNormal];
票数 103
EN

Stack Overflow用户

发布于 2014-08-19 12:04:24

SWIFT5.0:

代码语言:javascript
复制
// Bar title text color
let shadow = NSShadow()
shadow.shadowColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
shadow.shadowOffset = CGSize(width: 0, height: 1)
let color : UIColor = UIColor(red: 220.0/255.0, green: 104.0/255.0, blue: 1.0/255.0, alpha: 1.0)
let titleFont : UIFont = UIFont(name: "AmericanTypewriter", size: 16.0)!

let attributes = [
    NSAttributedString.Key.foregroundColor : color,
    NSAttributedString.Key.shadow : shadow,
    NSAttributedString.Key.font : titleFont
]
self.navigationItem.rightBarButtonItem?.setTitleTextAttributes(attributes, for: .normal)

// Or you can use
UIBarItem.appearance().setTitleTextAttributes(attributes, for: .normal)

SWIFT4.0:

代码语言:javascript
复制
// Bar title text color
let shadow = NSShadow()
shadow.shadowColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
shadow.shadowOffset = CGSize(width: 0, height: 1)
let color : UIColor = UIColor(red: 220.0/255.0, green: 104.0/255.0, blue: 1.0/255.0, alpha: 1.0)
let titleFont : UIFont = UIFont(name: "AmericanTypewriter", size: 16.0)!

let attributes = [
        NSAttributedStringKey.foregroundColor : color,
        NSAttributedStringKey.shadow : shadow,
        NSAttributedStringKey.font : titleFont
    ]

self.navigationItem.rightBarButtonItem?.setTitleTextAttributes(attributes, for: UIControlState.normal)
// Or you can use
UIBarItem.appearance().setTitleTextAttributes(attributes, for: UIControlState.normal)

目标C代码:

代码语言:javascript
复制
NSShadow *shadow = [NSShadow new];
[shadow setShadowColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0]];
[shadow setShadowOffset:CGSizeMake(0, 1)];

NSDictionary *attributes = @{
                                NSForegroundColorAttributeName: [UIColor colorWithRed:220.0/255.0 green:104.0/255.0 blue:1.0/255.0 alpha:1.0],
                                NSShadowAttributeName: shadow,
                                NSFontAttributeName: [UIFont fontWithName:@"AmericanTypewriter" size:16.0]
                             };

[self.navigationItem.rightBarButtonItem setTitleTextAttributes:attributes forState: UIControlStateNormal];

// Or you can use.

[[UIBarItem appearance] setTitleTextAttributes:attributes forState: UIControlStateNormal];
票数 43
EN

Stack Overflow用户

发布于 2013-10-01 03:42:08

下面是phix23 23的代码,只是更新了语法,我认为语法更简洁:

代码语言:javascript
复制
[[UIBarItem appearance] setTitleTextAttributes:@{
                      UITextAttributeTextColor: [UIColor colorWithRed:220.0/255.0 green:104.0/255.0 blue:1.0/255.0 alpha:1.0],
                UITextAttributeTextShadowColor: [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0],
               UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0, 1)],
                           UITextAttributeFont: [UIFont fontWithName:@"AmericanTypewriter" size:0.0]}
                                      forState: UIControlStateNormal];
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7810563

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档