iOS 7 UIBarButtonItem font changes when tapped

若如初见. 提交于 2019-12-05 03:44:02

The problem is that the tintColor setting conflicts with the title text attributes. Comment out that line and all will be well.

If you are going to use title text attributes, then incorporate the text color into those attributes:

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

By using this approach in viewDidLoad method you can set custom text,font,size etc all at once place.

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

You can use all of this :

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