添加按钮:
UIButton * btn = [UIButton buttonWithType:(UIButtonTypeRoundedRect)];
btn.frame = CGRectMake(100, 100, 100, 100);
[btn addTarget:self action:@selector(btnPressed:) forControlEvents:(UIControlEventTouchUpInside)];
[btn setTitle:@"确定" forState:(UIControlStateNormal)];
[self.view addSubview:btn];
//点击按钮时有高亮效果
btn.showsTouchWhenHighlighted = YES;
添加label:
UILabel * lbl = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 44)];
lbl.text = @"test";
lbl.textColor = [UIColor purpleColor];
lbl.font = [UIFont systemFontOfSize:18];
lbl.backgroundColor = [UIColor clearColor];
[self.view addSubview:lbl];
//======lable的frame根据text的不同变化
str = @"123456789";
lbl.text = str;
lbl.numberOfLines = 0;
CGSize size = [str sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(90, 100) lineBreakMode:(NSLineBreakByWordWrapping)];
lbl.frame = CGRectMake(100, 16, 80, size.height);
添加view:
UIView * aView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 80)];
aView.backgroundColor = [UIColor purpleColor];
[self.view addSubview:aView];
view为圆角:
需要:#import <QuartzCore/QuartzCore.h>
aView.layer.cornerRadius = 10;
给view添加手势:
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture)];
[self.view addGestureRecognizer:tap];
添加textField以及delegate方法:
需要:UITextFieldDelegate协议
UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(0, 50, 320, 44)];
text.backgroundColor = [UIColor purpleColor];
text.placeholder = @"请输入:";
text.borderStyle = UITextBorderStyleRoundedRect;
text.font = [UIFont systemFontOfSize:18];
text.textAlignment = NSTextAlignmentLeft;
//[text setContentMode:UIViewContentModeCenter];
[text setContentVerticalAlignment:(UIControlContentVerticalAlignmentCenter)];//内容在中间,上下
text.delegate = self;
[self.view addSubview:text];
#pragma mark - textField delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
textField的边框宽度和边框颜色:
需要:#import <QuartzCore/QuartzCore.h>
text.layer.borderWidth = 1;
text.layer.borderColor = [UIColor redColor].CGColor;
定义size大小:
CGSize size = [str sizeWithFont:[UIFont systemFontOfSize:20] constrainedToSize:CGSizeMake(320, 1000) lineBreakMode:(NSLineBreakByWordWrapping)];
播放音乐:
需要添加AVFoundation.framework框架
需要:#import <AVFoundation/AVFoundation.h>
NSURL *url = [NSURL URLWithString:@"http://zhangmenshiting.baidu.com/data2/music/903505/8930817375600128.mp3?xcode=750b4fdc9f5eeb0954dfe54c6e23e3be24b7b177cb0d29e2&song_id=89308173"];
NSData *urlData = [NSData dataWithContentsOfURL:url];
NSError *error = nil;
AVAudioPlayer * player = [[[AVAudioPlayer alloc]initWithData:urlData error:&error] autorelease];
//准备播放音乐
[player prepareToPlay];
[player play];
添加风火轮:
需要添加 QuartzCore.framework框架
需要:#import <QuartzCore/QuartzCore.h>
UIActivityIndicatorView * active = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleWhiteLarge)];
active.backgroundColor = [UIColor grayColor];
active.alpha = 0.5;
active.layer.cornerRadius = 10;
active.frame = CGRectMake(100, 100, 80, 80);
[self.view addSubview:active];
[active startAnimating];
添加scrollView:
UIScrollView * scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];
scrollView.backgroundColor = [UIColor orangeColor];
scrollView.contentSize = CGSizeMake(320 * 4, 460);
scrollView.pagingEnabled = YES;
[self.view addSubview:scrollView];
scrollView 显示哪一页:
[self.scrollView setContentOffset:CGPointMake(3 * 320, 0) animated:YES];