您的位置:首页 > 产品设计 > UI/UE

UI_iOS学习_UITextField_UILabel_UIButton

2013-12-29 21:06 489 查看
UITextField

//1.TextFied
//初始化textfield并设置位置及大小
textFied1 = [[UITextField alloc] initWithFrame:CGRectMake(10, 100, 100, 50)];
//textFied1.text = @"TextFied1";

//设置边框样式,只有设置了才会显示边框样式
textFied1.borderStyle = UITextBorderStyleRoundedRect;
/*   text.borderStyle = UITextBorderStyleRoundedRect;
        typedef enum {
UITextBorderStyleNone,
UITextBorderStyleLine,
UITextBorderStyleBezel,
UITextBorderStyleRoundedRect
} UITextBorderStyle;
*/

//设置字体颜色
textFied1.textColor = [UIColor blackColor];
//  textFied1.backgroundColor = [UIColor purpleColor];
textFied1.backgroundColor = [UIColor colorWithRed:196/255.0 green:241/255.0 blue:245/255.0 alpha:1];//通过RGB设置颜色,196/255.0  浮点型0.0到1.0

//对齐方式
textFied1.textAlignment = 1;    //NSTextAlignmentCenter    = 1,    // Visually centered
//NSTextAlignmentRight     = 2, // Visually right aligned

//水印提示
textFied1.placeholder = @"password";
//密码变圆点
textFied1.secureTextEntry =YES;

//设置键盘的样式

textFied1.keyboardType = UIKeyboardTypeNamePhonePad;
/*typedef enum {
UIKeyboardTypeDefault,      默认键盘,支持所有字符
UIKeyboardTypeASCIICapable, 支持ASCII的默认键盘
UIKeyboardTypeNumbersAndPunctuation, 标准电话键盘,支持+*#字符
UIKeyboardTypeURL,            URL键盘,支持.com按钮 只支持URL字符
UIKeyboardTypeNumberPad,             数字键盘
UIKeyboardTypePhonePad,   电话键盘
UIKeyboardTypeNamePhonePad,  电话键盘,也支持输入人名
UIKeyboardTypeEmailAddress,  用于输入电子 邮件地址的键盘
UIKeyboardTypeDecimalPad,    数字键盘 有数字和小数点
UIKeyboardTypeTwitter,       优化的键盘,方便输入@、#字符
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
} UIKeyboardType;
*/

//再次编辑就清空
textFied1.clearsOnBeginEditing = YES;
//设置为YES时文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动 
textFied1.adjustsFontSizeToFitWidth = YES;

UITextField使用系统协议

1,在.h文件遵守协议<UITextFieldDelegate>

2.在.m文件中设置代理textFied1.delegate =self;//使用系统协议(第二步设置代理,self是控制器)

3.使用系统协议,以下列出:

@protocol UITextFieldDelegate <NSObject>

@optional

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;        // return NO to disallow editing.
- (void)textFieldDidBeginEditing:(UITextField *)textField;           // became first responder
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;          // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
- (void)textFieldDidEndEditing:(UITextField *)textField;             // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;   // return NO to not change text

- (BOOL)textFieldShouldClear:(UITextField *)textField;               // called when clear button pressed. return NO to ignore (no notifications)
- (BOOL)textFieldShouldReturn:(UITextField *)textField;              // called when 'return' key pressed. return NO to ignore.

@end


我在这里写了三个

//使用系统协议(第三步使用系统协议)
#pragma -mark 点击键盘上的return键执行的方法
//该方法是系统协议方法,不需要创建对象去调用,点击的时候  自动调用
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textFied1 resignFirstResponder];//主要是[receiver resignFirstResponder]在哪调用就能把receiver对应的键盘往下收
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
NSLog(@"编辑将要结束");
//用一个动画解决键盘遮挡问题
//1,开始动画
[UIView beginAnimations:nil context:nil];
//2,动画持续时间
[UIView setAnimationDuration:0.5];
//3.
self.view.frame = CGRectMake(0, 20, 320, 460);
[UIView commitAnimations];
return YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
NSLog(@"将要开始编辑");

//用一个动画解决键盘遮挡问题
//1,开始动画
[UIView beginAnimations:nil context:nil];
//2,动画持续时间
[UIView setAnimationDuration:0.5];
//3.
self.view.frame = CGRectMake(0, -60, 320, 460);
[UIView commitAnimations];

return YES;
}


UILabel

//-------UILabel----------*/
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(110, 100, 100, 50)];
[self.view addSubview:myLabel];
//设置默认字体
NSString *text = @"标签文本";
myLabel.text = text;
myLabel.textAlignment = NSTextAlignmentCenter;
/*  NSTextAlignmentLeft //左对齐
NSTextAlignmentCenter //居中
NSTextAlignmentRight  //右对齐
NSTextAlignmentJustified//最后一行自然对齐
NSTextAlignmentNatural //默认对齐脚本
*/
myLabel.textColor = [UIColor magentaColor];
myLabel.numberOfLines = 0;//能显示无限行...
myLabel.shadowColor = [UIColor blackColor];
myLabel.shadowOffset = CGSizeMake(1, 2);
//用户交互(一些静态显示的默认是No)
myLabel.userInteractionEnabled = YES;
//myLabel.tag = 1;//用于标记tag值;
//myLabel.font =
for (int i = 1; i < 6; i++) {
UILabel *labe3 = [[UILabel alloc] initWithFrame:CGRectMake(10+60*(i-1), 250, 50, 50)];
[self.view addSubview:labe3];
labe3.tag = i;
}//setTag:0这个最好不要用,因为有的时候superView的tag的默认是0-----0这里为零的话 会报错
//通过tag值来找到想要的视图
UILabel *labelWithTag = (UILabel *)[self.view viewWithTag:1];
labelWithTag.text = @"我的tag是1啊";

//这代码是设置中间红字为红色
NSString *text1 = @"我是红色吗";
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:text1];
[attributeString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor],	NSFontAttributeName : [UIFont systemFontOfSize:17]} range:NSMakeRange(2, 1)];
myLabel.attributedText = attributeString;
[myLabel release];


UIButton

//这里创建一个圆角矩形的按钮
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

//    能够定义的button类型有以下6种,
//    typedef enum {
//        UIButtonTypeCustom = 0,          自定义风格
//        UIButtonTypeRoundedRect,         圆角矩形
//        UIButtonTypeDetailDisclosure,    蓝色小箭头按钮,主要做详细说明用
//        UIButtonTypeInfoLight,           亮色感叹号
//        UIButtonTypeInfoDark,            暗色感叹号
//        UIButtonTypeContactAdd,          十字加号按钮
//    } UIButtonType;

//给定在view上的位置
button1.frame = CGRectMake(20, 20, 280, 20);
//button背景色
button1.backgroundColor = [UIColor clearColor];
//设置button填充图片
//[button1 setImage:[UIImage imageNamed:@"btng.png"] forState:UIControlStateNormal];
//设置button标题
[button1 setTitle:@"点击" forState:UIControlStateNormal];
/* forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现*/
//以下是几种状态
//    enum {
//        UIControlStateNormal       = 0,         常规状态显现
//        UIControlStateHighlighted  = 1 << 0,    高亮状态显现
//        UIControlStateDisabled     = 1 << 1,    禁用的状态才会显现
//        UIControlStateSelected     = 1 << 2,    选中状态
//        UIControlStateApplication  = 0x00FF0000, 当应用程序标志时
//        UIControlStateReserved     = 0xFF000000  为内部框架预留,可以不管他
//    };

/*
* 默认情况下,当按钮高亮的情况下,图像的颜色会被画深一点,如果这下面的这个属性设置为no,
* 那么可以去掉这个功能
*/
button1.adjustsImageWhenHighlighted = NO;
/*跟上面的情况一样,默认情况下,当按钮禁用的时候,图像会被画得深一点,设置NO可以取消设置*/
button1.adjustsImageWhenDisabled = NO;
/* 下面的这个属性设置为yes的状态下,按钮按下会发光*/
button1.showsTouchWhenHighlighted = YES;

/* 给button添加事件,事件有很多种,我会单独开一篇博文介绍它们,下面这个时间的意思是
按下按钮,并且手指离开屏幕的时候触发这个事件,跟web中的click事件一样。
触发了这个事件以后,执行butClick:这个方法,addTarget:self 的意思是说,这个方法在本类中
也可以传入其他类的指针*/
[button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
//显示控件
[self.view addSubview:button1];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: