IOS學習筆記(6)label textField textview

使用UILabel顯示靜態文本

想要給用戶顯示靜態文本,並且控制文本的字體和顏色。

@property(nonatomic,strong)UILabel *myLabel;

@synthesize myLabel;

-(void)viewDidLoad{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    CGRect labelFrame = CGRectMake(0.0f,0.0f,100.0f,23.0f);

    self.myLabel = [ [UILabel alloc]initWithFrame:labelFrame];

    self.myLabel.text = @"hello world";

    self.myLabel.font = [UIFont boldSystemFontOfSize:14.0f];

    self.myLabel.numberOfLines = 3;//文本行數

    self.myLabel.adjustsFontSizeToFitWidth = YES; //標籤視圖保持靜態,並且標籤裏的字體能自動調整到適合標籤的邊界。

    self.myLabel.center = self.view.center;

    [self.view addSubview:self.myLabel];

}

使用UITextField接受用戶文本輸入(默認高度31像素)

@property( nonatomic,strong)UITextField *myTextField;

@synthesize myTextField;

-(void)viewDidLoad{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    CGRect textFieldFrame = CGRectMake(0.0f,0.0f,200.0f,31.0f);

    self.myTextField = [[UITextField alloc] initWithFrame:textFieldFrame];

    self.myTextField.borderStyle = UITextBorderStyleRoundedRect;//文本視圖邊框

    self.myTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;//文本視圖縱向居中

    self.myTextField.textAlignment = UITextAlignmentCenter;//文本水平對齊方式

    self.myTextField.text = @"hello world";

    self.myTextField.center = self.view.center;

    [self.view addSubview:self.myTextField];

}

UITextFieldDelegate協議

textFieldShouldBeginEditing://BOOL 設置文本視圖是否可編輯

textFieldDidBeginin: //當用戶開始編輯這個文本視圖時這個方法將會被調用。

textFieldShouldEndEditing: //這個方法返回一個BOOL值,它將告訴文本視圖是否結束當前的編輯進程。假如返回NO,用戶將不能終止文本的編輯。

textFieldDidEndEditing: 當文本視圖的編輯進程終止時將會被調用。

textField:shouldChangeCharacterInRange:replacementString: 任何時候文本視圖裏的文本被修改都會調用這個方法。返回是一個布爾值,NO,文本視圖裏的文本的修改將不會被通知和發生,YES,說明允許修改文本。

textFieldShouldClear:每個文本視圖都有一個clear按鈕,通常是一個圓形X按鈕。

textFieldShouldReturn:當用戶在鍵盤上按下return 或 enter 鍵時將會調用這個方法,嘗試隱藏鍵盤,你需要將這個文本視圖註冊爲這個方法的第一個響應者。

@property(nonatomic,strong)UITextField *myTextField;

@property(nonatomic,strong)UILabel *labelCounter;


@synthesize myTextField;

@synthesize labelCounter;

-(void)calculateAndDisplayTextFieldLengthWithText:(NSString *)paramText{

    NSString *characterOrCharacters = @"Characters";

    if([paramText length] == 1){

        characterOrCharacters = @"Character";

    }

    self.labelCounter.text = [NSString stringWithFormat:@"%lu %@",(unsigned long)[paramText length],characterOrCharacters];

}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    BOOL result = YES;

    if([textField isEqual:self.myTextField]){

        NSString *wholeText = [textField.text stringByReplacingCharactersInRange:range withString:string];

    [self calculateAndDisplayTextFieldLengthWithText:wholeText];

    }

    return result;

}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];

    return YES;

}

-(void)viewDidLoad{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whileColor];

    CGRect textFieldFrame = CGRectMake(38.0f,30.0f,220.0f,31.0f);

    self.myTextField = [[UITextField alloc]initWithFrame:textFieldFrame];

    self.myTextField.delegate = self;

    self.myTextField.borderStyle = UITextBorderStyleRounderRect;

    self.myTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

    self.myTextField.text = @"hello world";

    self.myTextField.placeholder = @"Enter text here … ";

    self.view addSubview:self.myTextField];

    CGRect labelCounterFrame = self.myTextField.frame;

    labelCounterFrame.origin.y += textFieldFrame.size.height + 10;

    self.labelCounter = [ [UILabel alloc]initWithFrame:labelCounterFrame];

    [self.view addSubview:self.labelCounter];

    [self calculateAndDisplayTextFieldLengthWithText];

    [self calculateAndDisplayTextFieldLengthWithText:selfTextField.text];

}

文本視圖有兩個相對屬性,它們分別是leftView和rightView。

UILabel *currencyLabel = [ [UILabel alloc]initWithFrame:CGRectZero];

currencyLabel.text = [ [ [NSNumberFormatter alloc]init]currencySymbol];

currencyLabel.font = self.myTextField.font;

[currencyLabel sizeToFit];

self.myTextField.leftView = currencyLabel;

self.myTextField.leftViewMode = UITextFieldViewModeAlways;

typedef enum{

    UITextfieldViewModeNever,

    UITextFieldViewModeWhileEditing,//假如已經聲明一個值給它,當用戶編輯這個文本視圖時左視圖將會顯示。

    UITextFieldViewModeUnlessEditing,//當用戶沒有在文本視圖裏編輯文本時左視圖將會顯示,但是一旦開始編輯,做視圖將會消失。

    UItextFieldViewModeAlways

}UITextFieldViewMode;


使用UITextView顯示文本域

@property( nonatomic,strong)  UItextView  *myTextView;

@synthesize myTextView;

-(void)viewDidLoad{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whileColor];

    self.myTextView = [[UITextView alloc]initWithFrame:self.view.bounds];

    self.myTextView.text = @"hello world";

    self.myTextView.font = [UIFont systemFontOfSize:16.0f];

    self.view addSubView:self.myTextView];

}


現假如你點擊文本視圖,虛擬鍵盤遮蓋了文本視圖的區域。怎麼辦呢?爲了能彌補這個缺陷,我們需要監聽一些待定通知:

UIKeyboardWillShowNotification //當鍵盤正準備顯示子屏幕上時這個通知將會被髮送給任何部件,比如文本視圖。

UIKeyboardDidShowNotification //當鍵盤已經顯示在屏幕上時將會發送這個通知。

UIKeyboardWIllHideNotification //當鍵盤即將被隱藏時將會發送通知

UIKeyboardDidHideNotification //當鍵盤完全隱藏時將會發送這個通知

所以我們的策略就是去發現鍵盤什麼時候準備在屏幕上顯示然後以何種方式調整視圖。

-(void)handleKeyBoardDidShow:(NSNotification *)paramNotification{

    NSValue *keyboardRectAsObject = [[paramNotification userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey];/*get the frame of the keyboard*/

    CGRect keyboardRect;

[keyboardRectAsObject getValue:&keyboardRect];/*place it in a CGRect*/

    self.myTextView.contentInset = UIEdgeInsetsMake(0.0f,0.0f,  keyboarddRect.size.height,0.0f);

}

-(void)handleKeyboardWillHide:(NSNotification *)paramNotification{

    self.myTextView.contentInset = UIEdgeInsetsZero;/*make the text view as the whole view again*/

}

-(void)viewWillAppear:(BOOL)paramAnimated{

    [super viewWillAppear:paramAnimated];

    [ [NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];

    [ [NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

    self.view.backgroundColor = [UIColor whiteColor];

    self.myTextView = [ [UITextView alloc] initWithFrame:self.view.bounds];

    self.myTextView.text = @""hello world……;

    self.myTextView.font = [UIFont systemFontOfSize:16.0f];

    [self.view addSubview: self.myTextView];

}

-(void)viewWillDisappear:(BOOL)paramAnimated{

    [super viewWillDisappear:paramAnimated];

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

在這段代碼中,在方法 viewWillAppear:開始建通鍵盤通知,在方法viewWillDisappear 結束監聽。


UITextView限制字數設置

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

{

    if (range.location>=140)

    {

        UIAlertView * alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"您已輸入140個字" delegate:nil cancelButtonTitle:@"返回" otherButtonTitles: nil];

        [alert show];

        [alert release];

        return NO;

    }

    else 

    {

        return YES;

    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章