设计带有placeHolder的TextView

简介:

设计带有placeHolder的TextView

效果:

源码:

PlaceholderTextView.h 与 PlaceholderTextView.m

//
//  PlaceholderTextView.h
//  YXTextView
//
//  Created by YouXianMing on 14/12/23.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface PlaceholderTextView : UIView

// 获取的字符串
@property (nonatomic, strong, readonly) NSString *string;

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

// 占位字符
@property (nonatomic, strong) NSString    *placeHolderString;

// 文本边缘留白
@property(nonatomic, assign) UIEdgeInsets  textContainerInset;

// 颜色设置
@property (nonatomic, strong) UIColor     *editTextColor;
@property (nonatomic, strong) UIColor     *placeHolderColor;

// 返回键是否用来做取消第一响应者
@property (nonatomic, assign) BOOL         returnButtonToResignFirstResponder;

// 取消第一响应者
- (void)resignTextViewFirstResponder;

@end


//
//  PlaceholderTextView.m
//  YXTextView
//
//  Created by YouXianMing on 14/12/23.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "PlaceholderTextView.h"

@interface PlaceholderTextView ()<UITextViewDelegate>

@property (nonatomic, strong) NSString *string;

@end

@implementation PlaceholderTextView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createTextView];
    }
    return self;
}

- (void)createTextView {
    self.textView                 = [[UITextView alloc] initWithFrame:self.bounds];
    self.textView.delegate        = self;
    self.textView.backgroundColor = [UIColor clearColor];
    self.textView.textColor       = [UIColor grayColor];
    [self addSubview:self.textView];
}

#pragma mark - 代理方法
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    // 设置编辑状态文字颜色
    textView.textColor = (self.editTextColor == nil ? [UIColor blackColor] : self.editTextColor);
    
    // 如果文字为placeHolder文字
    if ([textView.text isEqualToString:self.placeHolderString]) {
        textView.text = @"";
    }
    
    return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
    // 如果长度为0,则显示placeHolder文字
    if (textView.text.length == 0) {
        textView.text      = self.placeHolderString;
        textView.textColor = (self.placeHolderColor == nil ? [UIColor grayColor] : self.placeHolderColor);
    }
    
    return YES;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
    if (_returnButtonToResignFirstResponder == YES) {
        if ([text isEqualToString:@"\n"]) {
            [textView resignFirstResponder];
            return NO;
        }
    }
    
    return YES;
}

- (void)resignTextViewFirstResponder {
    [self.textView resignFirstResponder];
}

#pragma mark - 重写setter,getter方法
@synthesize string = _string;
- (NSString *)string {
    if ([self.textView.text isEqualToString:self.placeHolderString]) {
        return @"";
    } else {
        return self.textView.text;
    }
}
@synthesize placeHolderColor = _placeHolderColor;
- (void)setPlaceHolderColor:(UIColor *)placeHolderColor {
    _placeHolderColor       = placeHolderColor;
    self.textView.textColor = _placeHolderColor;
}
- (UIColor *)placeHolderColor {
    return _placeHolderColor;
}

@synthesize placeHolderString = _placeHolderString;
- (void)setPlaceHolderString:(NSString *)placeHolderString {
    _placeHolderString = placeHolderString;
    _textView.text     = placeHolderString;
}
- (NSString *)placeHolderString {
    return _placeHolderString;
}
@synthesize textContainerInset = _textContainerInset;
- (void)setTextContainerInset:(UIEdgeInsets)textContainerInset {
    _textContainerInset          = textContainerInset;
    _textView.textContainerInset = textContainerInset;
}
- (UIEdgeInsets)textContainerInset {
    return _textContainerInset;
}

@end

控制器源码:
//
//  ViewController.m
//  YXTextView
//
//  Created by YouXianMing on 14/12/23.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "ViewController.h"
#import "PlaceholderTextView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor blackColor];

    PlaceholderTextView *textView = [[PlaceholderTextView alloc] initWithFrame:CGRectMake(-1, 100, 322, 100)];
    [self.view addSubview:textView];
    
    textView.layer.borderWidth  = 1.f;
    textView.layer.borderColor  = [UIColor purpleColor].CGColor;
    
    textView.returnButtonToResignFirstResponder = YES;
    textView.textView.font      = [UIFont fontWithName:@"HelveticaNeue-Thin" size:22.f];
    textView.placeHolderColor   = [UIColor cyanColor];
    textView.editTextColor      = [UIColor redColor];
    textView.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10);
    textView.placeHolderString  = @"Input your name, please...";
}

@end

需要注意的一些细节:

目录
相关文章
|
2月前
|
XML Java Android开发
[Android]CheckBox复选框
[Android]CheckBox复选框
62 0
|
XML Java API
TextView(文本框)详解
本节给大家带来的UI控件是:TextView(文本框),用于显示文本的一个控件,另外声明一点,我不是翻译API文档,不会一个个属性的去扣,只学实际开发中常用的,有用的,大家遇到感觉到陌生的属性可以查询对应的API!
151 0
EditText与TextView的开发中的常用属性,打造完美布局
EditText与TextView的开发中的常用属性,打造完美布局
61 0
|
XML Android开发 数据格式
Android控件之CheckBox、RadioButton探究
CheckBox和RadioButton控件都只有选中和未选中状态,不同的是RadioButton是单选按钮,需要编制到一个RadioGroup中,同一时刻一个RadioGroup中只能有一个按钮处于选中状态
103 0
Android控件之CheckBox、RadioButton探究
|
数据安全/隐私保护 Android开发
|
C#
艾伟_转载:C# WinForm开发系列 - CheckBox/Button/Label/ProgressBar
包含自定义颜色显示的CheckBox,水晶效果按钮,透明圆角Label,Vista效果的ProgressBar等控件(文章及相关代码搜集自网络,仅供参考学习,版权属于原作者! ). 1.自定义颜色显示的CheckBox coloredcheckcontrols.
1435 0
|
Android开发 数据格式 XML