objective c - IOS UIButton with AttributedTitle ios -
i have attributed title button in view controller, want button change it's title when clicked example : "add favorite"
(clicked change to) "remove favorite"
.
- (ibaction)addtofav:(id)sender { nsmutableattributedstring *string,*string2; string = [[nsmutableattributedstring alloc] initwithstring:@"add 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 favorite"]; } else { nsattributedstring *attributedtitle = [_btnfav attributedtitleforstate:uicontrolstatenormal]; nsmutableattributedstring *mas = [[nsmutableattributedstring alloc] initwithattributedstring:attributedtitle]; [mas.mutablestring setstring:@"remove favorite"]; } }
- (ibaction)addtofav:(id)sender{ nsstring *string = @"add favorite"; nsstring *string2 = @"remove favorite"; nsstring *newtitle = nil; nsattributedstring *attributedtitle = [_btnfav attributedtitleforstate:uicontrolstatenormal]; //check title if([[attributedtitle string] isequaltostring:string]) { newtitle = string2; } else { newtitle = string; } //set title button nsmutableattributedstring *mas = [[nsmutableattributedstring alloc] initwithstring:newtitle]; [_btnfav setattributedtitle:mas forstate:uicontrolstatenormal]; }
Comments
Post a Comment