iOS 實戰(zhàn)筆記2-(GeekBand)

1.使用UILabel顯示靜態(tài)文本

想要給用戶顯示靜態(tài)文本,并且控制文本的字體和顏色。

@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;//文本行數(shù)

self.myLabel.adjustsFontSizeToFitWidth = YES; //標簽視圖保持靜態(tài),并且標簽里的字體能自動調(diào)整到適合標簽的邊界。

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協(xié)議

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

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

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

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

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

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

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

@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,//假如已經(jīng)聲明一個值給它,當用戶編輯這個文本視圖時左視圖將會顯示。

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];

}

現(xiàn)假如你點擊文本視圖,虛擬鍵盤遮蓋了文本視圖的區(qū)域。怎么辦呢?為了能彌補這個缺陷,我們需要監(jiān)聽一些待定通知:

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

UIKeyboardDidShowNotification //當鍵盤已經(jīng)顯示在屏幕上時將會發(fā)送這個通知。

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

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

所以我們的策略就是去發(fā)現(xiàn)鍵盤什么時候準備在屏幕上顯示然后以何種方式調(diào)整視圖。

-(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 結束監(jiān)聽。

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容