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;  

    

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。