码迷,mamicode.com
首页 > 其他好文 > 详细

NSButton文本颜色设置和Action两种状态行为

时间:2015-10-29 19:47:30      阅读:862      评论:0      收藏:0      [点我收藏+]

标签:

 

//获取文本颜色:

- (NSColor *)textColor

{

    NSAttributedString *attrTitle = [self attributedTitle];

    int len = [attrTitle length];

    NSRange range = NSMakeRange(0, MIN(len, 1)); // get the font attributes from the first character

    NSDictionary *attrs = [attrTitle fontAttributesInRange:range];

    NSColor *textColor = [NSColor controlTextColor];

    if (attrs)

    {

        textColor = [attrs objectForKey:NSForegroundColorAttributeName];

    }

   

    return textColor;

}

 

//设置文本颜色

- (void)setTextColor:(NSColor *)textColor

{

    NSMutableAttributedString *attrTitle =

        [[NSMutableAttributedString alloc] initWithAttributedString:[self attributedTitle]];

    int len = [attrTitle length];

    NSRange range = NSMakeRange(0, len);

    [attrTitle addAttribute:NSForegroundColorAttributeName value:textColor range:range];

    [attrTitle fixAttributesInRange:range];

    [self setAttributedTitle:attrTitle];

    [attrTitle release];

}

 

 

//一个button两种状态行为:

- (IBAction)pressSpaceKey:(id)sender {

  

    NSLog(@" go to record");

   

    // 需要给这个按钮增加一个事件,让它与keyDown/keyUp 关联,按住按钮时一直录音,松开按钮时录音结束。

 

    NSButton * btn = (NSButton *)sender;

    if ([btn tag] == 100) {

        btn.tag = 101;

     //添加动作代码

        [btn setTitle:@"停止录音"];

       

       

    }else if ([btn tag] == 101){

        btn.tag = 100;

    //添加另外一种状态的动作代码

        [btn setTitle:@"开始录音"];

      

    }

}

 

//单选框

IBOutlet NSMatrix *popoverType;

 

NSButton文本颜色设置和Action两种状态行为

标签:

原文地址:http://www.cnblogs.com/PJXWang/p/4921507.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!