NSButton And NSTextField

NSButton And NSTextField

建立一个应用,在窗口里面增加一个标签,12个按钮。

主要实现的功能是:标签的文本改变和颜色改变

按钮文本的改变和颜色的改变。

m文件

@implementation QXSAppDelegate

@synthesize m_LB_1;
@synthesize m_BT_1;
@synthesize m_BT_2;

- (void)dealloc
{
    [super dealloc];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
}

- (IBAction)OnBT_ChangeTo_Hello:(id)sender
{
   m_LB_1.stringValue = @"Hello,everyone.";
}

- (IBAction)OnBT_ChangeTo_Wonderful:(id)sender
{
    NSString *name1;
    name1=@"That is Wonderful!";
    m_LB_1.stringValue = name1;    
}

- (IBAction)OnBT_ChangeButtonsTitle:(id)sender
{
   [self Do_Button1Click_Event];
}

- (IBAction)OnBT_ChangeButtonsTitle2:(id)sender
{
    [self Do_Button2Click_Event];
}

-(void)Do_Button1Click_Event
{
    // Change Button1 Title
    NSString *name1;
    name1 = @"This is Button 1.";
    [m_BT_1 setTitle:name1];
    
    // Change Button2 Title
    NSString *name2;
    name2 = @"This is Button 2.";
    [m_BT_2 setTitle:name2];
   
}
-(void)Do_Button2Click_Event
{
    // Change Button1 Title
    NSString *name1;
    name1 = @"Change Label to Hello.";
    [m_BT_1 setTitle:name1];
    
    // Change Button2 Title
    NSString *name2;
    name2 = @"Change Label to Wonderful.";
    [m_BT_2 setTitle:name2];
}

- (IBAction)OnBT_ChangeColor:(id)sender
{
    NSColor *color = [NSColor  purpleColor];
   [self setButtonTextColor:m_BT_1 withColor:color];
}

- (IBAction)OnBT_ChangeBTColor2:(id)sender
{
    NSColor *color = [NSColor redColor];
    [self setButtonTextColor:m_BT_2 withColor:color];
}
- (IBAction)OnBT_ChangeBTTextAndColor1:(id)sender
{
    NSColor *color = [NSColor orangeColor];
    NSString *title1 =@"Apple";
    [self setButtonTextAndColor:m_BT_1 withTitle:title1 withColor:color];
}

- (IBAction)OnBT_ChangeBTTextAndColor2:(id)sender
{
    NSColor *color = [NSColor blueColor];
    NSString *title1 =@"Orange";
    [self setButtonTextAndColor:m_BT_2 withTitle:title1 withColor:color];
}

- (IBAction)OnBT_ChangeBTTextAndColor3:(id)sender
{
    [self Do_Button1Click_Event];
    NSColor *color1 = [NSColor magentaColor];
    [self setButtonTextColor:m_BT_1 withColor:color1];
    [self setButtonTextColor:m_BT_2 withColor:color1];    
}

- (IBAction)OnBT_ChangeBTTextAndColor4:(id)sender
{
    [self Do_Button2Click_Event];
    NSColor *color1 = [NSColor redColor];
    [self setButtonTextColor:m_BT_1 withColor:color1];
    [self setButtonTextColor:m_BT_2 withColor:color1];
}

/********************************************************
 Function : set Button Text Color
 *********************************************************/
- (void)setButtonTextColor:(NSButton*)button withColor:(NSColor*)color
{
    NSMutableAttributedString *colorTitle =
    
    [[NSMutableAttributedString alloc] initWithAttributedString:[button attributedTitle]];
    
    NSRange titleRange = NSMakeRange(0, [colorTitle length]);
    [colorTitle addAttribute:NSForegroundColorAttributeName value:color range:titleRange];
    [button setAttributedTitle:colorTitle];
}

/********************************************************
 Function : set Button Text Color
 *********************************************************/
- (void)setButtonTextAndColor:(NSButton*)button withTitle:title withColor:(NSColor*)color
{
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    
    [style setAlignment:NSCenterTextAlignment];
    
    NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:color, NSForegroundColorAttributeName, style, NSParagraphStyleAttributeName, nil];
    
    NSAttributedString *attrString = [[NSAttributedString alloc]initWithString:title attributes:attrsDictionary];
    
    [button setAttributedTitle:attrString];
    
    [style release];
    
    [attrString release];
}
- (IBAction)OnButton1:(id)sender
{
    [self OnBT_ChangeTo_Hello:m_BT_1];
     m_LB_1.textColor = [NSColor redColor];
     m_LB_1.stringValue = @"Tomorrow is sunny!";
   
}
/*
  改变标签的颜色和字体
 */
- (IBAction)OnButton2:(id)sender
{
    [self OnBT_ChangeTo_Wonderful:m_BT_2];
    m_LB_1.textColor = [NSColor grayColor];
    m_LB_1.stringValue = @"If I were you, I would come there!";
}


h文件

//
//  111AppDelegate.h
//  test_button
//
//  Created by someone on 26/5/14.
//  Copyright (c) 2014 someone. All rights reserved.
//

#import <Cocoa/Cocoa.h>

@interface QXSAppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;

@property (assign) IBOutlet NSTextField *m_LB_1;

@property (assign) IBOutlet NSButton *m_BT_1;

@property (assign) IBOutlet NSButton *m_BT_2;

- (void)setButtonTextColor:(NSButton*)button withColor:(NSColor*)color;

- (void)setButtonTextAndColor:(NSButton*)button withTitle:title withColor:(NSColor*)color;

-(void)Do_Button1Click_Event;

-(void)Do_Button2Click_Event;

@end


完。

测试成功!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值