基本空间的使用(MRC)

来源:互联网 发布:java url 转码na 编辑:程序博客网 时间:2024/06/07 03:38

// 准备工作
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
UIViewController *viewControl = [[UIViewController alloc] init];
self.window.rootViewController = viewControl;
[viewControl release];
[self.window release];
// 标签lable
CGFloat width = self.window.frame.size.width;
CGFloat height = self.window.frame.size.height;
// 创建一个标签的对象初始化
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(width / 3, height / 10, width / 3, 100)];
// 标签内容为 text 文本
lable.text = @”我最帅1234567812345678901234567890”;
// 标签的背景颜色
lable.backgroundColor = [UIColor yellowColor];
// 字体颜色
lable.textColor = [UIColor blackColor];
// 设置字体大小
lable.font = [UIFont systemFontOfSize:30];
// 设置文本行数
lable.numberOfLines = 3;
// 断行模式
lable.lineBreakMode = NSLineBreakByTruncatingTail;
// 阴影颜色及大小
lable.shadowColor = [UIColor redColor];
lable.shadowOffset = CGSizeMake(1, 2);
// 将控件加到视图上
[self.window addSubview:lable];
[lable release];
// 文本输入框
UITextField *textfield = [[UITextField alloc] initWithFrame:CGRectMake(width / 3, 200, width / 3, 50)];
// 设置背景颜色
textfield.backgroundColor = [UIColor cyanColor];
// 文本框提示的字
textfield.placeholder = @”手机号”;
// 输入文本的颜色
textfield.textColor = [UIColor redColor];
// 文本边框设置(枚举类型)
textfield.borderStyle = UITextBorderStyleRoundedRect;
// 对齐方式字体大小都与 lable 的设置相同
// 文本框有的时候需要判断是否允许输入
// 默认允许输入(bool 类型的)
textfield.enabled = YES;
// 是否字以圆点格式(类似密码中需要保密);
textfield.secureTextEntry = YES;
// 是否开始输入的时候清空
textfield.clearsOnBeginEditing = YES;
// 设置键盘风格
// textfield.keyboardType = UIKeyboardTypeNumberPad;
/**
UIKeyboardTypeDefault
UIKeyboardTypeASCIICapable
UIKeyboardTypeNumbersAndPunctuation
UIKeyboardTypeURL
UIKeyboardTypeNumberPad
UIKeyboardTypePhonePad
UIKeyboardTypeNamePhonePad
UIKeyboardTypeEmailAddress
UIKeyboardTypeTwitter
UIKeyboardTypeWebSearch
*/
// 设置键盘右下角
textfield.returnKeyType = UIReturnKeyGo;
/**
UIReturnKeyDefault,
UIReturnKeyGo,
UIReturnKeyGoogle,
UIReturnKeyJoin,
UIReturnKeyNext,
UIReturnKeyRoute,
UIReturnKeySearch,
UIReturnKeySend,
UIReturnKeyYahoo,
UIReturnKeyDone,
UIReturnKeyEmergencyCall,
UIReturnKeyContinue
*/
// textField 有监听事件(当输入文本内容的时候可以有一些操作)
// 将空间加载到视图上
[self.window addSubview:textfield];
textfield.tag = 1001;
[textfield release];
// 图片视图
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(width / 3, 300, width / 3, width / 3)];
// 在视图上添加图片的方法
// 将图片拖入到工程文件中
imageView.image = [UIImage imageNamed:@”1.JPG”];
imageView.backgroundColor = [UIColor greenColor];
// 设置填充的方式
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.window addSubview:imageView];
// 按钮
// 按钮的初始化特殊而且需要有点击事件不需要释放空间
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(width / 3, 400, width / 3, width / 3);
// 按钮的颜色与上面的空间相同
// 设置按钮的标题
[button setTitle:@”点我” forState: UIControlStateNormal];
// button(可以设置图片)
// [button setImage:<#(nullable UIImage *)#> forState:<#(UIControlState)#>]
// 按钮记得写点击事件
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:button];
// 上面的基本空间都有一个 tag 值得属性以便下面可以获取到这个 tag 值
return YES;
}
// 给按钮的点击事件
- (void)buttonAction:(UIButton *)sender {
NSLog(@”我最帅”);
// 获取上面的空间需要定义一个相同的基本空间来承接
// textField 是self.window 的子视图
UITextField *textField = [self.window viewWithTag:1001];
// 取消键盘的第一响应
[textField resignFirstResponder];
}

1 0
原创粉丝点击