我试图更改UIBarButtonItem
字体。 ViewControllers加载时效果很好。但是,如果我点击条形按钮,或向右滑动就像移动到上一个ViewController(但然后回到当前的ViewController),字体会变回系统字体。这是我在AppDelegate中设置的内容:
NSDictionary* barButtonItemAttributes = @{NSFontAttributeName: [UIFont fontWithName:@"SourceSansPro-Light" size:20.0f]};
[[UIBarButtonItem appearance] setTitleTextAttributes: barButtonItemAttributes forState:UIControlStateNormal];
[[UIBarButtonItem appearance] setTitleTextAttributes: barButtonItemAttributes forState:UIControlStateHighlighted];
[[UIBarButtonItem appearance] setTitleTextAttributes: barButtonItemAttributes forState:UIControlStateSelected];
[[UIBarButtonItem appearance] setTitleTextAttributes: barButtonItemAttributes forState:UIControlStateDisabled];
这是我的观点的一个例子WillAppear:
- (void) viewWillAppear:(BOOL)animated {
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(doneButtonPressed)];
self.navigationItem.rightBarButtonItem.tintColor = [UIColor colorWithRed:141.0/255.0 green:209.0/255.0 blue:205.0/255.0 alpha:1.0];
}
我是以某种方式更改字体,还是我滥用外观代理?
答案 0 :(得分:15)
问题是tintColor
设置与标题文本属性冲突。注释掉那条线,一切都会很好。
如果您要使用标题文本属性,请将文本颜色合并到这些属性中:
NSDictionary* barButtonItemAttributes =
@{NSFontAttributeName:
[UIFont fontWithName:@"Georgia" size:20.0f],
NSForegroundColorAttributeName:
[UIColor colorWithRed:141.0/255.0 green:209.0/255.0 blue:205.0/255.0 alpha:1.0]
};
答案 1 :(得分:3)
通过在viewDidLoad方法中使用此方法,您可以一次性设置自定义文本,字体,大小等。
UIView *customBarButtonView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
UIButton *buttonDone = [UIButton buttonWithType:UIButtonTypeSystem];
[buttonDone addTarget:self action:@selector(doneButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
buttonDone.frame = CGRectMake(10, 0, 50, 30);
buttonDone.titleLabel.textColor = [UIColor colorWithRed:141.0/255.0 green:209.0/255.0 blue:205.0/255.0 alpha:1.0];
buttonDone.titleLabel.font = [UIFont fontWithName:@"SourceSansPro-Light" size:20.0f];
[buttonDone setTitle:@"Done" forState:UIControlStateNormal];
[customBarButtonView addSubview:buttonDone];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:customBarButtonView];
答案 2 :(得分:2)
_myrButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"French KeyBoard" style:UIBarButtonItemStylePlain target:self action:@selector(RbuttonItemdown:)];
NSDictionary* itemTextAttributes = @{NSFontAttributeName:[UIFont fontWithName:@"Georgia" size:12.0f], NSForegroundColorAttributeName:[UIColor blueColor], NSBackgroundColorAttributeName:[UIColor lightGrayColor]};
[_myrButtonItem setTitleTextAttributes:itemTextAttributes forState:UIControlStateNormal];
[self.navigationItem setRightBarButtonItem:_myrButtonItem];
您可以使用以下所有内容:
NSString *const NSFontAttributeName ;
NSString *const NSParagraphStyleAttributeName ;
NSString *const NSForegroundColorAttributeName ;
NSString *const NSBackgroundColorAttributeName ;
NSString *const NSLigatureAttributeName ;
NSString *const NSKernAttributeName ;
NSString *const NSStrikethroughStyleAttributeName ;
NSString *const NSUnderlineStyleAttributeName ;
NSString *const NSStrokeColorAttributeName ;
NSString *const NSStrokeWidthAttributeName ;
NSString *const NSShadowAttributeName ;
NSString *const NSTextEffectAttributeName ;
NSString *const NSAttachmentAttributeName ;
NSString *const NSLinkAttributeName ;
NSString *const NSBaselineOffsetAttributeName ;
NSString *const NSUnderlineColorAttributeName ;
NSString *const NSStrikethroughColorAttributeName ;
NSString *const NSObliquenessAttributeName ;
NSString *const NSExpansionAttributeName ;
NSString *const NSWritingDirectionAttributeName ;
NSString *const NSVerticalGlyphFormAttributeName;