gpt4 book ai didi

带有 AttributedTitle 的 IOS UIButton

转载 作者:行者123 更新时间:2023-11-28 21:43:33 25 4
gpt4 key购买 nike

我在 View Controller 中有一个属性标题按钮,我希望我的按钮在我单击它时更改它的标题示例:“添加到收藏夹”(单击更改为)“删除收藏夹”

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

最佳答案

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

关于带有 AttributedTitle 的 IOS UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31175571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com