iOS开发-Day26-UILabel&UIButton&UITextField
来源:互联网 发布:人工智能与药物研发 编辑:程序博客网 时间:2024/05/20 16:12
1、UILable
@interface ViewController : UIViewController@property(strong,nonatomic) UILabel *lblName;@endViewController.m// 初始化 self.lblName=[[UILabel alloc] initWithFrame:CGRectMake(50, 100, 250, 40)];// 背景色 self.lblName.backgroundColor=[UIColor grayColor];// 文本 self.lblName.text=@"scjy.net";// 文本颜色 self.lblName.textColor=[UIColor blueColor];// 文本对齐方式// self.lblName.textAlignment=NSTextAlignmentCenter;// self.lblName.textAlignment=NSTextAlignmentRight;// 字体 //self.lblName.font=[UIFont systemFontOfSize:60];// self.lblName.font=[UIFont boldSystemFontOfSize:35];// self.lblName.font=[UIFont italicSystemFontOfSize:30]; self.lblName.font=[UIFont fontWithName:@"Farah" size:30]; [self.view addSubview:self.lblName];// NSArray *fonts =[UIFont familyNames];// NSLog(@"%@",fonts);// 阴影 self.lblName.shadowColor=[UIColor redColor]; self.lblName.shadowOffset=CGSizeMake(0, -0.75);// 文本显示样式 // self.lblName.lineBreakMode=NSLineBreakByTruncatingHead;// 行数// self.lblName.numberOfLines=5;// 字体大小适宜label宽度// self.lblName.adjustsFontSizeToFitWidth=YES; // self.lblName.adjustsLetterSpacingToFitWidth=YES;
2、UIButton
//@property(strong,nonatomic) UIButton *btnTest;- (void)viewDidLoad { [super viewDidLoad];// 设置按钮类型 self.btnTest=[UIButton buttonWithType:UIButtonTypeRoundedRect];// 指定位置 self.btnTest.frame=CGRectMake(100, 100, 100, 40); self.btnTest.tag=100;// 设置标题 [self.btnTest setTitle:@"放大" forState:UIControlStateNormal];// 添加事件 [self.btnTest addTarget:self action:@selector(change) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.btnTest];}-(void)change{// NSLog(@"test");// UIButton *btn=(UIButton *)[self.view viewWithTag:100];//// 获取标题// NSString *title =[btn currentTitle];// NSLog(@"%@",title); NSLog(@"%@",[self.btnTest currentTitle]);}
3、UITextField
//@property(strong,nonatomic) UITextField *txtName;- (void)viewDidLoad { [super viewDidLoad]; //初始化 self.txtName=[[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 40)]; //设置边框类型 self.txtName.borderStyle=UITextBorderStyleLine; //设置文字颜色 self.txtName.textColor=[UIColor redColor]; //设置默认文本 self.txtName.placeholder=@"请输入姓名"; //背景用图片填充 self.txtName.background=[UIImage imageNamed:@"logo.png"]; //键盘类型设置 //self.txtName.keyboardType=UIKeyboardTypePhonePad; //键盘return建类型 self.txtName.returnKeyType=UIReturnKeyNext; [self.view addSubview:self.txtName]; UIButton *btnHide=[UIButton buttonWithType:UIButtonTypeRoundedRect]; btnHide.frame=CGRectMake(230, 100, 100, 40); [btnHide setTitle:@"完成" forState:UIControlStateNormal]; [btnHide addTarget:self action:@selector(hideKeyBoard) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btnHide];}-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ //是文本失去焦点,用来隐藏键盘 [self.txtName resignFirstResponder];}-(void)hideKeyBoard{// 隐藏键盘 文本控件失去第一响应值 [self.txtName resignFirstResponder];}
4、代理隐藏键盘
1. 在你的控制器类中,加入UITextFieldDelegate协议,如:@interface EditingPersonViewController : UIViewController<UITextFieldDelegate2. 根据协议的要求,在实现文件中加入textFieldShouldReturn方法,如:-(BOOL) textFieldShouldReturn:(UITextField *)textField {[textField resignFirstResponder];return YES;}3. 将xib文件中的TextField控件的delegate变量指向到之前使用UITextFieldDelegate协议的那个控制器类 将TextField的delegate IBOutlet变量右键链接到前面的控制器类的实例上。或者使用代码方式,指定相关TextField的delegate变量。- (void)viewDidLoad {[super viewDidLoad];itemNameField.delegate = self;priceField.delegate = self;}第三步很容易忽略,之前就因为忘记指定delegate变量,导致点击键盘的return健,键盘死活不隐藏。
// ViewController.h#import <UIKit/UIKit.h>@interface ViewController : UIViewController<UITextFieldDelegate>@property(strong,nonatomic) UITextField *txtName;@property(strong,nonatomic) UIButton *btnHide;@end// ViewController.m#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.btnHide=[UIButton buttonWithType:UIButtonTypeCustom]; self.btnHide.frame=self.view.frame; //self.btnHide.backgroundColor=[UIColor greenColor]; self.txtName=[[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 40)]; self.txtName.borderStyle=1;// 添加代理 self.txtName.delegate=self; [self.view addSubview:self.txtName]; [self.view addSubview:self.btnHide]; [self.btnHide addTarget:self action:@selector(hideKeyBoard) forControlEvents:UIControlEventTouchUpInside]; [self.view sendSubviewToBack:self.btnHide];}-(void)hideKeyBoard{// 当前执行的代码的行数 // NSLog(@"%d",__LINE__);// 当前指定的方法的名称 // NSLog(@"%s",__func__); [self.txtName resignFirstResponder];}//-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event//{// [self.txtName resignFirstResponder];//}- (BOOL)textFieldShouldReturn:(UITextField *)textField{ if ([textField isFirstResponder]) { [textField resignFirstResponder]; } return YES;}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
0 0
- iOS开发-Day26-UILabel&UIButton&UITextField
- IOS UI 代码创建UIButton,UITextField,UILabel
- iOS——UILabel UIButton UITextField
- UIButton,UIImageView,UILabel,UITextField
- UITextField、UILabel、UIButton
- UILabel, UITextField, UIButton
- UILabel,UITextField,UIButton
- UIView UILabel UITextField UIButton
- UILabel、UITextField、UIButton
- UIButton, UILabel, UITextField
- UILabel,UITextField,UIButton,UIimageView
- iOS开发之 UILabel,UIButton
- iOS开发:常用的UIView控件——UILabel、UITextField、UIButton
- iOS开发——基本控件(UIButton,UILabel,UITextField,UISwitch,UISlider,UISegmentedControl)
- IOS开发UI系列之常用控件 UILabel, UITextField, UIButton, UIImageView等常用属性与方法
- iOS开发:常用的UIView控件——UILabel、UITextField、UIButton【转】
- iOS UI02.2_UIView,UILabel,UIButton,UItextfield归纳
- UILabel 、UIbutton 、 UITextField 代码总结
- OAuth 2.0 原理介绍
- JAVA实现百度OCR文字识别功能
- C# String.Format格式说明
- JavaScript实现类的private、protected、public、static以及继承
- cocosbuilder
- iOS开发-Day26-UILabel&UIButton&UITextField
- ext.net 数据的使用
- mysql下merge分表
- Maven学习总结(五)——聚合与继承
- android操作通过http与服务器通信
- 使用eclipse开发web项目中常见的中文乱码问题总结
- DeepID2——强大的人脸分类算法
- (题解)(搜索)POJ2069 Super Star
- 元胞自动机 生命游戏 Matlab实现 GUI