IOS 带有 AttributedTitle 的 UIButton ios

IOS UIButton with AttributedTitle ios

我在视图控制器中有一个属性标题按钮,我希望我的按钮在我单击它时更改它的标题 示例:"Add To Favorite"(单击更改为)"Remove Favorite"

- (IBAction)addToFav:(id)sender {
  NSMutableAttributedString *string,*string2;
  string = [[NSMutableAttributedString alloc] initWithString:@"Add To Favorite"];
  string2 = [[NSMutableAttributedString alloc] initWithString:@"Remove Favorite"];
  if([_btnFav.currentAttributedTitle isEqualToAttributedString:string]){
    NSAttributedString *attributedTitle = [_btnFav attributedTitleForState:UIControlStateNormal];
    NSMutableAttributedString *mas = [[NSMutableAttributedString alloc] initWithAttributedString:attributedTitle];
    [mas.mutableString setString:@"Add To Favorite"];
  } else {
    NSAttributedString *attributedTitle = [_btnFav attributedTitleForState:UIControlStateNormal];
    NSMutableAttributedString *mas = [[NSMutableAttributedString alloc] initWithAttributedString:attributedTitle];
    [mas.mutableString setString:@"Remove Favorite"];
  }
}

而不是创建 NSAttributedString 创建 NSMutableAttributedString 然后你可以像这样设置字符串。

 NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithAttributedString:[_ ButtonName attributedTitleForState:UIControlStateNormal]];

[string replaceCharactersInRange:NSMakeRange(0, string.length) string:@"Your String"];

[_ ButtonName setAttributedTitle:string forState:UIControlStateNormal];
- (IBAction)addToFav:(id)sender{
  NSString *string = @"Add To Favorite";
  NSString *string2 = @"Remove Favorite";
  NSString *newTitle = nil;

  NSAttributedString *attributedTitle = [_btnFav attributedTitleForState:UIControlStateNormal];
  //check the title
  if([[attributedTitle string] isEqualToString:string]) {
    newTitle = string2;
  }
  else {
    newTitle = string;
  }
  //set the title to the button
  NSMutableAttributedString *mas = [[NSMutableAttributedString alloc] initWithString:newTitle];
  [_btnFav setAttributedTitle:mas forState:UIControlStateNormal];
}

简单示例

normalselected

的第一个按钮设置不同的标题字符串
  -(void)createButton{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setFrame:CGRectMake(0, 150, 150, 35)];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];

    NSMutableAttributedString * attributeNormal= [[NSMutableAttributedString alloc] initWithString:@"Remove Favorite"];
    [btn.titleLabel setAttributedText:attributeNormal];
    [btn setAttributedTitle:attributeNormal forState:UIControlStateNormal];

    NSMutableAttributedString * attributedSelected= [[NSMutableAttributedString alloc] initWithString:@"Add To Favorite"];
    [btn.titleLabel setAttributedText:attributedSelected];
    [btn setAttributedTitle:attributedSelected forState:UIControlStateSelected];


}

点击按钮只需更改按钮样式

-(void)btnClick:(UIButton *)sender{
    sender.selected =! sender.selected;
}

要切换标题,请使用类似这样的东西

NSString *title;
NSDictionary *attributes; // a dictionary containing the attributes

if ([_btnFav.currentTitle isEqualToString:@"Add To Favorite"]){
    title = @"Remove Favorite";
} else {
    title = @"Add To Favorite"";
}
NSAttributedString *attributedTitle =  [[NSAttributedString alloc] initWithString:title attributes:attributes];
[_btnFav setAttributedTitle:attributedTitle  forState:UIControlStateNormal];