四大美女,,走起!!!
//一、UIlabel标签控件
//1.开辟空间并初始化
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
//2.设置属性
//1)背景颜色
[label setBackgroundColor:[UIColor magentaColor]];
//2)标题
[label setText:@"我一直都在rrrrrrrrrrrrrrrrrrrrrrrrrrrrrr"];
//3)字体加粗及大小
[label setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
//采用系统默认文字大小设置大小
//label setFont:[UIFont systemFontOfSize:12];
//4)标题位置
//标题居中
[label setTextAlignment:NSTextAlignmentCenter];
//向左对齐
[label setTextAlignment:NSTextAlignmentLeft];
//5)设置文本行数
//显示文本行数(限定行数),不设置时系统会默认行数为1
//当需要设置的行数为不限制数量的时候可以用numberOflines=0实现
//[label setNumberOfLines:2];
//设置文本多行显示
//[label setLineBreakMode:NSLineBreakByWordWrapping];
[label setNumberOfLines:0];
//6)字体阴影颜色
[label setShadowColor:[UIColor yellowColor]];
//7)阴影大小(阴影向X正方向偏移2,向y方向偏移1)
[label setShadowOffset:CGSizeMake(2, 1)];
//8)设置label的边框颜色
label.layer.borderColor=[UIColor blueColor].CGColor;
//9)设置label的边框宽度
label.layer.borderWidth=1;
//10)设置label圆角
label.layer.cornerRadius=10;
//3.添加到视图
[self.view addSubview:label];
- label.tag =101;
其中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大小使用sizeToFit方法,调整大小时会考虑到该属性中存储的值。例如,如果此属性设置为3,sizeToFit方法会调整label使它大到足以显示三行文本。
- [label sizeToFit];
- label.numberOfLines =1;
- label.adjustsFontSizeToFitWidth =YES;
如果行数是超过了1行,要实现自动调整字体大小功能,就没有可以自适应的系统方法可以使用,只有自己用代码实现,在设计时因为要考虑到手机屏幕的实际大小有限,如果字体太小会影响用户体验,所以要设置一个最小字号的判断,小于最小字号就要用到缩略显示,下面的代码中主要是用到
- CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(100, 180) lineBreakMode:NSLineBreakByCharWrapping];
来得到字体在某一字号下的高度,判断与label高度是否一致,其中text是输入label的文本内容,sizWithFont设置字体,constrainedToSize设置约束文本的矩形大小参数,其中宽度要和label一致,高度设置要足够高,要比label高很多,否则会出现文本显示不全的问题,lineBreakMode的作用上文有讲过。如果算出的高度超出了label高度,就把字号以循环的方式减小直到高度符合就跳出循环。
- float maxHeight =50;//设置最大高度
- float minFontSize =9;
- float height;
- int fontSize = 31;//设置最大字号
- NSString *text = @"输入文本内容";
- do {
- fontSize = fontSize - 1;
- UIFont *font =[UIFont fontWithName:@"Arial" size:fontSize];
- CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(100, 180)/*宽度与label的宽度一样,高度应高于label高度*/ lineBreakMode:NSLineBreakByCharWrapping];
- height = size.height;
- NSLog(@"height=%f,fontSize=%d,text=%@",height,fontSize,text);
- } while (height > maxHeight&&fontSize>minFontSize);
- UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 50)];
- label.text =text;
- if (fontSize ==9) {//判断字体是否小于最小字号,小于最小字号时就使用系统默认的缩略显示
- label.font = [UIFont fontWithName:@"Arial" size:15];
- }
- else{
- label.font = [UIFont fontWithName:@"Arial" size:fontSize];
- label.lineBreakMode = NSLineBreakByCharWrapping;//实现文字多行显示
- label.numberOfLines = 0;
- }
- [self.view addSubview:label];
其实就是用上面的方法得到高度再生成label
- NSString *text =[[NSString alloc]init];
- text = @"输入文本内容";
- CGSize size = CGSizeMake(280, 180);
- UIFont *fonts = [UIFont systemFontOfSize:14.0];
- CGSize msgSie = [text sizeWithFont:fonts constrainedToSize:size lineBreakMode: NSLineBreakByCharWrapping];
- UILabel *textLabel = [[UILabel alloc] init];
- [textLabel setFont:[UIFont boldSystemFontOfSize:14]];
- textLabel.frame = CGRectMake(20,70, 280,msgSie.height);
- textLabel.text = text;
- textLabel.lineBreakMode = NSLineBreakByCharWrapping;//实现文字多行显示
- textLabel.numberOfLines = 0;
- [self.view addSubview:textLabel];
设置背景图有两种方法,下面先介绍第一种方法:
设置背景图可以把一张大小与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];
- UIColor * color = [UIColor colorWithPatternImage:image];//image为需要添加的背景图
- UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 200)];
- [label setBackgroundColor:color];
- [self.view addSubview:label];
- -(UIImage *)scaleImage:(UIImage *)img ToSize:(CGSize)itemSize{
- UIImage *i;
- // 创建一个bitmap的context,并把它设置成为当前正在使用的context
- UIGraphicsBeginImageContext(itemSize);
- CGRect imageRect=CGRectMake(0, 0, itemSize.width, itemSize.height);
- // 绘制改变大小的图片
- [img drawInRect:imageRect];
- // 从当前context中创建一个改变大小后的图片
- i=UIGraphicsGetImageFromCurrentImageContext();
- // 使当前的context出堆栈
- UIGraphicsEndImageContext();
- // 返回新的改变大小后的图片
- return i;
- }
- CGSize size= CGSizeMake(100, 200);
- UIImage *image =[UIImage imageNamed:@"1.jpg"];
- UIImage *laterImage =[self scaleImage:image ToSize:size];
- UIColor * color = [UIColor colorWithPatternImage:laterImage];
- UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 200)];
- [label setBackgroundColor:color];
- [self.view addSubview:label];
二、UITextField (输入框)
//使用初始化创建对象
UITextField * field=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];
[field setBackgroundColor:[UIColor cyanColor]];
//设置边框风格
field.borderStyle=UITextBorderStyleRoundedRect;
//设置占位符
field.placeholder=@"请输入账号";
//弹出键盘
//数字键盘
// [file setKeyboardType:UIKeyboardTypeDecimalPad];
// 谷歌键盘
// [file setKeyboardType:UIReturnKeyGoogle];
//键盘右下角return按键类型(枚举值)
field returnKeyType=UIReturnKeyGoogel;
//重置(总是显示清除按钮)
[field setClearButtonMode:UITextFieldViewModeAlways];
//密码安全性(以点显示)
field.secureTextEntry=YES;
[self.view addSubview:field];
[field setDelegate:self];
//使用UITextFieldDelegate来隐藏键盘
//在iPhone界面上,时常会需要当用户输入完内容后,隐藏键盘。 当然有很多方法,今天只介绍使用UITextFieldDelegate这个协议实现隐藏键盘。
1. 在你的控制器类中,加入UITextFieldDelegate这个协议
如:@interface AddItemViewController : UIViewController <UITextFieldDelegate>
2. 在使用了UITextFieldDelegate协议的控制器类的实现中,加入- (BOOL)textFieldShouldReturn:方法。
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
//如何限制文本框只能输入数字:
//建立NSNumberFormatter的子类,增加这个方法,将formatter链接至文本框。
- (BOOL) isPartialStringValid: (NSString **) partialStringPtr
proposedSelectedRange: (NSRangePointer) proposedSelRangePtr
originalString: (NSString *) origString
originalSelectedRange: (NSRange) origSelRange
errorDescription: (NSString **) error
{
NSCharacterSet *nonDigits;
NSRange newStuff;
NSString *newStuffString;
nonDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
newStuff = NSMakeRange(origSelRange.location,
proposedSelRangePtr->location
- origSelRange.location);
newStuffString = [*partialStringPtr substringWithRange: newStuff];
if ([newStuffString rangeOfCharacterFromSet: nonDigits
options: NSLiteralSearch].location != NSNotFound) {
*error = @"不是数字";
return (NO);
} else {
*error = nil;
return (YES);
}
}
//点击 UITextView 输入文字,光标都从最初点开始
- (void)textViewDidChangeSelection:(UITextView *)textView
{
NSRange range;
range.location = 0;
range.length = 0;
textView.selectedRange = range;
}
//限制输入文本的长度
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (range.location >= MAX_LENGTH)
return NO; // return NO to not change text
return YES;
}
if (textField.text.length >= 10 && range.length == 0)
return NO;
return YES;
- (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 *button=[UIButton buttonWithType:UIButtonTypeSystem];
[button setFrame:CGRectMake(100, 200, 100, 100)];
//button的背景颜色
//[button setBackgroundColor:[UIColor yellowColor]];
// 设置button的标题
[button setTitle:@"登录" forState:UIControlStateNormal];
// 设置button标题的颜色
[button setTitleColor:[UIColor magentaColor] forState:UIControlStateNormal];
// 设置按钮按下会发光
[button setShowsTouchWhenHighlighted:YES];
//点击事件(button 、control)
//Target_action目标——动作模式,在两个对象间直接发送消息 self指的是当前对象 点击它后,它指定了一个target(目标对象),并执行目标对象上指定的action(方法:click点击)。
//添加点击事件
[button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
//设置按钮的背景图片
[button setBackgroundImage:[UIImage imageNamed:@"4.jpg"] forState:UIControlStateNormal];
[self.view addSubview:button];
}
//实现点击事件方法
-(void)click{
}
//四、UIImageView (图片)
//初始化
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 350)];
//直接显示图片
// [imageView setImage:[UIImage imageNamed:@"4.jpg"]];
//通过路劲显示图片
NSString *path=[[NSBundle mainBundle]pathForResource:@"3" ofType:@"jpg"];
[imageView setImage:[UIImage imageWithContentsOfFile:path]];
//设置圆角
imageView.layer.masksToBounds=YES;
imageView.layer.cornerRadius=10;
//设置边框颜色和大小
imageView.layer.borderColor=[UIColor yellowColor].CGColor;
imageView.layer.borderWidth=3;
[self.view addSubview:imageView];
//自动播放一系列图片
//1.图片添加到组
UIImage *image1=[UIImage imageNamed:@"1.jpg"];
UIImage *image2=[UIImage imageNamed:@"2.jpg"];
UIImage *image3=[UIImage imageNamed:@"3.jpg"];
UIImage *image4=[UIImage imageNamed:@"4.jpg"];
UIImage *image5=[UIImage imageNamed:@"5.jpg"];
NSArray *imageArray=@[image1,image2,image3,image4,image5];
//用for循环的方式把图片放入数组
// NSMutableArray *imageArray=[[NSMutableArray alloc]init];
// for (int i=0; i<5; i++) {
// NSString *filename=[NSString stringWithFormat:@"%d.jpg",i];
/*
//另一种获取图片路径方法
// NSString *path=[NSString stringWithFormat:@"%ld.jpg",a++];
[image setImage:[UIImage imageNamed:path]];
*/// NSString *path=[[NSBundle mainBundle]pathForResource:filename ofType:nil];
// UIImage *image=[UIImage imageWithContentsOfFile:path];
// [imageArray addObject:image];
//2.设置一组动态图
imageView.animationImages=imageArray;
//3.设定播放一组动态图片的时间
imageView.animationDuration=[imageArray count];
//4.设置重复次数(0表示无数次)
imageView.animationRepeatCount=0;
//5.开始播放
[imageView startAnimating];
//为图片添加单击事件:一定要先将userInteractionEnabled置为YES,这样才能响应单击事件
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizeralloc] initWithTarget:self action:@selector(tapImageView:)];
[imageView addGestureRecognizer:singleTap];
// 隐藏或者显示图片
// imageView.hidden = YES或者NO;
// 设置透明度
imageView.alpha =0.5;
// 设置高亮时显示的图片
//imageView.highlightedImage = (UIImage *)hightlightedImage;
// 设置正常显示的图片
//imageView.image = (UIImage *)image;
*******注意注意:
其中第一 二种属于一种,共两种:
1)用imageNamed的方式加载时,系统会把图像Cache到内存。如果图像比较大,或者图像比较多,用这种方式会消耗很大的内存,而且释放图像的内存是一件相对来说比较麻烦的事情。例如:如果利用imageNamed的方式加载图像到一个动态数组NSMutableArray,然后将将数组赋予一个UIView的对象的animationImages进行逐帧动画,那么这将会很有可能造成内存泄露。并且释放图像所占据的内存也不会那么简单。但是利用imageNamed加载图像也有自己的优势。对于同一个图像系统只会把它Cache到内存一次,这对于图像的重复利用是非常有优势的。例如:你需要在一个TableView里重复加载同样一个图标,那么用imageNamed加载图像,系统会把那个图标Cache到内存,在Table里每次利用那个图像的时候,只会把图片指针指向同一块内存。这种情况使用imageNamed加载图像就会变得非常有效。