UITextField 和 UITextView
前者是單行輸入,后者多行輸入。這里在 BLTwoViewController 中實(shí)現(xiàn):
#import <UIKit/UIKit.h>
#import "BLBaseViewController.h"
@interface BLTwoViewController:BLBaseViewController<UITextFieldDelegate,UITextViewDelegate>
@end
UITextField:
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 74, self.view.frame.size.width - 20, 31)];
textField.borderStyle = UITextBorderStyleRoundedRect; // 顯示輸入界面的樣式,這是一個(gè)圓角矩形的樣式
textField.placeholder = @""please input password; // 這個(gè)是顯示在 textfield 中的占位符
textField.secureTextEntry = YES; // 這種情況你輸入的文本的內(nèi)容是不會(huì)顯示出來(lái)的,只是以*****來(lái)表示
textField.clearButtonMode = UITextFieldViewModeWhileEditing; // 這個(gè)狀態(tài)是只有當(dāng)內(nèi)容在編輯的時(shí)候,在最右邊才會(huì)出現(xiàn) x 標(biāo)記
textField.keyboardType = UIKeyboardTypeEmailAddress // 輸入鍵盤(pán)的樣式
textField.returnKeyType = UIReturnKeyDone; // 可以選擇右下角的鍵是go 或者 done 等等
......
textField.delegate = self;
textField.contentVerticalAlignment = UIViewContentModeCenter; // 這個(gè)一般都需要設(shè)置,設(shè)置后,內(nèi)容會(huì)在輸入框內(nèi)顯示在垂直方向的中間,更加美觀
[self.view addSubview:textField];
細(xì)心的話你會(huì)注意到里面的代理,代理問(wèn)題會(huì)和多行輸入一同講解。
UITextView:
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, textField.frame.origin.y + 31 + 10, self.view.frame.size.width - 20, 80)];
textView.backgroundColor = [UIColor redColor];
textView.textColor = [UIColor blackColor];
textView.delegate = self;
textView.keyboardType = UIKeyboardTypeEmailAddress;
textView.returnKeyType = UIReturnKeyGo;
[self.view addSubview:textView];
與單行輸入不同的是,它沒(méi)有 placeholder 和 clearButtonMode。
它們的代理方法有如下:
- (void)textFieldShouldReturn:(UITextField *)textField
{
NSLog(@"%@", textField.text);
[textField resignFirstResponder];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
NSLog(@"%@", textField.text);
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSLog(@"%@", textField.text);
}
- (void)textViewDidChange:(UITextView *)textView
{
NSLog(@"%@", textView);
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ([text is EqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
textView 是一個(gè)scrollView 所以是可以上下滾動(dòng)的。
UIScrollView , 這個(gè)是在第三個(gè)controller 中做的:
@interface BLThreeViewController: BLBaseViewController<UIScrollViewDelegate>
{
UIScrollView *_scrollView;
UIPageControl *_pageControl;
UIView *_contentView;
}
我們會(huì)結(jié)合 UIScrollView 和 UIPagecontrol 兩個(gè)控件一起講,因?yàn)樗鼈兘?jīng)常會(huì)一起使用。首先看 scrollView 的實(shí)現(xiàn):
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 64, scrollViewWidth, scrollViewHeight)];
_scrollView.backgroundColor = [UIColor redColor];
[self.view addSubview:_scrollView];
_scrollView.contentSize = CGSizeMake(scrollViewWidth * 5, scrollViewHeight * 5);
_scrollView.pagingEnabled = YES; // 設(shè)置之后允許分頁(yè),其實(shí)是根據(jù)偏移量來(lái)處理問(wèn)題的
_scrollView.maximumZoomScale = 3; // 可以對(duì)其上的頁(yè)面進(jìn)行放大和縮小,這里設(shè)置了最大方法倍數(shù)為3.
_scrollView.minimumZoomScale = 0.5;
_scrollView.delegate = self;
經(jīng)過(guò)設(shè)置 UIScrollView 的大小之后,可以看到我們可以在紅色界面中滾動(dòng)界面, 說(shuō)明它的內(nèi)容有這么大:

scrollView.png
??我們同樣可以在 scrollView 上設(shè)置別的 普通 UIView 視圖。
for (int i = 0; i < 5; i++) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(i * scrollViewWidth, 0, scrollViewWidth, scrollViewHeight)];
if (i % 2 == 0) {
view.backgroundColor = [UIColor blackColor];
} else {
view.backgroundColor = [UIColor whiteColor];
}
[_contentView addSubview:view];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, scrollViewWidth, scrollViewHeight)];
imageView.backgroundColor = [UIColor clearColor];
NSString *imageName = [NSString stringWithFormat:@"bg%i.png", i];
imageView.image = [UIImage imageNamed:imageName];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[view addSubview:imageView];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, scrollViewWidth, scrollViewHeight)];
label.font = [UIFont boldSystemFontOfSize:100];
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.text = [NSString stringWithFormat:@"%i", i + 1];
[view addSubview:label];
}
差不多得到下面的效果圖:

scrollView效果圖.jpg