8

UIButton の Title Color をデフォルト値に戻す方法を知りたいです。

このように、何かがオンになっていることを示すためにタイトルの色を変更するボタンがあります。

[cell.offStateSwitch setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

次に、オフになったら、デフォルトの色に戻したいと思います。

UIButton のデフォルトのシステム カラーを取得する方法がわかりません。多くの人が特定の RGB 値を強制することでこれを行っていることがわかりましたが、これは iOS の異なるバージョン間で必ずしも正しいとは限りません。

4

5 に答える 5

9

テキストをtintColorに戻したいという意味だと思います。現在の tintColor だけが必要な場合は、次のようにします。

[button setTitleColor:button.tintColor forState:UIControlStateNormal];

ただし、tintColor は、アラートがポップアップしたときなど、特定の状況で自動的に変更されるはずです。それを実現するには、独自のボタンを定義する必要があると思います。

@interface MyButton : UIButton
@end

@implementation MyButton

- (void) tintColorDidChange
{
    [self setTitleColor:self.tintColor forState:UIControlStateNormal];
    [super tintColorDidChange];
}

@end

その場合、選択した状態を特別な色として使用することをお勧めします。

[button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];

button.selected = YESあなたが言うように、「何かがオンになっている」ときに設定します。

于 2013-11-01T13:56:46.390 に答える
1

iOS にはデフォルトのタイトル カラーのプロパティはありません。簡単に取得できます。

defaultColor を定義するだけです:

UIColor* defaultColor; 次に、viewDidLoadプットまたはビューが初期化される場所で:

defaultColor = [button titleColorForState: UIControlStateNormal];

次に、デフォルトのタイトルの色としてdefaultColorがあります。

于 2013-09-23T05:32:09.193 に答える
0

次のコードを cellForRowAtIndexPath デリゲートに追加します......

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

cell.button.tag=indexPath.row+100;

[cell.button addTarget:self selector:@selector(changeButtonTitle:) forControlEvents:UIcontrolTouchUpEventInside];

次に、ボタンアクションで次のコードを追加します

-(void)changeButtonTitle:(UIButton *)button
{
    UITableViewCell *cell=(UITableViewCell *)button.superview.superview;
    NSIndexpath *indexpath=[self.yourtableview indexPathForCell:cell];
    UIButton *tempButton=(UIButton *)[cell viewWithtag:indexPath.row+100];
    [tempButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
于 2013-09-23T05:42:44.963 に答える