IOS筆記之UIKit_UITextField,uikit_uitextfield
- (void)viewDidLoad
{
[super viewDidLoad];
//建立在你已經遵守了<協議UITextFieldDelegate>
self.numTF.delegate = self;
self.passTF.delegate = self;
//密文顯示
self.passTF.secureTextEntry = YES;
}
#pragma mark- UITextField事件監聽
//當輸入文字框將要開始編輯時
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
NSLog(@"單行輸入文字框將要開始編輯時");
return YES;
}
//當輸入文字框開始進入編輯模式時
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(@"單行輸入文字框開始編輯時");
}
//將要完成編輯時調用
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
NSLog(@"單行輸入文字框將要完成編輯");
return YES;
}
//已經退出編輯模式時
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSLog(@"已經退出編輯模式時調用");
//列印當前TextField的內容
NSLog(@"%@",textField.text);
//顯示到TextLabel
self.textLabel.text = textField.text;
}
//當你按下鍵盤上的Return鍵時調用該方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//選中當行輸入文字框時,就改變第一訊息響應者的身份。
[self.numTF resignFirstResponder];
[self.passTF resignFirstResponder];
return YES;
}
#pragma mark-字型換行
- (void)viewDidLoad
{
[super viewDidLoad];
//預設自動換行
self.myLabel.numberOfLines = 0;
//顯示的內容
NSString *string = @"asdfaeatretgfdsgfdgsdgsdgfsdgsdfgsdfgerwtewrtywetyhwerghgfshw4tygwtrfg";
//顯示的顏色
self.myLabel.backgroundColor = [UIColor redColor];
//顯示出內容
self.myLabel.text = string;
//計算文本高度(字典)
NSDictionary *attribute = @{NSFontAttributeName: self.myLabel.font};
CGSize size = [string boundingRectWithSize:CGSizeMake(100, 0) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
CGRect frame = self.myLabel.frame;
frame.size.height = size.height;
self.myLabel.frame = frame;
}