美文网首页
13、UIView和常用组件(三)

13、UIView和常用组件(三)

作者: HQ今日磨墨 | 来源:发表于2015-08-02 17:47 被阅读36次

UITextField 和 UITextView

前者是单行输入,后者多行输入。这里在 BLTwoViewController 中实现:

#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;         // 显示输入界面的样式,这是一个圆角矩形的样式
textField.placeholder = @""please input password;         // 这个是显示在 textfield 中的占位符
textField.secureTextEntry = YES;          // 这种情况你输入的文本的内容是不会显示出来的,只是以*****来表示
textField.clearButtonMode = UITextFieldViewModeWhileEditing;       // 这个状态是只有当内容在编辑的时候,在最右边才会出现 x 标记
textField.keyboardType = UIKeyboardTypeEmailAddress           // 输入键盘的样式
textField.returnKeyType = UIReturnKeyDone;        // 可以选择右下角的键是go 或者 done 等等
......
textField.delegate = self;
textField.contentVerticalAlignment = UIViewContentModeCenter;        // 这个一般都需要设置,设置后,内容会在输入框内显示在垂直方向的中间,更加美观
[self.view addSubview:textField];

细心的话你会注意到里面的代理,代理问题会和多行输入一同讲解。


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

与单行输入不同的是,它没有 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 是一个scrollView 所以是可以上下滚动的。


UIScrollView , 这个是在第三个controller 中做的:

@interface BLThreeViewController: BLBaseViewController<UIScrollViewDelegate>
{
    UIScrollView     *_scrollView;
    UIPageControl    *_pageControl;
    UIView           *_contentView;
}

我们会结合 UIScrollView 和 UIPagecontrol 两个控件一起讲,因为它们经常会一起使用。首先看 scrollView 的实现:

_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;      // 设置之后允许分页,其实是根据偏移量来处理问题的
_scrollView.maximumZoomScale = 3;       // 可以对其上的页面进行放大和缩小,这里设置了最大方法倍数为3.
_scrollView.minimumZoomScale = 0.5;
_scrollView.delegate = self;

经过设置 UIScrollView 的大小之后,可以看到我们可以在红色界面中滚动界面, 说明它的内容有这么大:


scrollView.png

  我们同样可以在 scrollView 上设置别的 普通 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

相关文章

网友评论

      本文标题:13、UIView和常用组件(三)

      本文链接:https://www.haomeiwen.com/subject/mlrvqttx.html