UITextView 实现 placeholder 及隐藏键盘 ios

实现 placeholder   详见: http://code4app.com/ios/CBTextView/523965516803fa4e61000001

自定义一个CBTextView.h  (也可以从上面网址下载,然后直接看后面的调用即可)

#import <UIKit/UIKit.h>

@interface CBTextView : UIView

{

UIColor *defaultTextColor;

NSString *prevText;

}

@property (strong, nonatomic) UITextView *textView;

@property (strong, nonatomic) NSString *placeHolder;

@property (strong, nonatomic) UIColor *placeHolderColor;

@property (weak, nonatomic) id<UITextViewDelegate> aDelegate;

@end

CBTextView.m

#import "CBTextView.h"

#define SHOW_DEBUG_VIEW     0

#define RGBAlphaColor(r, g, b, a) \

[UIColor colorWithRed:(r/255.0)\

green:(g/255.0)\

blue:(b/255.0)\

alpha:(a)]

#define OpaqueRGBColor(r, g, b) RGBAlphaColor((r), (g), (b), 1.0)

@interface CBTextView () <UITextViewDelegate>

@end

@implementation CBTextView

- (id)initWithCoder:(NSCoder *)aDecoder

{

self = [super initWithCoder:aDecoder];

if (self) {

[self setup];

}

return self;

}

- (id)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self) {

[self setup];

}

return self;

}

- (void)setup

{

[self addSubview:self.textView];

self.placeHolderColor = OpaqueRGBColor(199, 200, 201);

self.textView.delegate = self;

#if SHOW_DEBUG_VIEW

self.textView.backgroundColor = DEBUG_VIEW_ITEM_COLOR;

self.backgroundColor = DEBUG_VIEW_CONTAINER_COLOR;

#endif

}

#pragma mark - Layout

- (void)setFrame:(CGRect)frame

{

[super setFrame:frame];

CGRect aFrame = CGRectZero;

aFrame.origin = self.bounds.origin;

aFrame.size = frame.size;

self.textView.frame = aFrame;

}

#pragma mark - Accessor

- (void)setPlaceHolder:(NSString *)placeHolder

{

_placeHolder = placeHolder;

self.textView.text = placeHolder;

}

- (void)setPlaceHolderColor:(UIColor *)placeHolderColor

{

_placeHolderColor = placeHolderColor;

self.textView.textColor = placeHolderColor;

}

- (void)setTextColor:(UIColor *)textColor

{

defaultTextColor = self.textView.textColor;

self.textView.textColor = textColor;

}

- (void)setADelegate:(id)aDelegate

{

_aDelegate = aDelegate;

}

- (UITextView *)textView

{

if (_textView == nil) {

_textView = [[UITextView alloc] init];

_textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

}

return _textView;

}

#pragma mark - UITextViewDelegate

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView

{

if ([self.aDelegate respondsToSelector:@selector(textViewShouldBeginEditing:)]) {

return [self.aDelegate textViewShouldBeginEditing:textView];

}

return YES;

}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView

{

if ([self.aDelegate respondsToSelector:@selector(textViewShouldEndEditing:)]) {

return [self.aDelegate textViewShouldEndEditing:textView];

}

return YES;

}

- (void)textViewDidBeginEditing:(UITextView *)textView

{

self.textView.text = prevText;

self.textView.textColor = defaultTextColor;

if ([self.aDelegate respondsToSelector:@selector(textViewDidBeginEditing:)]) {

[self.aDelegate textViewDidBeginEditing:textView];

}

}

- (void)textViewDidEndEditing:(UITextView *)textView

{

prevText = self.textView.text;

if (!prevText || [prevText length]==0) {

self.textView.text = self.placeHolder;

self.textView.textColor = self.placeHolderColor;

}

if ([self.aDelegate respondsToSelector:@selector(textViewDidEndEditing:)]) {

[self.aDelegate textViewDidEndEditing:textView];

}

}

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

{

if ([self.aDelegate respondsToSelector:@selector(textView:shouldChangeTextInRange:replacementText:)]) {

return [self.aDelegate textView:textView shouldChangeTextInRange:range replacementText:text];

}

return YES;

}

- (void)textViewDidChange:(UITextView *)textView

{

if ([self.aDelegate respondsToSelector:@selector(textViewDidChange:)]) {

[self.aDelegate textViewDidChange:textView];

}

}

- (void)textViewDidChangeSelection:(UITextView *)textView

{

if ([self.aDelegate respondsToSelector:@selector(textViewDidChangeSelection:)]) {

[self.aDelegate textViewDidChangeSelection:textView];

}

}

#pragma mark - Actions

- (BOOL)resignFirstResponder

{

return [self.textView resignFirstResponder];

}

@end

开始调用:
#import "CBTextView.h"
@property(strong,nonatomic)CBTextView *textView;

CBTextView *textView1=[[CBTextView alloc]initWithFrame:CGRectMake(5, 5, 220, 80)];

textView1.backgroundColor=[UIColor clearColor];

self.textView=textView1;

_textView.placeHolder = @"这软件做得不错,你们接受定制开发吗?";

_textView.placeHolderColor = [UIColor grayColor];

self.textView.aDelegate=self;

[self.view addSubview:textView1];

//隐藏键盘,实现UITextViewDelegate

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

{

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

[self.textView resignFirstResponder];

return NO;

}

return YES;

}

时间: 2024-12-28 21:16:37

UITextView 实现 placeholder 及隐藏键盘 ios的相关文章

UITextView 实现placeholder的方法

本文转载至 http://www.cnblogs.com/easonoutlook/archive/2012/12/28/2837665.html 在UITextField中自带placeholder属性,可以用于提示输入框信息.但是UITextView并不具备此功能 介绍两种方法来实现: 第一种: 初始化UITextView //首先定义UITextView UITextView *textView = [[UITextView alloc] init]; textView.font = [U

UITextView 点return 隐藏键盘

iOS开发中,发现UITextView没有想UITextField中textFieldShouldReturn:这样的方法,那么要实现UITextView return键隐藏键盘,可以通过判断输入的字符是不是回车符来实现. 首先,声明要实现UITextView 的delegate. @interface MyViewController :UIViewController <UITextViewDelegate> 然后, 设置textView的delegate.textView.delegat

IOS隐藏键盘

最近遇到二个键盘会自动弹出的问题: 1.UIWebView加载网页后,点击网页内的链接在UIWebView内进行跳转时,键盘自动弹起: 2.调用选择照片时,iPod上选择照片后也会自动弹出键盘,比如从图库进到具体某个文件夹内,或者再返回图库,直接点中照片然后编辑的时候,都会自动弹出:   问题一是这样处理的 - (void)webViewDidStartLoad:(UIWebView*)webView{    [activityIndicatorView_ startAnimating];   

iOS开发隐藏键盘方法总结

iOS开发里键盘是经常需要打交道的地方,下面为大家带来我整理总结的几种隐藏键盘的方法. 一.隐藏自身软键盘 当对于有多个UITextField控件都想通过点击“Return”来隐藏自身软键盘的情况,这时的最好办法是使用Did End on Exit事件.在点击软键盘右下角的“Return”按钮后,会触发该事件.该事件有一个sender参数表示当前文本框,这样便可以编写一个通用的事件处理方法(.m文件). - (IBAction)TextField_DidEndOnExit:(id)sender 

js控制ios端的input/textarea元素失去焦点时隐藏键盘

同事在测试产品时发现这样一个:"某些页面击完input框,在点空白处时,iOS设备的键盘不能隐藏并且焦点也不会失去" 带着这个问题我进行了测试,发现在安卓的设备上并没有这种问题出现. 于是写js进行测试给document添加一个click事件,发现了问题的原因: 安卓是可以触发click事件的,而iPhone不会触发. 也就是说在iOS设备下你点击空白的document处input并不能失去焦点. 解决办法: 既然click不能触发iPhone的事件,那就需要找触屏事件来触发一次: o

IOS UITextView加上placeholder

UITextView上如何加上类似于UITextField的placeholder呢,其实在UITextView上加上一个UILabel,然后再实现 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text这个代理方法就可以了. 具体实现如下: - (BOOL)textView:(UITextView *)textView shou

[爱上Swift] day10:IOS 点击空白处隐藏键盘的几种方法

IOS7 点击空白处隐藏键盘的几种方法 IOS开发中经常要用到输入框,默认情况下点击输入框就会弹出键盘,但是必须要实现输入框return的委托方法才能取消键盘的显示,对于用户体验来说很不友好,我们可以实现点击键盘以外的空白区域来将键盘隐藏,以下我总结出了几种隐藏键盘的方法: 首先说明两种可以让键盘隐藏的Method: 1.[view endEditing:YES]  这个方法可以让整个view取消第一响应者,从而让所有控件的键盘隐藏. 2.[textFiled resignFirstRespon

IOS开发隐藏键盘的4种方法

IOS开发隐藏键盘的4种方法 开发的app中常会用到键盘输入完成后隐藏键盘的情况.在此总结了4种方法来实现我们的目的. 方法一--delegate方式 第一种隐藏键盘的方法,称为delegate三步法: 1. 遵循(委托/代理); 2. 调用; 3. 关联头文件(ViewController.h)中遵循代理 . 代码如下: #import <UIKit/UIKit.h> @interface ViewController : UIViewController<UITextFieldDel

iOS开发几种隐藏键盘方法

摘要:iOS开发里键盘是经常需要打交道的地方,下面为大家带来几种隐藏键盘的方法. 一.隐藏自身软键盘 当对于有多个UITextField控件都想通过点击"Return"来隐藏自身软键盘的情况,这时的最好办法是使用Did End on Exit事件.在点击软键盘右下角的"Return"按钮后,会触发该事件. 该事件有一个sender参数表示当前文本框,这样便可以编写一个通用的事件处理方法(.m文件). 1 - (IBAction)TextField_DidEndOnE