标签:
1 // 创建UILabel对象 2 UILabel *userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 100, 100, 30)]; 3 4 // 设置文字 5 userNameLabel.text = @"用户名"; 6 7 // 将UILabel添加到父视图 8 [self.window addSubview:userNameLabel]; 9 10 // 释放所有权(MRC模式下) 11 // [userNameLabel release]; 12 // 创建UILabel对象(适配屏幕) 13 14 UILabel *userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 200, 80, 40)]; 15 UILabel *l1 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(userNameLabel.frame) + 20, CGRectGetMinY(userNameLabel.frame), CGRectGetWidth(self.window.frame) - CGRectGetWidth(userNameLabel.frame) - 60, CGRectGetHeight(userNameLabel.frame))]; 16 UILabel *l2 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(userNameLabel.frame), CGRectGetMaxY(userNameLabel.frame) + 10, CGRectGetWidth(userNameLabel.frame), CGRectGetHeight(userNameLabel.frame))]; 17 UILabel *l3 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(userNameLabel.frame) + 20, CGRectGetMaxY(userNameLabel.frame) + 10, CGRectGetWidth(self.window.frame) - CGRectGetWidth(userNameLabel.frame) - 60, CGRectGetHeight(userNameLabel.frame))];
// 设置背景颜色 userNameLabel.backgroundColor = [UIColor cyanColor]; // 设置文本对齐方式 userNameLabel.textAlignment = NSTextAlignmentRight; // 设置文字颜色 userNameLabel.textColor = [UIColor purpleColor]; // 设置字体 userNameLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20]; // 打印所有字体样式 NSLog(@"%@", [UIFont familyNames]); // 显示行数 userNameLabel.numberOfLines = 3; //断行模式(以单词断行) userNameLabel.lineBreakMode = NSLineBreakByWordWrapping; // 阴影颜色 userNameLabel.shadowColor = [UIColor blackColor]; // 阴影大小 userNameLabel.shadowOffset = CGSizeMake(2, 1);
// 创建UITextField对象 UITextField *userNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 190, 30)]; // 设置边框风格 userNameTextField.borderStyle = UITextBorderStyle RoundedRect; // 设置占位符 userNameTextField.placeholder = @"手机号/邮箱"; // 将UITextField添加到父视图 [self.window addSubview:userNameTextField]; // 释放所有权(MRC) [userNameTextField release];
// 设置文本内容 userNameTextField.text = @"用户名:"; userNameTextField.textColor = [UIColor blackColor]; // 文本对齐方式 userNameTextField.textAlignment = NSTextAlignmentLeft;
// 是否允许编辑 userNameTextField.enabled = YES; // 开始编辑时是否清空输入框 userNameTextField.clearsOnBeginEditing = YES; //是否安全输入 userNameTextField.secureTextEntry = YES; // 弹出键盘的类型 userNameTextField.keyboardType = UIKeyboardTypeAlphabet; // 键盘右下角return按钮类型(枚举值) userNameTextField.returnKeyType = UIReturnKeyDefault;
// 清除按钮模式 userNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing; // 输入框左视图 UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)]; leftView.backgroundColor = [UIColor yellowColor]; userNameTextField.leftView = leftView; userNameTextField.leftViewMode = UITextFieldViewModeAlways; // 输入框右视图 UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)]; rightView.backgroundColor = [UIColor redColor]; userNameTextField.rightView = rightView; userNameTextField.rightViewMode = UITextFieldViewModeAlways;
// 1.设置代理 userNameTextField.delegate = self; // 2.遵守协议 @interface AppDelegate : UIResponder <UIApplicationDelegate,UITextFieldDelegate> // 3.实现协议方法 - (BOOL)textFieldShouldReturn:(UITextField *)textField { NSLog(@"键盘上回车按键"); [textField resignFirstResponder]; return YES; } // 成为第一响应者,运行就进入编辑状态 [userNameTextField becomeFirstResponder];
// 1.当textField将要开始编辑的时候告诉委托人 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { return YES; } // 2.当textField已经编辑的时候告诉委托人 - (void)textFieldDidBeginEditing:(UITextField *)textField { } // 3.当textField将要完成编辑的时候告诉委托人 - (BOOL)textFieldShouldEndEditing:(UITextField *)textField { return YES; } // 4.当textField已经完成编辑的时候告诉委托人 - (void)textFieldDidEndEditing:(UITextField *)textField { } // 5.当点击键盘上回车按键时候告诉委托人 - (BOOL)textFieldShouldReturn:(UITextField *)textField { return YES; }
// 1.创建对象并初始化 (使用类方法) UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; // 2.设置属性 button.frame = CGRectMake(50, 100, 300, 50); button.backgroundColor = [UIColor orangeColor]; // 设置标题 普通状态下 [button setTitle:@"点 我" forState:UIControlStateNormal]; // 设置标题 高亮状态下(点住) [button setTitle:@"谁点我" forState:UIControlStateHighlighted]; // 设置标题颜色 button.tintColor = [UIColor whiteColor]; // 设置button的背景图片 // 创建UIImage对象 UIImage *afuImage = [UIImage imageNamed:@"afu.jpg"]; // 普通状态下 [button setBackgroundImage:afuImage forState:UIControlStateNormal]; // 高亮状态下 [button setBackgroundImage:[UIImage imageNamed:@"zhatian.jpg"] forState:UIControlStateHighlighted]; // 设置前景图片(必须是镂空图) [button setImage:[UIImage imageNamed:@"222.png"] forState:UIControlStateNormal]; // 3.添加事件 // 单击状态下 [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside]; // 4.添加到父视图 [self.window addSubview:button];
// 实现按钮点击事件 - (void)buttonClick:(UIButton *)sender { NSLog(@"点我"); sender.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1]; // 移除事件 [sender removeTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside]; }
标签:
原文地址:http://www.cnblogs.com/soley/p/5397759.html