iOS开发-Day26-UILabel&UIButton&UITextField

1、UILable

@interface ViewController : UIViewController
@property(strong,nonatomic) UILabel *lblName;
@end
ViewController.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<UITextFieldDelegate
2. 根据协议的要求,在实现文件中加入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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值