iOS——UILabel UIButton UITextField

  1. 在面板上创建一个新的全屏面板
    UIView *contentView = [[UIView alloc]initWithFrame”[UIScreen mainScreen].bounds];
    contentView.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:contentView];

UILabel知识点:

步骤: 创建控件
配置属性
添加到父视图上
释放所有权

 // 1. 创建UILabel对象

UILabel *label = [[UILabel alloc]initWithFrame:(CGRectMake(10, 80, 300, 60))];
// 2. 配置UILabel的属性
// 2.1 设置背景颜色
label.backgroundColor = [UIColor lightGrayColor];
[self.window addSubview:label];
// 2.2 设置显示的文字
label.text = @”AVacterSuoNingAVacterSuoNingAVacterSuoNing”;
// 2.3 设置文本颜色
label.textColor = [UIColor redColor];
// 2.4 设置文本字体
label.font = [UIFont systemFontOfSize:20];
// 加粗字体
label.font = [UIFont boldSystemFontOfSize:40];
// UIFont是一个字体类,便利系统中可以使用字体名称
for (NSString *name in [UIFont familyNames]) {
NSLog(@”%@”,name);
}

label.font = [UIFont fontWithName:@”Snell Roundhand” size:20];

      // 2.5 设置行数

label.numberOfLines = 0; // 设置为0,表示不限制行数,默认是1

      // 2.6 行数的截取方式

//NSLineBreakByCharWrapping 通过单词截取
//NSLineBreakByCharWrapping 通过字符截取
// ··· ··· 还有几种
label.lineBreakMode = NSLineBreakByCharWrapping;

      // 2.7 设置label的阴影颜色

label.shadowColor = [UIColor blackColor];

      // 2.8 设置阴影偏移量

label.shadowOffset = CGSizeMake(2, 1);

      // 2.9 设置文本的对齐方式

//NSTextAlignmentLeft
//NSTextAlignmentRight

label.textAlignment = NSTextAlignmentCenter;

      // 2.10 给Label加圆角,下面两个方法必须同时设定

label.layer.cornerRadius = 10;
label.layer.masksToBounds = YES;

 // 3. 添加到父视图

[contentView addSubview:label];
// 4. 释放所有权
[contentView release];

/**
改变Label文字的大小

下面分两种情况考虑:1、UILabel宽度不变,根据字体多少,自动调整UILabel的高度,并折行显示。代码如下:label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 300, 20)];
label.font = [UIFont boldSystemFontOfSize:20.0f]; //UILabel的字体大小
label.numberOfLines = 0; //必须定义这个属性,否则UILabel不会换行
label.textColor = [UIColor whiteColor];
label.textAlignment = NSTextAlignmentLeft; //文本对齐方式
[label setBackgroundColor:[UIColor redColor]];
//宽度不变,根据字的多少计算label的高度
NSString *str = @”可以更改此内容进行测试,宽度不变,高度根据内容自动调节”;
CGSize size = [str sizeWithFont:label.font constrainedToSize:CGSizeMake(label.frame.size.width, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
//根据计算结果重新设置UILabel的尺寸
[label setFrame:CGRectMake(0, 10, 300, size.height)];
label.text = str;
[self.view addSubview:label]; 复制代码2、UILabel高度不变,根据字体多少,自动调整UILabel的宽度,并折行显示代码如下
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 20, 20)];
label.font = [UIFont boldSystemFontOfSize:20.0f]; //UILabel的字体大小
label.numberOfLines = 0; //必须定义这个属性,否则UILabel不会换行
label.textColor = [UIColor whiteColor];
label.textAlignment = NSTextAlignmentLeft; //文本对齐方式
[label setBackgroundColor:[UIColor redColor]];
//高度固定不折行,根据字的多少计算label的宽度

pragma mark UITextField知识点

// UITextField 继承自UIControl,是在UILabel的基础上扩充了文本编辑的功能,可以允许用于输入或者修改文字

 //1. 创建UITextField对象

UITextField *field = [[UITextField alloc]initWithFrame:CGRectMake(10, 200, 300, 30)];
field.tag = 200;
// 2. 配置属性
// Command + shift +K 控制键盘弹出与收回
field.backgroundColor = [UIColor lightGrayColor];
// 2.1 背景颜色
field.backgroundColor = [UIColor cyanColor];
// 2.2 设置文本输入框的提示文字
field.placeholder = @”请输入索宁的三围:”;
// 2.3 设置输入框文字
field.text = @”50”;
// 2.4 设置输入框的文本颜色
field.textColor = [UIColor redColor];
// 2.5 设置文本输入框的对其格式
field.textAlignment = NSTextAlignmentCenter;
// 2.6 关闭文本输入框的是否可编辑功能,默认是可编辑(YES)
// field.enabled = NO;
// 关闭用户的交互功能
// field.userInteractionEnabled = NO;
// 2.7 当文本输入框开始编辑的时候清空输入框内的内容,默认是NO,只在第一次编辑的时候起作用
field.clearsOnBeginEditing = YES;
// 2.8 设置键盘弹出的格式
field.keyboardType = UIKeyboardTypeDefault;
// 2.9 设置return键的类型
field.returnKeyType = UIReturnKeyJoin;
// 2.10 设置文本输入框是否以加密的形式显示,默认是NO
field.secureTextEntry = NO;
// 2.11 设置文本输入框的样式
// UITextBorderStyleNone 无
// UITextBorderStyleBezel 带阴影边框
// UITextBorderStyleLine 实线边框
field.borderStyle = UITextBorderStyleRoundedRect; // 圆角边框

      // 2.12 自定义弹出视图

/*
UIView *inputView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
inputView.backgroundColor = [UIColor redColor];
field.inputView = inputView;
[inputView release];
*/
// 2.13 自定义键盘上方的辅助视图
UIView *AccessoryView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
AccessoryView.backgroundColor = [UIColor redColor];
field.inputAccessoryView =AccessoryView;
[AccessoryView release];

      // 2.14 设置文本框的输入按钮

// UITextFieldViewModeNever, 默认
// UITextFieldViewModeWhileEditing, 编辑时出现
// UITextFieldViewModeUnlessEditing, 不编辑时出现
// UITextFieldViewModeAlway 一直出现

field.clearButtonMode = UITextFieldViewModeAlways;

      // 2.15 代理属性

// 选择代理条件,能成为代理的对象必须是能够按到源代码的类,因为我们要在该类 .m 中实现协议中的方法
field.delegate = self; // 可以在类的延展中也可以遵循协议,在上面声明

 // 3. 添加到父视图     

[contentView addSubview:field];

 // 4. 释放

[field release];

pragma mark UIButton知识点

// UIButton 是iOS中用来响应用户点击事件的控件,是UIControl的子类

// 1. 创建UIButton对象
// UIButtonTypeSystem 系统样式
// UIButtonTypeDetailDisclosure 详细信息(浅色背景)
// UIButtonTypeInfoDark 详细信息(深色背景)
// UIButtonTypeInfoLight 详细信息(浅色背景)
// UIButtonTypeContactAdd 加号
// UIButtonTypeCustom 自定义格式,当需要添加图片的时候使用此种类型
UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)]; //由便利构造器创建 不需要释放

 // 2. 配置属性
      // 2.1 配置背景颜色

button.backgroundColor = [UIColor magentaColor];
// 2.2 设置button的frame
button.frame = CGRectMake(10, 300, 300, 50);
// 2.3 给button切圆角
button.layer.cornerRadius = 15;
// 2.4 给button添加标题
// 注:添加标题的时候一定要写清楚状态
// [button setTitle:@”正常状态” forState:(UIControlStateNormal)];
// [button setTitle:@”高亮状态” forState:(UIControlStateHighlighted)];
// [button setTitle:@”不可用状态” forState:UIControlStateDisabled];
// [button setTitle:@”选中状态” forState:(UIControlStateSelected)];
// 2.5 设置button是否可用
// 默认是可用的YES,设置为NO就不可用了
// button.enabled = NO;
// 2.6 设置button是否处于选中状态 默认是处于没有选中状态 NO,设置为YES处于选中状态
button.selected = NO;
// 2.7 设置button上标题文字的大小
// button是一个复合视图(有多个视图构成的视图),其中titleLabel是标题,用来显示标题,还有一个imageView,用来显示图片;
// button.titleLabel.font = [UIFont systemFontOfSize:20];
// 2.8 设置button标题的颜色
// button.tintColor = [UIColor redColor];
// 2.9 设置button的imageView的图片
// UIImage 是一个图片类,继承自NSObject
// .png 格式的图片不需要后缀,其他都需要,其他都需要
// 创建button的时候需要是Custom类型
// UIImage *image = [UIImage imageNamed:@”1”];
// [button setImage:image forState:(UIControlStateNormal)];

      // 2.10 设置button的背景图片

UIImage *image2 = [UIImage imageNamed:@”2.jpg”];
[button setBackgroundImage:image2 forState:(UIControlStateNormal)];
UIImage *image3 = [UIImage imageNamed:@”3.jpg”];
[button setBackgroundImage:image3 forState:(UIControlStateHighlighted)];

      // 2.11 button的关联事件

// target: button指定响应的对象
// action: 指定的相应对象调用的方法,方法用来处理button的点击事件
// events: 指的是事件的触发时机
[button addTarget:self
action:@selector(handleAction:) forControlEvents:UIControlEventTouchUpInside];

// 3. 添加到父视图上
[contentView addSubview:button];

self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

下面是函数:

  • (void)handleAction:(UIButton *)button{
    NSLog(@”鼓掌”);
    }

pragma mark 实现UITextFieldDelegate中的方法

// 此方法触发的时机:当我们点击return按钮的自动调用
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
// 取消文本输入框的第一响应者
[textField resignFirstResponder];
return YES;
}

pragma mark 点击空白处回收键盘

 // 点击屏幕结束的自动走这个方法

- (void)touchesEnded:(NSSet )touches withEvent:(UIEvent )event{
// 获取field所在的父视图
UIView *contentView = [self.window viewWithTag:300];
// 从contentView上获取field
UITextField field = (UITextField )[contentView viewWithTag:200]; //需要转换类型

 // 取消第一响应者

[field resignFirstResponder];
}

创建一个可以将下面TextField文本框中输入的文字转移到上面Label中的全部代码:

  • (void)dealloc
    {
    self.window = nil;
    [super dealloc];
    }
    // 绿色为创建文件后需要人为修改的部分
  • (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
    // Override point for customization after application launch.
    // 上面是系统创建的面板
    UIView *contectView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    contectView.tag = 500;
    contectView.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:contectView];

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(30, 30, 200, 30)];

label.backgroundColor = [UIColor grayColor];
label.textColor = [UIColor blackColor];
label.tag = 200;
[contectView addSubview:label];
[label release];

UITextField *field = [[UITextField alloc]initWithFrame:CGRectMake(30, 80, 200, 30)];

field.backgroundColor = [UIColor grayColor];
field.textColor = [UIColor blueColor];
field.tag = 300;
field.placeholder = @”请输入内容”;
[contectView addSubview:field];
[field release];

UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
button.frame = CGRectMake(50, 120, 100, 30);
button.backgroundColor = [UIColor redColor];
[button setTintColor:[UIColor blackColor]];
[button setTitle:@”向上” forState:(UIControlStateNormal)];

[button addTarget:self action:@selector(exchange:) forControlEvents:UIControlEventTouchUpInside];

[contectView addSubview:button];

[contectView release];

self.window.backgroundColor = [UIColor greenColor];
[self.window makeKeyAndVisible];
return YES;

}

  • (void)exchange:(NSInteger)tag{
    // 获得父视图
    UIView *content = [self.window viewWithTag:500 ];
    UILabel lab = (UILabel )[content viewWithTag:200];
    UITextField field = (UITextField )[content viewWithTag:300];
    lab.text = field.text;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值