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
完。
测试成功!