這一章的目標是把手上的 UIKit 元件摸熟:先回顧窗口與視圖,再逐一認識標籤、文字框、按鈕、開關、滑桿與分段控制項。
依用途分類會比死背名稱有效:
CGRect labelFrame = CGRectMake(0, 10, 100, 50); UILabel *myLabel = [[UILabel alloc] initWithFrame:labelFrame]; myLabel.backgroundColor = [UIColor clearColor]; myLabel.textColor = [UIColor redColor]; myLabel.font = [UIFont fontWithName:@"Verdana" size:18.0]; myLabel.numberOfLines = 2; myLabel.text = @"Hello World\nSecond line"; [self.view addSubview:myLabel]; [myLabel release];
CGRect textRect = CGRectMake(10, 10, 300, 31); UITextField *myTextField = [[UITextField alloc] initWithFrame:textRect]; myTextField.backgroundColor = [UIColor whiteColor]; myTextField.font = [UIFont systemFontOfSize:22.0]; myTextField.adjustsFontSizeToFitWidth = YES; myTextField.minimumFontSize = 2.0; myTextField.delegate = self;
按鍵盤的 Return 時收起鍵盤:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
限制輸入長度:
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
NSInteger MAX_CHARS = 10;
NSMutableString *newText = [NSMutableString stringWithString:textField.text];
[newText replaceCharactersInRange:range withString:string];
return ([newText length] <= MAX_CHARS);
}
建立 View 後,從物件庫把按鈕拖進來,用屬性檢查器調整外觀;在 .h 中宣告 IBAction,並在 IB 裡拉線連接:
- (IBAction)buttonPress:(UIButton *)sender;
- (IBAction)buttonPress:(UIButton *)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示標題"
message:@"通過 xib 實現的按鈕處理方法"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
roundedButtonType = [UIButton buttonWithType:UIButtonTypeRoundedRect];
roundedButtonType.frame = CGRectMake(50.0, 250.0, 88.0, 48.0);
[roundedButtonType setTitle:@"Rounded" forState:UIControlStateNormal];
roundedButtonType.backgroundColor = [UIColor clearColor];
[roundedButtonType addTarget:self
action:@selector(action:)
forControlEvents:UIControlEventTouchUpInside];
roundedButtonType.tag = 2; // 用 tag 分辨多個按鈕
帶圖片的按鈕:
[myButton setImage:[UIImage imageNamed:@"myButtonImage.png"] forState:UIControlStateNormal];
CGRect switchRect = CGRectMake(120, 50, 0, 0);
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:switchRect];
[mySwitch addTarget:self
action:@selector(switchAction:)
forControlEvents:UIControlEventValueChanged];
[mySwitch setOn:YES animated:YES];
想改開關上「ON / OFF」的字樣與顏色,可以深入它的子視圖去取出兩個 label:
UIView *mainView = [[[[mySwitch subviews] objectAtIndex:0] subviews] objectAtIndex:2]; UILabel *onLabel = [[mainView subviews] objectAtIndex:0]; UILabel *offLabel = [[mainView subviews] objectAtIndex:1]; onLabel.text = @"YES"; offLabel.text = @"NO"; onLabel.textColor = [UIColor yellowColor]; offLabel.textColor = [UIColor greenColor];
不過要提醒:這種做法依賴私有視圖層級,系統一升級就可能失效,正式產品不建議使用。
CGRect sliderRect = CGRectMake(20, 50, 280, 40); UISlider *mySlider = [[UISlider alloc] initWithFrame:sliderRect]; mySlider.minimumValue = 0; mySlider.maximumValue = 100; mySlider.continuous = YES; // 拖動過程持續觸發
把值吸附到固定刻度再顯示:
-(void)sliderAction:(id)sender {
int stepAmount = 10;
float stepValue = (abs([(UISlider *)sender value]) / stepAmount) * stepAmount;
[sender setValue:stepValue];
lblSliderValue.text = [NSString stringWithFormat:@"%d", (int)stepValue];
}
把這些元件串起來練習,你會發現它們的套路高度一致:建立 → 設定屬性 → addTarget 綁事件 → 加到視圖 → 釋放。記住這個節奏,換任何一個 UIKit 控制項都能快速上手。