标签:
UILabel
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 20.0, 200.0, 50.0)]; //设置Label的位置和大小 //设置显示文字 label1.text = @"用户名"; //设置字体:粗体,正常的是 SystemFontOfSize label1.font = [UIFont boldSystemFontOfSize:20]; //设置文字颜色 label1.textColor = [UIColor orangeColor]; //设置文字位置 label1.textAlignment = UITextAlignmentRight; label2.textAlignment = UITextAlignmentCenter; //设置字体大小适应label宽度 label4.adjustsFontSizeToFitWidth = YES; //设置label的行数 label5.numberOfLines = 2; UIlabel.backgroudColor=[UIColor clearColor]; //可以去掉背景色 //设置高亮 label6.highlighted = YES; label6.highlightedTextColor = [UIColor orangeColor]; //设置阴影 label7.shadowColor = [UIColor redColor]; label7.shadowOf fset = CGSizeMake(1.0,1.0); //设置是否能与用户进行交互 label7.userInteractionEnabled = YES; //设置label中的文字是否可变,默认值是YES label3.enabled = NO; //设置文字过长时的显示格式 label3.lineBreakMode = UILineBreakModeMiddleTruncation;//截去中间 // typedef enum { // UILineBreakModeWordWrap = 0, // UILineBreakModeCharacterWrap, // UILineBreakModeClip,//截去多余部分 // UILineBreakModeHeadTruncation,//截去头部 // UILineBreakModeTailTruncation,//截去尾部 // UILineBreakModeMiddleTruncation,//截去中间 // } UILineBreakMode; //如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为 label4.baselineAdjustment = UIBaselineAdjustmentNone; // typedef enum { // UIBaselineAdjustmentAlignBaselines, // UIBaselineAdjustmentAlignCenters, // UIBaselineAdjustmentNone, // } UIBaselineAdjustment;
有时需要设置UILabel中文本的行数,其属性值默认为1,用于设置该UILabel只能显示一行文本。
oldPasswordLabel.numberOfLines = 2;
UITextField
//初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)]; //当输入框没有内容时,水印提示 ,提示内容为“用户名” //显示灰色字体,作为提示信息 text.placeholder = @"用户名"; //设置边框样式,只有设置了才会显示边框样式 text.borderStyle = UITextBorderStyleRoundedRect; typedef enum { UITextBorderStyleNone, UITextBorderStyleLine, UITextBorderStyleBezel, UITextBorderStyleRoundedRect } UITextBorderStyle; //设置键盘的样式 text.keyboardType = UIKeyboardTypeNumberPad; typedef enum { UIKeyboardTypeDefault, //默认键盘,支持所有字符 UIKeyboardTypeASCIICapable, //支持ASCII的默认键盘 UIKeyboardTypeNumbersAndPunctuation, //标准电话键盘,支持+*#字符 UIKeyboardTypeURL, //URL键盘,支持.com按钮 只支持URL字符 UIKeyboardTypeNumberPad, //数字键盘 UIKeyboardTypePhonePad, //电话键盘 UIKeyboardTypeNamePhonePad, //电话键盘,也支持输入人名 UIKeyboardTypeEmailAddress, //用于输入电子 邮件地址的键盘 UIKeyboardTypeDecimalPad, //数字键盘 有数字和小数点 UIKeyboardTypeTwitter, //优化的键盘,方便输入@、#字符 UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, } UIKeyboardType; //每输入一个字符就变成点 用语密码输入 text.secureTextEntry = YES; //设置输入框的背景颜色,此时设置为白色 如果使用了自定义的背景图片边框会被忽略掉 text.backgroundColor = [UIColor whiteColor]; //设置背景图片 text.background = [UIImage imageNamed:@"dd.png"]; //设置背景 text.disabledBackground = [UIImage imageNamed:@"cc.png"]; //设置输入框内容的字体样式和大小 text.font = [UIFont fontWithName:@"Arial" size:20.0f]; //设置字体颜色 text.textColor = [UIColor redColor]; //输入框中是否有个叉号,在什么时候显示,用于一次性删除输入框中的内容 text.clearButtonMode = UITextFieldViewModeAlways; typedef enum { UITextFieldViewModeNever, //从不出现 UITextFieldViewModeWhileEditing, //编辑时出现 UITextFieldViewModeUnlessEditing, //除了编辑外都出现 UITextFieldViewModeAlways //一直出现 } UITextFieldViewMode; //输入框中一开始就有的文字 text.text = @"一开始就在输入框的文字"; //是否纠错 text.autocorrectionType = UITextAutocorrectionTypeNo; typedef enum { UITextAutocorrectionTypeDefault, //默认 UITextAutocorrectionTypeNo, //不自动纠错 UITextAutocorrectionTypeYes, //自动纠错 } UITextAutocorrectionType; //再次编辑就清空 text.clearsOnBeginEditing = YES; //内容对齐方式 text.textAlignment = UITextAlignmentLeft; //内容的垂直对齐方式 UITextField继承自UIControl,此类中有一个属性contentVerticalAlignment text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; //设置为YES时文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动 textFied.adjustsFontSizeToFitWidth = YES; //设置自动缩小显示的最小字体大小 text.minimumFontSize = 20; //首字母是否大写 text.autocapitalizationType = UITextAutocapitalizationTypeNone; typedef enum { UITextAutocapitalizationTypeNone, 不自动大写 UITextAutocapitalizationTypeWords, 单词首字母大写 UITextAutocapitalizationTypeSentences, 句子的首字母大写 UITextAutocapitalizationTypeAllCharacters, 所有字母都大写 } UITextAutocapitalizationType; //return键变成什么键 text.returnKeyType =UIReturnKeyDone; typedef enum { UIReturnKeyDefault, //默认 灰色按钮,标有Return UIReturnKeyGo, //标有Go的蓝色按钮 UIReturnKeyGoogle, //标有Google的蓝色按钮,用语搜索 UIReturnKeyJoin, //标有Join的蓝色按钮 UIReturnKeyNext, //标有Next的蓝色按钮 UIReturnKeyRoute, //标有Route的蓝色按钮 UIReturnKeySearch, //标有Search的蓝色按钮 UIReturnKeySend, //标有Send的蓝色按钮 UIReturnKeyYahoo, //标有Yahoo的蓝色按钮 UIReturnKeyYahoo, //标有Yahoo的蓝色按钮 UIReturnKeyEmergencyCall, //紧急呼叫按钮 } UIReturnKeyType; //键盘外观 textView.keyboardAppearance=UIKeyboardAppearanceDefault; typedef enum { UIKeyboardAppearanceDefault, //默认外观,浅灰色 UIKeyboardAppearanceAlert, //深灰 石墨色 } UIReturnKeyType; //设置代理 用于实现协议 text.delegate = self; //把textfield加到视图中 [self.window addSubview:text]; //最右侧加图片是以下代码 左侧类似 UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]]; text.rightView=image; text.rightViewMode = UITextFieldViewModeAlways; typedef enum { UITextFieldViewModeNever, UITextFieldViewModeWhileEditing, UITextFieldViewModeUnlessEditing, UITextFieldViewModeAlways } UITextFieldViewMode;
在处理密码等隐私类的信息时,可能需要将输入的信息隐藏一下。
//每输入一个字符就变成点 ,用语密码输入 [passwordTextField setSecureTextEntry:YES];
也可以设置文本框关联的键盘,如下:
//设置键盘的样式 text.keyboardType = UIKeyboardTypeNumberPad; typedef enum { UIKeyboardTypeDefault, //默认键盘,支持所有字符 UIKeyboardTypeASCIICapable, //支持ASCII的默认键盘 UIKeyboardTypeNumbersAndPunctuation, //标准电话键盘,支持+*#字符 UIKeyboardTypeURL, //URL键盘,支持.com按钮 只支持URL字符 UIKeyboardTypeNumberPad, //数字键盘 UIKeyboardTypePhonePad, //电话键盘 UIKeyboardTypeNamePhonePad, //电话键盘,也支持输入人名 UIKeyboardTypeEmailAddress, //用于输入电子 邮件地址的键盘 UIKeyboardTypeDecimalPad, //数字键盘 有数字和小数点 UIKeyboardTypeTwitter, //优化的键盘,方便输入@、#字符 UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, } UIKeyboardType;
有时需要限制输入文本的长度,这类操作也非常普遍和重要。
//限制输入文本的长度 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if ([textField.text length] > MAXLENGTH) { textField.text = [textField.text substringToIndex:MAXLENGTH-1]; return NO; } return YES; }
UIButton
UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; confirmButton.frame = CGRectMake(110, 60+40*3+20, 100, 37); [confirmButton setTitle:@"确定" forState:UIControlStateNormal]; //正常状况下button显示的标题 [confirmButton setTitle:@"确定" forState:UIControlStateHighlighted]; //高亮显示时button的标题 confirmButton.backgroundColor = [UIColor redColor]; [confirmButton addTarget:self action:@selector(confirm:) forControlEvents:UIControlEventTouchUpInside];//button被按下又抬起后发生的事件 //@selector可以理解为"选择子",selector是一个指针变量,类似于sender。 这里是将method的方法指定给新建的这个confirmButton [self.view addSubview:confirmButton];
若要设置UIButton的背景图片时:
UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom]; confirmButton.frame = CGRectMake(10, 60, 100, 40); UIImage *nextStepImage = [UIImage imageNamed:@"app.png"]; UIImage *nextStepDownImage = [UIImage imageNamed:@"app.png"]; nextStepImage = [nextStepImage resizableImageWithCapInsets:UIEdgeInsetsMake(8, 8, 8, 8)]; nextStepDownImage = [nextStepDownImage resizableImageWithCapInsets:UIEdgeInsetsMake(8, 8, 8, 8)]; [confirmButton setBackgroundImage:nextStepImage forState:UIControlStateNormal]; [confirmButton setBackgroundImage:nextStepDownImage forState:UIControlStateHighlighted]; [confirmButton setTitle:@"确定" forState:UIControlStateNormal]; [confirmButton addTarget:self action:@selector(confirm:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:confirmButton];
标签:
原文地址:http://www.cnblogs.com/0618yn/p/5066711.html