博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设计带有placeHolder的TextView
阅读量:5869 次
发布时间:2019-06-19

本文共 4681 字,大约阅读时间需要 15 分钟。

设计带有placeHolder的TextView

效果:

源码:

PlaceholderTextView.h 与 PlaceholderTextView.m

////  PlaceholderTextView.h//  YXTextView////  Created by YouXianMing on 14/12/23.//  Copyright (c) 2014年 YouXianMing. All rights reserved.//#import 
@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 ()
@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

需要注意的一些细节:

 

 

转载地址:http://jttnx.baihongyu.com/

你可能感兴趣的文章
Java程序员必须掌握的8大排序算法
查看>>
如何设计一个优秀的API
查看>>
字符串操作函数的编写
查看>>
docker 实践(一)安装
查看>>
设计模式 - 结构型 - 代理模式
查看>>
2014年工作中遇到的20个问题:101-120
查看>>
Fans同学的基本信息-V1.0
查看>>
w2k8 time sync
查看>>
试论社会学专业定×××方法课程教学现状及改革途径
查看>>
macOS、PowerPoint、Excel、Word快捷键
查看>>
文本处理三剑客之sed基本用法
查看>>
[Micropython TPYBoard ] ADC的使用方法
查看>>
水质不达标泳池健康隐患多 教你三招辨识水质好坏
查看>>
Netty学习笔记1:5种IO模型
查看>>
Sqoop官方文档翻译1
查看>>
大型网站架构系列:负载均衡详解[转]
查看>>
select css 去掉倒三角
查看>>
设计思想
查看>>
MD5加密随机增强的构想
查看>>
golang在ubuntu-12.04和centos-6.5上的安装
查看>>