我在视图控制器中有一个属性标题按钮,我希望我的按钮在我点击它时更改它的标题
示例:"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"];
}
}
答案 0 :(得分:2)
- (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];
}
答案 1 :(得分:0)
您可以像这样设置字符串,而不是创建NSAttributedString创建NSMutableAttributedString。
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithAttributedString:[_ ButtonName attributedTitleForState:UIControlStateNormal]];
[string replaceCharactersInRange:NSMakeRange(0, string.length) string:@"Your String"];
[_ ButtonName setAttributedTitle:string forState:UIControlStateNormal];
答案 2 :(得分:0)
简单示例
为normal
和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按钮点击只需更改按钮样式
-(void)btnClick:(UIButton *)sender{
sender.selected =! sender.selected;
}
答案 3 :(得分:0)
要切换标题,请使用类似
的内容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];