【问题标题】:Change Existing Set UIColors on UIButton Press在 UIButton Press 上更改现有的 Set UIColors
【发布时间】:2018-10-04 09:38:55
【问题描述】:

目标是在单击 UIButton 时更改我的 UITextView 的字体颜色和背景颜色,然后在再次单击 UIButton 时将它们恢复为原始颜色;然后重复,等等。

默认颜色设置

  • 文字:blackColor
  • 后台:lightGrayColor

单击按钮时(更改为..)

  • 文字:greenColor
  • 后台:blackColor

(如果再次点击,则改回默认颜色设置)

到目前为止,我有这个:

- (IBAction) enableSleepMode:(id)sender {

    [txtNotes setTextColor:[UIColor greenColor]];
    [txtNotes setBackgroundColor:[UIColor blackColor]];

}

我很抱歉,但我不确定从这里去哪里。提前致谢。

【问题讨论】:

  • 究竟是什么不工作?该代码似乎可以将其设置为绿色/黑色?你可以做一个if (txtNotes.textColor == [UIColor greenColor]) 来确定它的状态并来回交换......

标签: ios iphone uibutton uitextview


【解决方案1】:

你说要改变'UITextView'的字体颜色和背景颜色而不是'UIButton'对吧? .那你加UItextViewDelegate了吗?

我已经这样做了

编辑:

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    [txtView setBackgroundColor:[UIColor lightGrayColor]];
    [txtView setTextColor:[UIColor blackColor]];
    str = @"first";
    }

   -(IBAction)bt:(id)sender
{
    if([str isEqualToString:@"first"])
    {
        [txtView setBackgroundColor:[UIColor blackColor]];
        [txtView setTextColor:[UIColor greenColor]];
        str = @"second";
    }
    else
    {
       [txtView setBackgroundColor:[UIColor lightGrayColor]];
       [txtView setTextColor:[UIColor blackColor]];  
        str = @"first";
    }
}

【讨论】:

  • 啊!这个很漂亮,谢谢!如果我没有在我的原始帖子中明确说明这一点,我深表歉意。今晚我可以访问我的工作站时尝试一下。
  • 它现在说Property 'selected' not found on object UIBarButtonItem content.screencast.com/users/revblaze/folders/Jing/media/…
  • 注意:我确实已连接所有已发送的操作和引用出口。
  • @MattBush 你在问题中说它的 UIButton 。我认为 UIbarButton 没有选择属性。你可以用这样的栏按钮来做到这一点。检查我编辑的代码。虽然它是非常直接的方法。在编辑后的代码中,'str' 是一个 NSString 变量。在.h文件中声明
  • 工作得很好。感谢大家的帮助!
【解决方案2】:

试试这个

 (void)setTextColor:(UIColor *)color forState:(UIControlState)state

[button setTextColor:[UIColor greenColor] forState:UIControlStateNormal];
[button setTextColor:[UIColor blackColor] forState:UIControlStateHighlighted];

 (void)setBackgroundColor:(UIColor *)color forState:(UIControlState)state

[button setBackgroundColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor lightgrayColor] forState:UIControlStateHighlighted];

我认为你正在寻找类似的东西

【讨论】:

  • 是的,这正是我想要的!现在我该如何实现它,因为无论我在哪里添加它,我都会返回 2 个错误:Use of undeclared identifier setTextColorUse of undeclared identified setBackgroundColor 并不是它目前在我的 enableSleepMode IBAction 下。
  • 好笑。您是否适当地重命名了按钮?我知道它适用于标题/栏。有一种更复杂的方法。见mobile.tutsplus.com/tutorials/iphone/…。在他们的示例中,当文本字段处于活动状态时,背景变为绿色。它涉及添加一个布尔值。
猜你喜欢
  • 1970-01-01
  • 2014-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-31
  • 1970-01-01
相关资源
最近更新 更多