效果图
AppDelegate.h文件
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,UITextFieldDelegate> //添加协议
//编辑环境为MRC, 改用 retain 修饰实例变量
@property (retain, nonatomic) UIWindow *window;
@end
AppDelegate.m文件
@implementation AppDelegate
//编辑环境选用的是MRC模式, 需重写dealloc方法
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
//3.color -> 数值 Apple SD Gothic Neo Courier New
UILabel *redLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 80, 30)];
redLabel.backgroundColor = [UIColor redColor];
NSLog(@"%@",[UIFont familyNames]);
redLabel.font = [UIFont fontWithName:@"Courier New" size:25];
redLabel.textAlignment = NSTextAlignmentCenter;
redLabel.text = @"Red";
redLabel.textColor = [UIColor whiteColor];
[self.window addSubview:redLabel];
[redLabel release];
UITextField *redTextField = [[UITextField alloc] initWithFrame:CGRectMake(160, 100, 165, 30)];
redTextField.tag = 101;
redTextField.backgroundColor = [UIColor whiteColor];
redTextField.placeholder = @"请输入0-255的数";
redTextField.borderStyle = UITextBorderStyleRoundedRect;
[self.window addSubview:redTextField];
[redTextField release];
UILabel *greenLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 150, 80, 30)];
greenLabel.backgroundColor = [UIColor greenColor];
greenLabel.text= @"Green";
greenLabel.textColor = [UIColor whiteColor];
greenLabel.font = [UIFont fontWithName:@"Courier New" size:25];
greenLabel.textAlignment = NSTextAlignmentCenter;
[self.window addSubview:greenLabel];
[greenLabel release];
UITextField *greenTextField = [[UITextField alloc] initWithFrame:CGRectMake(160, 150, 165, 30)];
greenTextField.tag = 102;
greenTextField.backgroundColor = [UIColor whiteColor];
greenTextField.placeholder = @"请输入0-255的数";
greenTextField.borderStyle = UITextBorderStyleRoundedRect;
[self.window addSubview:greenTextField];
[greenTextField release];
UILabel *blueLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 200, 80, 30)];
blueLabel.backgroundColor = [UIColor blueColor];
blueLabel.text = @"Blue";
blueLabel.textColor = [UIColor whiteColor];
blueLabel.textAlignment = NSTextAlignmentCenter;
blueLabel.font = [UIFont fontWithName:@"Courier New" size:25];
[self.window addSubview:blueLabel];
[blueLabel release];
UITextField *blueTextField = [[UITextField alloc] initWithFrame:CGRectMake(160, 200, 165, 30)];
blueTextField.tag = 103;
blueTextField.backgroundColor = [UIColor whiteColor];
blueTextField.placeholder = @"请输入0-255的数";
blueTextField.borderStyle = UITextBorderStyleRoundedRect;
[self.window addSubview:blueTextField];
[blueTextField release];
UIButton *okButton = [UIButton buttonWithType:UIButtonTypeSystem];
okButton.frame = CGRectMake(150, 280, 75, 30);
okButton.backgroundColor = [UIColor whiteColor];
[okButton setTitle:@"确定" forState:UIControlStateNormal];
[self.window addSubview:okButton];
[okButton addTarget:self action:@selector(pressButton) forControlEvents:UIControlEventTouchUpInside];
return YES;
}
点击按钮的pressButton的方法实现
- (void)pressButton {
NSInteger Red, Green, Blue;
Red =arc4random() % 256 ;
Green = arc4random() % 256 ;
Blue = arc4random() % 256 ;
NSString *redString = [NSString stringWithFormat:@"%ld",Red];
NSString *greenString = [NSString stringWithFormat:@"%ld",Green];
NSString *blueString = [NSString stringWithFormat:@"%ld",Blue];
UITextField *redField = (UITextField *)[self.window viewWithTag:101];//通过tag值找到 redTextField
UITextField *greenField = (UITextField *)[self.window viewWithTag:102];//通过tag值找到 greenTextField
UITextField *blueField = (UITextField *)[self.window viewWithTag:103];//通过tag值找到 blueTextField
redField.text = redString;
greenField.text = greenString;
blueField.text = blueString;
self.window.backgroundColor = [UIColor colorWithRed:Red / 255. green:Green / 255. blue:Blue / 255. alpha:1.0];
}