13、UIView和常用組件(三)

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

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

  • 掌握 UIScrollView的常見(jiàn)屬性 UIScrollView的常用代理方法 UIScrollView的縮放 ...
    JonesCxy閱讀 2,876評(píng)論 1 12
  • 今天,她一如繼往地沒(méi)睡好。像往常一樣睡夢(mèng)中的她被兒子的哭聲吵醒,頭昏腦脹地爬起來(lái),滿帶怨氣地找奶瓶,沖好奶后迅速喂...
    萱耳傾聽(tīng)閱讀 270評(píng)論 6 0
  • 向高中邁進(jìn)
    擦擦擦可口可樂(lè)了閱讀 195評(píng)論 0 0
  • 一個(gè)地方總是人來(lái)人往的,好是熱鬧。偶爾會(huì)來(lái)一些熟人也偶爾回來(lái)一些生人。我們總是喜歡去自己特別熟悉或者根本不熟悉的地...
    Caphintty圖圖閱讀 177評(píng)論 0 1
  • “Hey,Man,How are you doing?”James 看到老朋友黑煙來(lái)了開(kāi)心得很。 “Not bad...
    闡釋逐暗閱讀 419評(píng)論 0 0

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