UILabel 、UIbutton 、 UITextField 代码总结

本文详细介绍了如何在iOS应用中配置界面元素,包括按钮、标签和文本框等,并提供了具体的代码实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//

//  ViewController.m

//  Strategy

//


各位亲 有时间可以去看看我的  “金骏家居淘宝店” http://jinjun1688.taobao.com/shop/view_shop.htm?tracelog=twddp 买时说明在我的博客看到有优惠哦 还有意外礼品赠送  真正的程序员淘宝店


#import "ViewController.h"


@interfaceViewController ()


@property (nonatomic,retain)UITextView *textView;

@end


@implementation ViewController




//- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

//{

//    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

//    if (self) {

//        // Custom initialization

//    }

//    return self;

//}


- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

   UIButton *button1 = [[UIButtonalloc]initWithFrame:CGRectMake(200, 200, 150, 150)]; //参考点是左上角

    [button1 setBackgroundColor:[UIColorblueColor]];  //设置背景颜色

    [button1 setTitle:@"button"forState:UIControlStateNormal];  //按钮标签

    [button1 setTitleColor:[UIColorwhiteColor]forState:UIControlStateNormal];//设置标题颜色

//    [button1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

    [button1 setTitleShadowColor:[UIColorgrayColor]forState:UIControlStateNormal];//设置阴影

    [button1 setBackgroundImage:[UIImageimageNamed:@"button.jpj"]forState:UIControlStateHighlighted];  //设置背景图片

    [button1 addTarget:selfaction:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];

    

    [self.viewaddSubview:button1];  //显示控件

    

    UIButton *button2 = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];    //设置按钮风格

    button2.frame =CGRectMake(10, 20, 40, 40);

    [button2 setImage:[UIImageimageNamed:@"button.jpj"]forState:UIControlStateNormal];//设置图片button

    

 

    

    

    

    [self.viewaddSubview:button2];

    

    //设置位置

    UILabel *lable = [[UILabelalloc]initWithFrame:CGRectMake(20, 50, 150, 100)];

     //给标签命名

    lable.text =@"用户名";

    //设置字体大小

    lable.font = [UIFontboldSystemFontOfSize:40];

     //设置标签颜色

    lable.textColor = [UIColororangeColor];

   //设置标签文字对齐为止,居中,居左,居右

    lable.textAlignment =UITextAlignmentCenter;   

    lable.textAlignment =UITextAlignmentRight;

   //设置字体大小是否适应label宽度

    lable.adjustsFontSizeToFitWidth=YES;

     //设置label行数

    //lable.numberOfLines = 3;

   //设置文本是否高亮设置高亮时的颜色

    lable.highlighted =YES;  

    lable.highlightedTextColor = [UIColorblueColor];

   //设置能否和用户进行交互

    lable.userInteractionEnabled =YES;

   //设置label中的文字是否可变,默认值是yes

    lable.enabled =YES;

   //设置文字过长时的显示格式  此为截去中间部分

    lable.lineBreakMode =UILineBreakModeMiddleTruncation;

    //设置背景色为透明

    lable.backgroundColor = [UIColorclearColor]; 

    //自定义颜色

   UIColor *color1 = [UIColorcolorWithRed:1.0fgreen:50.0fblue:0.0falpha:1.0f];

    lable.textColor = color1;

   //设置阴影颜色编译的位置

    lable.shadowColor = [UIColorredColor];

    lable.shadowOffset =CGSizeMake(1.0, 1.0);

    

    [self.viewaddSubview:lable];

    

    

    

    UITextField* text = [[UITextFieldalloc]initWithFrame:CGRectMake(150, 60, 120, 120)];  //设置位置

    [text setBorderStyle:UITextBorderStyleRoundedRect]; //设置外框类型

    text.placeholder =@"用户名";  //默认显示的字 在文本框中显示灰色的字,用于提示用户应该在这个文本框输入什么内容。当这个文本框中输入了数据时,用于提示的灰色的字将会自动消失。

    text.secureTextEntry =YES;    //密码

    text.clearsOnBeginEditing =YES;//设置为YES当用点触文本字段时,字段内容会被清除

    text.adjustsFontSizeToFitWidth =YES;//设置为YES时文本会自动缩小以适应文本窗口的大小。默认是保持原来大小,而让长文本滚动 

    text.autocorrectionType =UITextAutocorrectionTypeYes//定义文本是否使用iPhone的自动更新功能

    text.autocapitalizationType =UITextAutocapitalizationTypeNone;// 设置键盘自动大小写的属性

    text.returnKeyType =UIReturnKeyDone;   //设置键盘完成的按钮

    text.clearButtonMode =UITextFieldViewModeWhileEditing// 清空输入字符不为空,且在编辑状态时(及获得焦点)显示清空按钮

    

    text.delegate =self;    //设置委托

    

    

//    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(100, 250, 120, 120)];

//

//    self.textView.text = @"sss";

//    

//  

//    self.textView.autocapitalizationType = UITextAutocapitalizationTypeNone;

//    self.textView.returnKeyType = UIReturnKeyDone;

//      self.textView.delegate = self;

//    

//    [self.view addSubview:self.textView];

    

    

    

    [self.viewaddSubview:text];

    

    

    

    

//    [super viewDidLoad];

//    self.textView = [[UITableView alloc]initWithFrame:self.view.frame ];

//    self.textView.text = @"user";

//    [self.view addSubview:selfView];

    

}



//委托方法


  //当开始点击textField会调用的方法

-(void) textFieldDidBeginEditing:(UITextField *)textField  

{

    

}


//textField编辑结束时调用的方法

-(void) textFieldDidEndEditing:(UITextField *)textField    

{

    

}


//按下Done按钮调用的方法,让键盘消失

- (BOOL) textFieldShouldReturn:(UITextField *) textField

{

    [textField resignFirstResponder];


    returnYES;

    

}


-(IBAction) textFieldDone:(id) sender

{

    [_textViewresignFirstResponder];

}



- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}








-(void) viewWillAppear:(BOOL)animated

{

   //注册通知,监听键盘出现

    [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(handleKeyboardDidShow:)name:UIKeyboardDidShowNotificationobject:nil];

    

   //注册通知,监听键盘消失事件

    [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(handleKeyboardDidHidden)name:UIKeyboardDidShowNotificationobject:nil];

    

    [superviewWillAppear:YES];

}


//监听事件

-(void) handleKeyboardDidShow:(NSNotification*)paramNotification

{

    //获取键盘高度

    NSValue *keyboardRectAsObject = [[paramNotificationuserInfo]objectForKey:UIKeyboardFrameEndUserInfoKey];

    

   CGRect keyboardRect;

    [keyboardRectAsObjectgetValue:&keyboardRect];

    

    self.textView.contentInset =UIEdgeInsetsMake(0, 0, keyboardRect.size.height,0);

    

}


-(void)handleKeyboardDidHidden

{

    

      self.textView.contentInset =UIEdgeInsetsZero;

}


-(void)viewDidAppear:(BOOL)animated

{

    [[NSNotificationCenterdefaultCenter]removeObserver:self ];

    

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值