Ios 如何在UIBarItem中使用setTitleTextAttributes:forState?
如何在Ios 如何在UIBarItem中使用setTitleTextAttributes:forState?,ios,objective-c,swift,uinavigationcontroller,uibaritem,Ios,Objective C,Swift,Uinavigationcontroller,Uibaritem,如何在iOS的UIBarItem中使用setTitleTextAttributes:forState: 如何设置NSDictionary?无法使其工作,文档对此也不是很清楚 来自文档: setTitleTextAttributes:forState: - (void)setTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state // Bar tit
iOS
的UIBarItem
中使用setTitleTextAttributes:forState:
如何设置NSDictionary
?无法使其工作,文档对此也不是很清楚
来自文档:
setTitleTextAttributes:forState:
- (void)setTitleTextAttributes:(NSDictionary *)attributes
forState:(UIControlState)state
// 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)
// 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)
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];
为给定控件状态设置标题的文本属性:
setTitleTextAttributes:forState:
- (void)setTitleTextAttributes:(NSDictionary *)attributes
forState:(UIControlState)state
// 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)
// 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)
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];
参数:
属性:包含文本属性键值对的字典。可以使用NSString UIKit Additions参考中列出的键指定字体、文字颜色、文字阴影颜色和文字阴影偏移量
状态:要为其设置标题文本属性的控件状态。示例代码:
[[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];
以下是phix23的代码,只是更新了语法,我认为更简洁:
[[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];
Swift 5.0:
setTitleTextAttributes:forState:
- (void)setTitleTextAttributes:(NSDictionary *)attributes
forState:(UIControlState)state
// 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)
// 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)
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];
Swift 4.0:
setTitleTextAttributes:forState:
- (void)setTitleTextAttributes:(NSDictionary *)attributes
forState:(UIControlState)state
// 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)
// 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)
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];
目标C代码:
setTitleTextAttributes:forState:
- (void)setTitleTextAttributes:(NSDictionary *)attributes
forState:(UIControlState)state
// 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)
// 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)
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];
Swift5设置
UIBarItem
标题颜色
let attributes = [
NSAttributedString.Key.foregroundColor : UIColor.orange,
NSAttributedString.Key.font : UIFont.systemFont(ofSize: 20)
]
vc.tabBarItem.setTitleTextAttributes(attributes, for: .normal)
没有值WithUIOffset方法。我的程序在代码中的该点崩溃。请参见UITextAttribute在iOS 7开始时已被弃用,您应该使用NSAttributedString类的值,例如,您应该使用NSForegroundColorAttributeNameSwift 4而不是UITextAttributeString,实际上如下所示:
[NSAttributedStringKey.foregroundColor:color]
您应该更新代码。