您的位置:首页 > 产品设计 > UI/UE

iOS控件之UITextView字数控制以及占位符的实现

2016-07-12 13:30 477 查看
下面介绍 UITextView 的字数控制,占位符,以及键盘回退方法.

1.字数控制实现

#import "ViewController.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width

@interface ViewController ()<UITextViewDelegate>

/** textView */
@property (nonatomic, weak) UITextView *textView;

/** placeHoldLabel */
@property (nonatomic, weak) UILabel *placeHoldLabel;

/** noticeLabel */
@property (nonatomic, weak) UILabel *noticeLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, 200)]; //初始化大小并自动释放

_textView = textView;

textView.textColor = [UIColor blackColor];//设置字体颜色

textView.font = [UIFont fontWithName:@"Arial" size:18.0];//设置 字体 和 大小

textView.delegate = self;// 设置控制器为 textView 的代理方法

textView.backgroundColor = [UIColor lightGrayColor];//设置它的背景颜色

textView.returnKeyType = UIReturnKeyDefault;//返回键的类型

textView.keyboardType = UIKeyboardTypeDefault;//键盘类型

textView.scrollEnabled = YES;//是否可以拖动

[self.view addSubview:textView];

UILabel *placeHoldLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, kScreenWidth, 25)];

self.placeHoldLabel = placeHoldLabel;

placeHoldLabel.font = [UIFont systemFontOfSize:18];

placeHoldLabel.enabled = NO;

placeHoldLabel.text = @"请输入您的内容...";

placeHoldLabel.numberOfLines=0;

placeHoldLabel.font =  [UIFont systemFontOfSize:12];

[self.textView addSubview:placeHoldLabel];

UILabel *noticeLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 280, kScreenWidth * 0.95, 20)];

self.noticeLabel = noticeLabel;

noticeLabel.textAlignment = NSTextAlignmentRight;

noticeLabel.text = @"您还可以输入200字!";

[self.view addSubview:noticeLabel];
}

#pragma mark - 2.textView delegate
-(void)textViewDidChange:(UITextView *)textView
{
if ([self.textView.text length] == 0) {
[self.placeHoldLabel setHidden:NO];
}else{
[self.placeHoldLabel setHidden:YES];
}
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{

if([text isEqualToString:@"\n"]){

[textView resignFirstResponder];

return NO;
}

if (range.location>=200){

self.noticeLabel.text=@"还能输入0字";
return  NO;
} else {

self.noticeLabel.text=[NSString stringWithFormat:@"还能输入%lu字",200-range.location];
return YES;
}
}
@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  textview 控件 界面 ios