shasa po
shasa po

Reputation: 41

IOS UIButton with AttributedTitle ios

I have an attributed title button in a view controller, I want my button change it's title when i clicked it example : "Add To Favorite" (clicked change to) "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"];
  }
}

Upvotes: 0

Views: 706

Answers (4)

vadian
vadian

Reputation: 285250

To toggle the title use something like this

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

Upvotes: 0

Sunny Shah
Sunny Shah

Reputation: 13020

Simple Example

Set different title string for button one for the normal and for the selected

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


}

ON button tap just change button style

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

Upvotes: 0

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 40018

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

Upvotes: 2

user3432164
user3432164

Reputation:

Instead of creating NSAttributedString create NSMutableAttributedString then you can just set the string like this.

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

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

[_ ButtonName setAttributedTitle:string forState:UIControlStateNormal];

Upvotes: 0

Related Questions