2015年10月3日 星期六

iOS筆記:UITextField & UITextView &Date Picker

UITextField

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    textField1 = [[UITextField alloc] init];
    textField1.frame = CGRectMake(10, 40, 200, 150);
    textField1.backgroundColor = [UIColor lightGrayColor];
    textField1.text = @"Test";

    textField1.font = [UIFont systemFontOfSize:20];
    textField1.textColor = [UIColor purpleColor];
    [self.view addSubview: textField1];

    textField1.textAlignment = NSTextAlignmentCenter; //設定對其方式
    textField1.autocorrectionType = UITextAutocorrectionTypeNo;//是不是要訂正輸入的字

    textField1.keyboardType = UIKeyboardAppearanceDefault;
}
    testField1.Delegate = self;
//文字欄位正在進行編輯時的動作
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return YES;
}

//鍵盤按下 Return ( Done ) 鍵時的動作
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    //收起鍵盤
    [textField resignFirstResponder];
    return YES;
}

//TextField 文字欄位完成編輯時
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    return YES;
}

#pragma UIViewDelegate
//點擊文字框以外的地方時
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //指定欄位如在作用中,則隱藏鍵盤
    if( textField1.isTracking ){
        [textField1 resignFirstResponder];
    }
    [super touchesBegan:touches withEvent:event];
}

UITextView

使用上跟UITextField很相似

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    textView1 = [[UITextView alloc] init];
    textView1.frame = CGRectMake(40, 40, 200, 200);
    textView1.backgroundColor = [UIColor lightGrayColor];
    textView1.text = @"Default Test";

    textView1.font = [UIFont systemFontOfSize:20];
    textView1.textColor = [UIColor purpleColor];
    [self.view addSubview: textView1];

    textView1.textAlignment = NSTextAlignmentCenter; //設定對其方式
    textView1.autocorrectionType = UITextAutocorrectionTypeNo;//是不是要訂正輸入的字

    textView1.keyboardType = UIKeyboardAppearanceDefault;
    textView1.delegate = self;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView1 resignFirstResponder];
        return NO;
    }
    else {
    return YES;
    }
}

Date Picker

Apple認為選取器是實作日期跟時間選擇的最佳介面, 故提供兩種介面選擇, 一種為 Date Picker另一為自訂的Picker View.

要存取Date Picker存取的NSDate資料時就需要使用其date屬性. NSDateFormatter物件可以讓我們把NSDate物件轉換成字串. 如下面範例先初始化NSDateFormatter物件, 在使用setDateFormat方法, 建立出自訂格式. 再利用NSDateFormatter的另一個方法stringFromDate, 只要將NSDate傳入stringFromDate方法, 就會回傳指定格式.

enter image description here

測定時間差異
利用NSDate物件的timeIntervalSinceDate方法, 此方法會回傳兩個日期間的秒數差距.

- (IBAction)DateButton:(UIButton *)sender {
    NSDate *date = self.datePicker.date;

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    #設定顯示的格式
    #[formatter setDateFormat:@"yyyy-MM-dd"];
    [formatter setDateFormat:@"yyyy/MM/dd/HH:mm"];

    NSString *stringFromDate = [formatter stringFromDate:date];
    NSLog(@"%@", stringFromDate);
    NSLog(@"%@", [NSDate date]); 
    #currently date.

    int timeInterval = [date timeIntervalSince1970];  
    #Give us the time interval or haw many seconds have passed since January 1,1970.

    NSLog(@"%i", timeInterval);
}

enter image description here

參考資料
1. http://unicode.org/reports/tr35/tr35-6.html#date_format_patterns
2. http://sh932111.pixnet.net/blog/post/29625583-ios_uitextfield-%E7%9A%84%E6%87%89%E7%94%A8-(%E7%B4%94%E4%BD%BF%E7%94%A8%E7%A8%8B%E5%BC%8F%E7%A2%BC%E8%A8%AD%E5%AE%9A)
3. http://rickyy-blog.logdown.com/tags/UITextField
4. http://www.cnblogs.com/ChinaKingKong/p/4690581.html

沒有留言:

張貼留言