1、初始化UILabel
label = [[UILabel alloc] initWithFrame:CGRectMake(60,100,300,200)];
label.textAlignment =UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.text =@"Hello";
label.font = [UIFont systemFontOfSize:50];
label.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"ELBackgroundImage"]];
label.shadowColor = [UIColor yellowColor];
[self.viewaddSubview:label]
2、详细参数解释
//设置显示文字
label.text=@"Hello";
//设置字体
label.font= [UIFont systemFontOfSize:50];
//设置字体颜色,也可以用图片
label.textColor= [UIColor colorWithPatternImage:[UIImage imageNamed:@"ELBackgroundImage"]];
label.textColor= [UIColor yellowColor];
//设置文字对齐位置,居左,居中,居右
label.textAlignment=UITextAlignmentCenter;
//设置字体大小是否适应label宽度
label.adjustsFontSizeToFitWidth=YES;
//设置label的行数
label.numberOfLines=2;//设置Label的最大显示行数(0表示不限)
//设置文本是否高亮
label.highlighted=YES;
//设置高亮时的颜色
label.highlightedTextColor= [UIColor orangeColor];
//设置阴影的颜色
label.shadowColor= [UIColor redColor];
//设置阴影的偏移位置
label.shadowOffset=CGSizeMake(1.0,1.0);
//设置是否能与用户进行交互
label.userInteractionEnabled=YES;
//设置label中的文字是否可变,默认值是YES
label.enabled=NO;
//设置文字过长时的显示格式
label.lineBreakMode=UILineBreakModeMiddleTruncation; //截去中间
在定义里面允许有以下格式显示:
typedef enum{
UILineBreakModeWordWrap = 0, // Wrap at word boundaries
UILineBreakModeCharacterWrap, // Wrap at character
UILineBreakModeClip, // 截去多余部分
UILineBreakModeHeadTruncation, // 截去头部
UILineBreakModeTailTruncation, // 截去尾部
UILineBreakModeMiddleTruncation, //截去中间
} UILineBreakMode;
如果adjustsFontSizeToFitWidth = YES;这个属性就来控制文本基线的行为
label.baselineAdjustment=UIBaselineAdjustmentNone;
在定义里面允许有以下格式显示
typedef enum{
UIBaselineAdjustmentAlignBaselines = 0,
UIBaselineAdjustmentAlignCenters,
UIBaselineAdjustmentNone,
} UIBaselineAdjustment;
//设置背景为透明
label.backgroundColor= [UIColor clearColor];
也可以使用自定义的颜色:
label.backgroundColor= [UIColorcolorWithRed:1.0fgreen:50.0fblue:0.0falpha:1.0f];//UIColor里的RGB值是CGFloat类型的在0~1范围内,对应0~255的颜色值范围
二、UITextField
1、初始化UITextField
_textField = [[UITextField alloc]initWithFrame:CGRectMake(70,150,200,40)];
_textField.autocapitalizationType =UITextAutocapitalizationTypeNone;//首字母是否大写
_textField.placeholder =@"请输入帐号";
_textField.backgroundColor = [UIColor clearColor];
_textField.keyboardAppearance =UIKeyboardAppearanceAlert;//键盘外观
_textField.contentVerticalAlignment =UIControlContentVerticalAlignmentCenter;//内容垂直对齐方式
_textField.textAlignment =UITextAlignmentCenter;//内容对齐方式
_textField.autocorrectionType =UITextAutocorrectionTypeDefault;//是否纠错
_textField.keyboardType =UIKeyboardTypeDefault;//设置键盘样式
_textField.clearButtonMode =UITextFieldViewModeWhileEditing;//输入框中是否有叉号,在什么时候显示,用于一次性删除输入框中的内容
_textField.borderStyle =UITextBorderStyleRoundedRect;//样式设置为圆角矩形
_textField.returnKeyType =UIReturnKeyDone;//return键变成什么键
_textField.delegate =self;//设置代理用于实现协议
_textField.secureTextEntry =YES;//每输入一个字符就变成点,用于密码输入
_textField.clearsOnBeginEditing =YES;//下次编辑时清除内容;
//最左侧加图片
UIImageView *imgv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon-iwant-2.png"]];
_textField.leftView = imgv;
_textField.leftViewMode =UITextFieldViewModeWhileEditing;
_textField.minimumFontSize =10;
_textField.adjustsFontSizeToFitWidth =YES;//设置为YES时文本会自动缩小以适应文本窗口大小。默认是保持原来大小,而让长文本滚动
[self.viewaddSubview:_textField];
2、详细参数解释
键盘类型 :
typedef enum {
UIKeyboardTypeDefault, //默认键盘,支持所有字符
UIKeyboardTypeASCIICapable, //支持ASCII的默认键盘
UIKeyboardTypeNumbersAndPunctuation, //标准电话键盘,支持+*#字符
UIKeyboardTypeURL, //URL键盘,支持.com按钮 只支持URL字符
UIKeyboardTypeNumberPad, //数字键盘
UIKeyboardTypePhonePad, //电话键盘
UIKeyboardTypeNamePhonePad, //电话键盘,也支持输入人名
UIKeyboardTypeEmailAddress, //用于输入电子邮件地址的键盘
UIKeyboardTypeDecimalPad, //数字键盘 有数字和小数点
UIKeyboardTypeTwitter, //优化的键盘,方便输入@、#字符
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
} UIKeyboardType;
首字母是否大写
typedef enum {
UITextAutocapitalizationTypeNone, //不自动大写
UITextAutocapitalizationTypeWords, //单词首字母大写
UITextAutocapitalizationTypeSentences, //句子的首字母大写
UITextAutocapitalizationTypeAllCharacters, //所有字母都大写
} UITextAutocapitalizationType;
键盘外观
typedef enum {
UIKeyboardAppearanceDefault, //默认外观,浅灰色
UIKeyboardAppearanceAlert, //深灰 石墨色
} UIKeyboardAppearanceType;
typedef enum {
UIReturnKeyDefault, //默认 灰色按钮,标有Return
UIReturnKeyGo, //标有Go的蓝色按钮
UIReturnKeyGoogle, //标有Google的蓝色按钮,用语搜索
UIReturnKeyJoin, //标有Join的蓝色按钮
UIReturnKeyNext, //标有Next的蓝色按钮
UIReturnKeyRoute, //标有Route的蓝色按钮
UIReturnKeySearch, //标有Search的蓝色按钮
UIReturnKeySend, //标有Send的蓝色按钮
UIReturnKeyYahoo, //标有Yahoo的蓝色按钮
UIReturnKeyEmergencyCall, //紧急呼叫按钮
} UIReturnKeyType;
3、委托方法
//返回一个BOOL值,指定textField是否可以编辑,返回NO则不许编辑
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return YES;
}
//当开始编辑时会调用的方法
-(void)textFieldDidBeginEditing:(UITextField *)textField
//返回YES允许编辑状态结束并且resignFirstResponder,返回NO不许编辑状态结束
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField;
//当textfield编辑结束时调用的方法
-(void)textFieldDidEndEditing:(UITextField *)textField
//按下Return按钮,键盘消失 _textField.delegate = self;_textField的代理是我,我去实现把键盘往下收的方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField
//clear button事件返回NO过滤之
-(BOOL)textFieldShouldClear:(UITextField *)textField
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//要防止文字被改变可以返回NO
//这个方法的参数中有一个NSRange对象,指明了被改变文字的位置
return YES;
}
4、UITextField可以直接设置leftView 或 rightView,直接看例子
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(5,10,30,20)];
nameLabel.text = @"昵称";
nameLabel.textColor = [UIColor blackColor];
nameLabel.backgroundColor = [UIColor clearColor];
nameLabel.font = [UIFont systemFontOfSize:14];
UITextField *nTextField = [[UITextField alloc] initWithFrame:CGRectMake(10,215,300,40)];
nTextField.borderStyle = UITextBorderStyleLine;
nTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
nTextField.delegate = self;
nTextField.contentVerticalAlignment =UIControlContentVerticalAlignmentCenter;
nTextField.contentHorizontalAlignment= UIControlContentHorizontalAlignmentCenter;
nTextField.textAlignment = UITextAlignmentLeft;
nTextField.keyboardType = UIKeyboardTypeDefault;
nTextField.leftView = nameLabel;
nTextField.leftViewMode = UITextFieldViewModeAlways;
rightView也是一样的设置方式,其中的mode有四种;
UITextFieldViewModeUnlessEditing; 除了编辑外都出现
UITextFieldViewModeNever; 从不出现
UITextFieldViewModeAlways; 一直出现
UITextFieldViewModeWhileEditing; 编辑时出现
5、回收键盘:
方法一:
- (void)viewDidLoad
{
[super viewDidLoad];
UIControl *backControl = [[UIControl alloc]initWithFrame:CGRectMake(0,0,320,480)];
backControl.backgroundColor = [UIColor clearColor];
[backControl addTarget:selfaction:@selector(tap)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:backControl];
[backControl release];
}
//键盘回收1
-(void)tap
{
[_textField resignFirstResponder];
}
方法二;
//键盘回收2
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[_textField resignFirstResponder];
}
6、通知
UITextField派生自UIControl,所以UIControl类中的通知系统在文本字段中也可以使用。除了UIControl类的标准事件,你还可以使用下列UITextField类特有的事件
UITextFieldTextDidBeginEditingNotification
UITextFieldTextDidChangeNotification
UITextFieldTextDidEndEditingNotification
当文本字段退出编辑模式时触发。通知的object属性存储了最终文本。
因为文本字段要使用键盘输入文字,所以下面这些事件发生时,也会发送动作通知
UIKeyboardWillShowNotification //键盘显示之前发送
UIKeyboardDidShowNotification //键盘显示之后发送
UIKeyboardWillHideNotification //键盘隐藏之前发送
UIKeyboardDidHideNotification //键盘隐藏之后发送
7、代替输入
inputView //代替标准的系统键盘
inputAccessoryView //编辑时显示在系统键盘或用户自定义的inputView上面的视图
8、xib详细参数解释
1、Text :设置文本框的默认文本。
2、Placeholder : 可以在文本框中显示灰色的字,用于提示用户应该在这个文本框输入什么内容。当这个文本框中输入了数据时,用于提示的灰色的字将会自动消失。
3、Background :
4、Disabled : 若选中此项,用户将不能更改文本框内容。
5、接下来是三个按钮,用来设置对齐方式。
6、Border Style : 选择边界风格。
7、Clear Button : 这是一个下拉菜单,你可以选择清除按钮什么时候出现,所谓清除按钮就是出一个现在文本框右边的小 X ,你可以有以下选择:
7.1 Never appears : 从不出现
7.2 Appears while editing : 编辑时出现
7.3 Appears unless editing :
7.4 Is always visible : 总是可见
8、Clear when editing begins : 若选中此项,则当开始编辑这个文本框时,文本框中之前的内容会被清除掉。比如,你现在这个文本框 A 中输入了 "What" ,之后去编辑文本框 B,若再回来编辑文本框 A ,则其中的 "What" 会被立即清除。
9、Text Color : 设置文本框中文本的颜色。
10、Font : 设置文本的字体与字号。
11、Min Font Size : 设置文本框可以显示的最小字体(不过我感觉没什么用)
12、Adjust To Fit : 指定当文本框尺寸减小时,文本框中的文本是否也要缩小。选择它,可以使得全部文本都可见,即使文本很长。但是这个选项要跟 Min Font Size 配合使用,文本再缩小,也不会小于设定的 Min Font Size 。
接下来的部分用于设置键盘如何显示。
13、Captitalization : 设置大写。下拉菜单中有四个选项:
13.1 None : 不设置大写
13.2 Words : 每个单词首字母大写,这里的单词指的是以空格分开的字符串
13.3 Sentances : 每个句子的第一个字母大写,这里的句子是以句号加空格分开的字符串
13.4 All Characters : 所以字母大写
14、Correction : 检查拼写,默认是 YES 。
15、Keyboard : 选择键盘类型,比如全数字、字母和数字等。
16、Appearance:
17、Return Key : 选择返回键,可以选择 Search 、 Return 、 Done 等。
18、Auto-enable Return Key : 如选择此项,则只有至少在文本框输入一个字符后键盘的返回键才有效。
19、Secure : 当你的文本框用作密码输入框时,可以选择这个选项,此时,字符显示为星号。
1.Alignment Horizontal 水平对齐方式
2.Alignment Vertical 垂直对齐方式
3.用于返回一个BOOL值 输入框是否 Selected(选中) Enabled(可用) Highlighted(高亮)