內容:
一、UILabel
二、UITextField
三、UIButton
四、UIImageView
UILabel :
*先創建一個view畫紙containerView
//創建一個UILabel、標簽視圖
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(50, 100, 250, 100)]autorelease];
//設置label的背景顏色
label.backgroundColor = [UIColor redColor];
//設置label的文本
label.text = @"zhonger is a beautiful girl,she loves jinkangda";
//設置label的文本顏色
label.textColor = [UIColor whiteColor];
//設置label的文本對齊方式,默認左對齊
label.textAlignment = NSTextAlignmentCenter;
//設置label的字體,默認17
// label.font = [UIFont systemFontOfSize:20];
//加粗
label.font = [UIFont boldSystemFontOfSize:20];
//設置label多行顯示,默認是1,單行顯示
label.numberOfLines = 0;
//設置label的文本陰影的顏色和偏移量
label.shadowColor = [UIColor darkGrayColor];
//陰影向x正?向偏移2,向y正?向偏移1。
label.shadowOffset = CGSizeMake(2, 1);
//設置折行模式
label.lineBreakMode = NSLineBreakByWordWrapping;
[containerView addSubview:label];
運行結果.png
**使用添加的延展方法來添加一個標簽視圖:**
**AppDelegate.m中****:**
**1、聲明**
@interface AppDelegate ()
//創建UILabel
- (UILabel *)createLabelWithText:(NSString *)text frame:(CGRect)frame textColor:(UIColor*)textColor textAlignment:(NSTextAlignment)textAlignment numberOfLines:(NSInteger)numberOfLines font:(UIFont *)font;
@end
**2、實現**
@implementation AppDelegate
//實現UILabel
- (UILabel *)createLabelWithText:(NSString *)text frame:(CGRect)frame textColor:(UIColor*)textColor textAlignment:(NSTextAlignment)textAlignment numberOfLines:(NSInteger)numberOfLines font:(UIFont *)font {
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.text = text;
label.textColor = textColor;
label.textAlignment = textAlignment;
label.numberOfLines = numberOfLines;
label.font = font;
return [label autorelease];
}
**3、使用**
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
... ...
UILabel *label1 = [self createLabelWithText:@"豬八達點秋香" frame:CGRectMake(100, 300, 200,40) textColor:[UIColor magentaColor] textAlignment:NSTextAlignmentCenter numberOfLines:0 font:[UIFont boldSystemFontOfSize:21]];
[containerView addSubview:label1];
... ...
}
****
... ...
@end
UITextField:
//創建一個輸入框,UITextField
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 200,40)];
//告知用戶這個輸入框應該輸入的信息
textField.placeholder = @"請輸入一種花的名字:";
//設置輸入框的邊框樣式
textField.borderStyle = UITextBorderStyleRoundedRect;
//設置輸入框是否重新輸入的時候清空內容
textField.clearsOnBeginEditing = YES;
//設置密文輸入
textField.secureTextEntry = YES;
UITextInputTraits:
//設置鍵盤樣式
textField.keyboardType = UIKeyboardTypeNumberPad;
//設置鍵盤上的return鍵樣式
textField.returnKeyType = UIReturnKeyDone;
//設置鍵盤上的清除樣式模式
textField.clearButtonMode = UITextFieldViewModeAlways;
[containerView addSubview:textField];
[textField release];
運行結果.png
UITextField常?代理?法:
//當textField將要開始編輯的時候告訴委托?
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
//當textField已經編輯的時候告訴委托?
- (void)textFieldDidBeginEditing:(UITextField *)textField;
//當textField將要完成編輯的時候告訴委托?
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;
//當textField已經完成編輯的時候告訴委托?
- (void)textFieldDidEndEditing:(UITextField *)textField;
//將某個范圍內的字符替換為另一段字符
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
//當按下鍵盤上的清除鍵時告訴委托人
- (BOOL)textFieldShouldClear:(UITextField *)textField;
//當點擊鍵盤上回車按鍵時候告訴委托?
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
UIButton:
//創建一個UIButton對象
****
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//設置frame
button.frame = CGRectMake(100, 300, 200, 150);
//設置button顯示的文本(標題)
[button setTitle:@"一月" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
/*這里注意:
UIButtonTypeCustom 比 UIButtonTypeSystem更靈活
UIButtonTypeSystem會設置一些默認樣式,如,字體顏色為 藍色
即 UIButtonTypeSystem 不給定標題顏色也可顯示,但是 UIButtonTypeCustom 就不行了
*/
//設置了背景圖片為普通狀態,按鈕未被點擊時的狀態
[button setBackgroundImage:[UIImage imageNamed:@"xigua.png"] forState:UIControlStateNormal];
//設置了背景圖片為高亮狀態,按鈕被點擊時的狀態
[button setBackgroundImage:[UIImage imageNamed:@"BtnOff"] forState:UIControlStateHighlighted];
/*
這里注意:
UIControlStateHighlighted 高亮狀態下,點住不動才可顯示圖片
如果兩個狀態設置了不同圖片,點擊出現閃爍圖片改變效果
如果圖片是png格式的,后綴可不寫
*/
//設置了背景圖片,是否出現閃爍的效果,即是否出現點擊是的高亮狀態,默認YES
button.adjustsImageWhenHighlighted = NO;
//設置button的前景圖片
[button setImage:[UIImage imageNamed:@"xigua.png"] forState:UIControlStateNormal];
/*
這里注意:
設置button的背景圖片,不論圖片大小,都填充顯示,顯示的與button等大
設置button的前景圖片,如果圖片的大小大于設置的button的frame,那么圖片會被壓縮到與button等大,如果圖片大小小于button的frame,保持原大小
在UIButtonTypeCustom狀態下顯示原圖,在UIButtonTypeSystem狀態下顯示圖片輪廓
*/
//關鍵方法,為button添加一個事件
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[containerView addSubview:button];
/*
這里注意:
MVC,V即view,C即control
Button是UIView的子類,View不執行方法,交給Control去執行,解耦合
addTarget:(執行對象) action:(方法) forControlEvents:(點擊方式)
UIControlEventTouchUpInside 點擊方式為:點擊后松手
*/
UIImageView:
UIImageView是iOS中用于顯示圖片的類,iOS中幾乎所有看到的圖片,都是由這個類來顯示的。
Paste_Image.png
//每次使用都需要加載,不過使用完可以進行釋放,不會對內存造成太大的壓力,大圖片使用,不常用圖片
NSString *path = [[NSBundle mainBundle] resourcePath];
NSString *imagePath = [NSString stringWithFormat:@"%@/600.jpeg",path];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:imagePath];
//不需要每次加載,但是當程序結束時才釋放,小圖片使用,常用圖片
UIImage *image1 = [UIImage imageNamed:@"600.jpeg"];
//圖片不能直接顯示在屏幕上需要載體
//載體
UIImageView *imageView = [[UIImageView alloc] initWithImage:image1];
//圖片顯示在屏幕上的大小是由載體控制的
imageView.frame = CGRectMake(10, 10, 300, 500);
****
//內容模式
/*
默認效果:UIViewContentModeScaleToFill - 拉伸充滿整個載體
UIViewContentModeScaleAspectFill - 拉伸不改變比例,充滿最大的一邊
UIViewContentModeScaleAspectFit - 拉伸不改變比例,充滿最小的一邊
*/
imageView.contentMode = UIViewContentModeScaleAspectFill;
UIImageView的動態圖(實現動畫):
animationImages //設置一組動態圖片
animationDuration //設置播放一組動態圖片的時間
animationRepeatCount //設置重復次數
startAnimating //開始動畫
stopAnimating //結束動畫
//做動態圖
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 173, 173)];
//指定展示圖片
// imageView.image = [UIImage imageNamed:@"dog-26(被拖移).tiff"];
//UIImageView動畫 - 播放序列圖
NSMutableArray *mArr = [NSMutableArray arrayWithCapacity:0];
for (int i = 1; i < 30; i ++) {
//獲取圖片名稱
NSString *picStr = [NSString stringWithFormat:@"dog-%d(被拖移).tiff",i];
//獲取每一張圖片對象
UIImage *image = [UIImage imageNamed:picStr];
//將圖片添加到數組
[mArr addObject:image];
}
//設置動畫播放數組,指定做動畫的所有圖片
imageView.animationImages = mArr;
//指定動畫時間(秒)
imageView.animationDuration = 2.0;
//重復次數,默認為0,一直重復,,直到使用stopAnimating
imageView.animationRepeatCount = 0;
//開啟動畫
[imageView startAnimating];
[containerView addSubview:imageView];
[imageView release];