1.设置UIButton的按钮内容
//设置自定义的按钮//UIButton *button1=[UIButton buttonWithType:UIButtonTypeCustom];//设置一个圆角的按钮UIButton *button1=[UIButton buttonWithType:UIButtonTypeRoundedRect];button1.frame=CGRectMake(80,250,250, 30);//按钮的位置坐标[button1 setTitle:@"Button1" forState:UIControlStateNormal];//普通状态按钮标题[button1 setTitle:@"高亮状态" forState:UIControlStateHighlighted];//高亮状态的按钮标题//高亮状态光晕效果[button1 setShowsTouchWhenHighlighted:YES];//设置标题的颜色[button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];//设置标题的字体大小[button1.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];//设置背景颜色[button1 setBackgroundColor:[UIColor blueColor]];//图片被拉伸式地设置背景图片[button1 setBackgroundImage:[UIImage imageNamed:@"1.jpg"] forState:UIControlStateNormal];//图片保持原来大小地设置背景图片//[button1 setImage:[UIImage imageNamed:@"1.jpg"] forState:UIControlStateNormal];[[button1 titleLabel]setShadowColor:[UIColor blackColor]];[[button1 titleLabel]setShadowOffset:CGSizeMake(-0.5, -0.5)];button1.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;[self.view addSubview:button1];//监听事件[button1 addTarget:self action:@selector(Click_Button) forControlEvents:UIControlEventTouchUpInside];}-(void)Click_Button{NSLog(@"已点击...");}
2.设置UIImageView的图片内容
1.实例化控件 UIImageView *img = [[UIImageView alloc] init]; //简写UIImageView *img = [UIImageView new]; 2.设置控件内容,后面的background是图片名称,只需拖进项目的Assets文件即可 img.image = [UIImage imageNamed:@"background"]; 3.设置控件的frame(坐标) img.frame = CGRectMake(0, 0, 100, 100); 4.添加在父控件 [self.view addSubView:img];
2.3 使用transform属性imageView.transform = CGAffineTransformMakeTranslation(CGFloat dx, CGFloat dy); 其中dx与dy表示想要往x或者y方向移动多少,而不是移动到多少。3、旋转图像imageView.transform = CGAffineTransformMakeRotation(CGFloat angle); 要注意它是按照顺时针方向旋转的,而且旋转中心是原始ImageView的中心,也就是center属性表示的位置。 这个方法的参数angle的单位是弧度,而不是我们最常用的度数,所以可以写一个宏定义: #define degreesToRadians(x) (M_PI*(x)/180.0)4、缩放图像 还是使用transform属性:imageView.transform = CGAffineTransformMakeScale(CGFloat scale_w, CGFloat scale_h);
5、为图片添加单击事件:imageView.userInteractionEnabled = YES;//使控件实现交互UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];[imageView addGestureRecognizer:singleTap];一定要先将userInteractionEnabled置为YES,这样才能响应单击事件6.其他设置imageView.hidden = YES或者NO; // 隐藏或者显示图片imageView.alpha = (CGFloat) al; // 设置透明度imageView.highlightedImage = (UIImage *)hightlightedImage; // 设置高亮时显示的图片imageView.image = (UIImage *)image; // 设置正常显示的图片[imageView sizeToFit]; // 将图片尺寸调整为与内容图片相同
3.设置UILabel的属性内容
设置label的标记(tag)label.tag =101;设置label的文本内容label.text =@"abcd"设置label的文字类型与大小label.font = [UIFont systemFontOfSize:12];//采用系统默认文字设置大小 label.font = [UIFont fontWithName:@"Arial" size:30];//设置文字类型与大小 设置label的文字颜色label.textColor = [UIColor lightGrayColor];//其中textColor要用UIColor类型 设置文本的对齐方式label.textAlignment = NSTextAlignmentLeft; 其中textAlignment有三种设置方式:NSTextAlignmentLeft为向左对齐,NSTextAlignmentCenter为居中对齐,NSTextAlignmentRight为向右对齐如果有一些文章介绍时用的是UITextAlignmentCenter/UITextAlignmentLeft/UITextAlignmentRight,那是iOS6以前的用法,iOS6的最新用法已改当文本内容很多,label无法全部显示时label会将文本内容以省略号的方式代替,下面说一下label文本省略方式的设置label.lineBreakMode =NSLineBreakByCharWrapping;//其中lineBreakMode可选值为 linBreakMode enum{ NSLineBreakByWordWrapping = 0,//保留整个单词,以空格为边界 NSLineBreakByCharWrapping,//保留整个字符 NSLineBreakByClipping,//以边界为止 NSLineBreakByTruncatingHead,//省略开头,以省略号代替 NSLineBreakByTruncatingTail,//省略结尾,以省略号代替 NSLineBreakByTruncatingMiddle//省略中间,以省略号代替 } 设置文本的行数label.numberOfLines = 0;//行数设置为1,不设置时系统会默认行数为1,设置为0时,表示自动换行,但需要给定控件宽度设置控件自适应内容的大小[label sizeToFit]; 设置label的边框粗细与颜色,设置前要在相应文件中加入#import<QuartzCore/QuartzCore.h>label.layer.borderColor = [UIColor lightGrayColor].CGColor;//边框颜色,要为CGColor label.layer.borderWidth = 1;//边框宽度 设置label的背景颜色label.backgroundColor =[UIColor yellowColor];设置背景图可以把一张大小与label一样的图放在label的后面一层,然后把label的背景设置为透明,这样实现label有背景UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 200, 400)]; UIImageView *imageView =[[UIImageView alloc]init]; imageView.frame =CGRectMake(50, 50, 200, 400); UIImage *image=[UIImage imageNamed:@"1.jpg"]; imageView.image =image;//imageView会根据自身大小改变添加的图片的大小所以不需要额外设置image label.backgroundColor = [UIColor clearColor]; label.text =@"hello world"; label.font = [UIFont systemFontOfSize:30]; label.textColor = [UIColor yellowColor]; [self.view addSubview:imageView];//添加的顺序不能错,否则图片会覆盖label [self.view addSubview:label]; 设置文本阴影label.shadowColor =[UIColor grayColor]; 设置阴影大小label.shadowOffset = CGSizeMake(2.0, 2.0);设置label圆角label.layer.cornerRadius = 10; 要是用这样的设置要先在头文件中加上#import<QuartzCore/QuartzCore.h>
4.设置TextField的属性内容
定义一个TextFielduserNameField = [[UITextField alloc] initWithFrame:CGRectMake(userNameImg.frame.origin.x+30,userNameImg.frame.origin.y, 165, 40)]; userNameField.placeholder = @"User Name";userNameField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"手机号码" attributes:@{ NSForegroundColorAttributeName:placeHolderColor}];上面这句是设置placeHolder的颜色,placeHolderColor是自己需要的颜色 userNameField.backgroundColor = [UIColor clearColor]; userNameField.delegate = self; userNameField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; userNameField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; userNameField.borderStyle = UITextBorderStyleNone; userNameField.font = [UIFont systemFontOfSize:14.0]; [self.view addSubview:userNameField];//设置边框样式,只有设置了才会显示边框样式 text.borderStyle = UITextBorderStyleRoundedRect; typedef enum { UITextBorderStyleNone, UITextBorderStyleLine, UITextBorderStyleBezel, UITextBorderStyleRoundedRect } UITextBorderStyle; //设置输入框的背景颜色,此时设置为白色如果使用了自定义的背景图片边框会被忽略掉 text.backgroundColor = [UIColor whiteColor];//设置背景 text.background = [UIImage imageNamed:@"dd.png"];//设置背景 text.disabledBackground = [UIImage imageNamed:@"cc.png"];//当输入框没有内容时,水印提示提示内容为password text.placeholder = @"password";//设置输入框内容的字体样式和大小 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.secureTextEntry = YES;//是否纠错 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.keyboardType = UIKeyboardTypeNumberPad;typedef enum { UIKeyboardTypeDefault, 默认键盘,支持所有字符 UIKeyboardTypeASCIICapable, 支持ASCII的默认键盘 UIKeyboardTypeNumbersAndPunctuation, 标准电话键盘,支持+*#字符 UIKeyboardTypeURL, URL键盘,支持.com按钮 只支持URL字符UIKeyboardTypeNumberPad, 数字键盘UIKeyboardTypePhonePad, 电话键盘 UIKeyboardTypeNamePhonePad, 电话键盘,也支持输入人名UIKeyboardTypeEmailAddress, 用于输入电子 邮件地址的键盘 UIKeyboardTypeDecimalPad, 数字键盘 有数字和小数点 UIKeyboardTypeTwitter, 优化的键盘,方便输入@、#字符 UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, } UIKeyboardType; //首字母是否大写 text.autocapitalizationType = UITextAutocapitalizationTypeNone;typedef enum { UITextAutocapitalizationTypeNone, 不自动大写 UITextAutocapitalizationTypeWords, 单词首字母大写 UITextAutocapitalizationTypeSentences, 句子的首字母大写 UITextAutocapitalizationTypeAllCharacters, 所有字母都大写} UITextAutocapitalizationType;