PlaceholderTextView

效果

源码

https://github.com/YouXianMing/UI-Component-Collection 的 PlaceholderTextView

//
// PlaceholderTextView.h
// PlaceholderTextView
//
// Created by YouXianMing on 16/7/18.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h>
@class PlaceholderTextView; @protocol PlaceholderTextViewDelegate <NSObject> @optional /**
* Asks the delegate if editing should begin in the specified text view.
*
* @param textView PlaceholderTextView's object.
*
* @return YEStrue if an editing session should be initiated; otherwise, NOfalse to disallow editing.
*/
- (BOOL)placeholderTextViewShouldBeginEditing:(PlaceholderTextView *)textView; /**
* Asks the delegate if editing should stop in the specified text view.
*
* @param textView PlaceholderTextView's object.
*
* @return YEStrue if editing should stop; otherwise, NOfalse if the editing session should continue
*/
- (BOOL)placeholderTextViewShouldEndEditing:(PlaceholderTextView *)textView; /**
* Tells the delegate that editing of the specified text view has begun.
*
* @param textView PlaceholderTextView's object.
*/
- (void)placeholderTextViewDidBeginEditing:(PlaceholderTextView *)textView; /**
* Tells the delegate that editing of the specified text view has ended.
*
* @param textView PlaceholderTextView's object.
*/
- (void)placeholderTextViewDidEndEditing:(PlaceholderTextView *)textView; /**
* Asks the delegate whether the specified text should be replaced in the text view.
*
* @param textView PlaceholderTextView's object.
*
* @return YEStrue if the old text should be replaced by the new text; NOfalse if the replacement operation should be aborted.
*/
- (BOOL)placeholderTextShouldChangeText:(PlaceholderTextView *)textView; @end @interface PlaceholderTextView : UIView /**
* PlaceholderTextView's delegate.
*/
@property (nonatomic, weak) id <PlaceholderTextViewDelegate> delegate; /**
* Current string.
*/
@property (nonatomic, strong, readonly) NSString *currentString; #pragma mark - UITextView related. /**
* The TextView.
*/
@property (nonatomic, strong, readonly) UITextView *textView; /**
* The textView's containerInset.
*/
@property (nonatomic) UIEdgeInsets textContainerInset; #pragma mark - Placeholder related. /**
* Placeholder attributed string.
*/
@property (nonatomic, strong) NSAttributedString *attributedPlaceholder; /**
* PlaceHorderString gap from left.
*/
@property (nonatomic) CGFloat placeHorderLeftEdge; /**
* PlaceHorderString gap from top.
*/
@property (nonatomic) CGFloat placeHorderTopEdge; #pragma mark - PlaceholderTextView's event. /**
* PlaceholderTextView resign first responder.
*/
- (void)placeholderTextViewResignFirstResponder; /**
* PlaceholderTextView become first responder.
*/
- (void)placeholderTextViewbecomeFirstResponder; @end
//
// PlaceholderTextView.m
// PlaceholderTextView
//
// Created by YouXianMing on 16/7/18.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import "PlaceholderTextView.h" @interface PlaceholderTextView () <UITextViewDelegate> @property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) NSString *currentString; @end @implementation PlaceholderTextView #pragma mark - Frame related method. - (void)layoutSubviews { [super layoutSubviews]; self.textView.frame = self.bounds;
[self resetPlaceHorderFrame];
} - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.textField = [[UITextField alloc] init];
self.textField.enabled = NO;
self.textField.textColor = [UIColor clearColor];
[self addSubview:self.textField]; 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];
} return self;
} #pragma mark - FirstResponder related. - (void)placeholderTextViewResignFirstResponder { [self.textView resignFirstResponder];
} - (void)placeholderTextViewbecomeFirstResponder { [self.textView becomeFirstResponder];
} #pragma mark - UITextViewDelegate - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { NSString *currentText = [textView.text stringByReplacingCharactersInRange:range withString:text];
self.textField.text = currentText;
self.currentString = currentText; if (self.delegate && [self.delegate respondsToSelector:@selector(placeholderTextShouldChangeText:)]) { return [self.delegate placeholderTextShouldChangeText:self]; } else { return YES;
}
} - (BOOL)textViewShouldBeginEditing:(UITextView *)textView { if (self.delegate && [self.delegate respondsToSelector:@selector(placeholderTextViewShouldBeginEditing:)]) { return [self.delegate placeholderTextViewShouldBeginEditing:self]; } else { return YES;
}
} - (BOOL)textViewShouldEndEditing:(UITextView *)textView { if (self.delegate && [self.delegate respondsToSelector:@selector(placeholderTextViewShouldEndEditing:)]) { return [self.delegate placeholderTextViewShouldEndEditing:self]; } else { return YES;
}
} - (void)textViewDidBeginEditing:(UITextView *)textView { if (self.delegate && [self.delegate respondsToSelector:@selector(placeholderTextViewDidBeginEditing:)]) { [self.delegate placeholderTextViewDidBeginEditing:self];
}
} - (void)textViewDidEndEditing:(UITextView *)textView { if (self.delegate && [self.delegate respondsToSelector:@selector(placeholderTextViewDidEndEditing:)]) { [self.delegate placeholderTextViewDidEndEditing:self];
}
} #pragma mark - PlaceHorder related - (void)resetPlaceHorderFrame { self.textField.attributedPlaceholder = _attributedPlaceholder;
[self.textField sizeToFit]; CGRect newFrame = self.textField.frame;
newFrame.origin.x = _placeHorderLeftEdge;
newFrame.origin.y = _placeHorderTopEdge;
self.textField.frame = newFrame;
} #pragma mark - Setter & Getter - (void)setTextContainerInset:(UIEdgeInsets)textContainerInset { _textContainerInset = textContainerInset;
_textView.textContainerInset = textContainerInset;
} - (void)setPlaceHorderLeftEdge:(CGFloat)placeHorderLeftEdge { _placeHorderLeftEdge = placeHorderLeftEdge;
[self resetPlaceHorderFrame];
} - (void)setPlaceHorderTopEdge:(CGFloat)placeHorderTopEdge { _placeHorderTopEdge = placeHorderTopEdge;
[self resetPlaceHorderFrame];
} - (void)setAttributedPlaceholder:(NSAttributedString *)attributedPlaceholder { _attributedPlaceholder = attributedPlaceholder;
[self resetPlaceHorderFrame];
} @end
//
// PlaceholderTextView+ConvenientSetup.h
// PlaceholderTextView
//
// Created by YouXianMing on 16/7/18.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import "PlaceholderTextView.h" @interface PlaceholderTextView (ConvenientSetup) /**
* PlaceholderTextView's placeholderString setup.
*
* @param string The placeholderString.
* @param font Font.
* @param color Color.
* @param leftEdge Gap from left.
* @param topEdge Gap from top.
*/
- (void)placeholderString:(NSString *)string font:(UIFont *)font color:(UIColor *)color leftEdge:(CGFloat)leftEdge topEdge:(CGFloat)topEdge; /**
* PlaceholderTextView's textView setup.
*
* @param font Font.
* @param color Color.
* @param containerInset TextContainerInset.
*/
- (void)textViewFont:(UIFont *)font color:(UIColor *)color containerInset:(UIEdgeInsets)containerInset; /**
* Create the InputAccessoryView with the specified heigh.
*
* @param height The view's height.
*
* @return InputAccessoryView.
*/
- (UIView *)createInputAccessoryViewWithViewHeight:(CGFloat)height; @end
//
// PlaceholderTextView+ConvenientSetup.m
// PlaceholderTextView
//
// Created by YouXianMing on 16/7/18.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import "PlaceholderTextView+ConvenientSetup.h" @implementation PlaceholderTextView (ConvenientSetup) - (void)placeholderString:(NSString *)string font:(UIFont *)font color:(UIColor *)color leftEdge:(CGFloat)leftEdge topEdge:(CGFloat)topEdge { NSParameterAssert(string);
NSParameterAssert(font);
NSParameterAssert(color); NSString *placeHorderString = string;
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:placeHorderString];
[attributeString addAttribute:NSFontAttributeName value:font range:NSMakeRange(, placeHorderString.length)];
[attributeString addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(, placeHorderString.length)]; self.placeHorderLeftEdge = leftEdge;
self.placeHorderTopEdge = topEdge;
self.attributedPlaceholder = attributeString;
} - (void)textViewFont:(UIFont *)font color:(UIColor *)color containerInset:(UIEdgeInsets)containerInset { self.textView.font = font;
self.textView.textColor = color;
self.textContainerInset = containerInset;
} - (UIView *)createInputAccessoryViewWithViewHeight:(CGFloat)height { UIView *inputAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(, , [UIScreen mainScreen].bounds.size.width, height)];
inputAccessoryView.backgroundColor = [UIColor clearColor];
self.textView.inputAccessoryView = inputAccessoryView; return inputAccessoryView;
} @end
//
// ViewController.m
// PlaceholderTextView
//
// Created by YouXianMing on 16/7/18.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "PlaceholderTextView.h"
#import "PlaceholderTextView+ConvenientSetup.h" @interface ViewController () <PlaceholderTextViewDelegate> { PlaceholderTextView *_textView;
} @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIColor *grayColor = [UIColor grayColor];
UIColor *textColor = [[UIColor blackColor] colorWithAlphaComponent:0.95f];
UIColor *whiteColor = [UIColor whiteColor];
UIFont *font_16 = [UIFont systemFontOfSize:.f]; // Add UITapGestureRecognizer.
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureEvent)];
[self.view addGestureRecognizer:tapGesture]; // Create PlaceholderTextView.
_textView = [[PlaceholderTextView alloc] initWithFrame:CGRectMake(, , , )];
_textView.layer.borderWidth = 0.5f;
_textView.delegate = self;
[self.view addSubview:_textView]; // Set placeholderString.
[_textView placeholderString:@"请输入您的评价(少于50字)" font:font_16 color:grayColor leftEdge:.f topEdge:.f]; // Set textView.
[_textView textViewFont:font_16 color:textColor containerInset:UIEdgeInsetsMake(.f, .f, .f, .f)]; // Create inputAccessoryView.
UIView *inputAccessoryView = [_textView createInputAccessoryViewWithViewHeight:.f];
inputAccessoryView.backgroundColor = grayColor; // Setup inputAccessoryView.
UIButton *button = [[UIButton alloc] initWithFrame:inputAccessoryView.bounds];
button.titleLabel.font = [UIFont systemFontOfSize:.f];
[button setTitle:@"确定" forState:UIControlStateNormal];
[button setTitleColor:whiteColor forState:UIControlStateNormal];
[button setTitleColor:[whiteColor colorWithAlphaComponent:0.5f] forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(inputAccessoryViewEvent) forControlEvents:UIControlEventTouchUpInside];
[inputAccessoryView addSubview:button];
} #pragma mark - Event related. - (void)inputAccessoryViewEvent { [_textView placeholderTextViewResignFirstResponder];
} - (void)gestureEvent { [self.view endEditing:YES];
} #pragma mark - PlaceholderTextViewDelegate - (BOOL)placeholderTextShouldChangeText:(PlaceholderTextView *)textView { NSLog(@"--> %@", textView.currentString);
BOOL result; textView.currentString.length >= ? (result = NO) : (result = YES);
return result;
} - (BOOL)placeholderTextViewShouldBeginEditing:(PlaceholderTextView *)textView { NSLog(@"placeholderTextViewShouldBeginEditing");
return YES;
} - (BOOL)placeholderTextViewShouldEndEditing:(PlaceholderTextView *)textView { NSLog(@"placeholderTextViewShouldEndEditing");
return YES;
} - (void)placeholderTextViewDidBeginEditing:(PlaceholderTextView *)textView { NSLog(@"placeholderTextViewDidBeginEditing");
} - (void)placeholderTextViewDidEndEditing:(PlaceholderTextView *)textView { NSLog(@"placeholderTextViewDidEndEditing");
} #pragma mark - System method. - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated];
[_textView placeholderTextViewbecomeFirstResponder];
} @end

PlaceholderTextView的更多相关文章

  1. 简易封装一个带有占位文字的TextView

    在实际iOS应用开发中我们经常会用到类似于下图所示的界面,即带有占位文字的文本框:

  2. 设计带有placeHolder的TextView

    设计带有placeHolder的TextView 效果: 源码: PlaceholderTextView.h 与 PlaceholderTextView.m // // PlaceholderText ...

  3. UINavigationbar跳转黑色

    bug效果:导航栏过渡出现黑色

随机推荐

  1. jQuery下的onChange事件在某些情况下无效

    onChage无效的原因: 虽然表面上感觉是当内容发生变化时,就会触发onchange事件,但是那只能在页面上操作.而如果通过dom对象去修改它的value则什么事也不会发生. onchange触发原 ...

  2. 索引Hint提示(INDEX Hint)

    定义:所谓的索引Hint提示,就是强制查询优化器为一个查询语句执行扫描或者使用一个指定的索引 前提:利用索引提示的前提就是当前表存在索引了,如果是堆表的情况,只能通过表扫描获取数据了. 用处:很多时候 ...

  3. hdu 5053 (2014上海网赛L题 求立方和)

    题目大意:给你L到N的范围,要求你求这个范围内的所有整数的立方和. Sample Input2 //T1 32 5 Sample OutputCase #1: 36Case #2: 224 # inc ...

  4. Linux系统运维笔记(二),Linux文件编辑命令

    Linux系统运维笔记 Linux文件编辑命令 首先我们使用命令 vi filename 打开一个文件,这个时候进入到的是命令模式 接下来我们按i,然后键盘随便输入写内容. 然后按ESC重新进入到命令 ...

  5. 【LOJ】#2479. 「九省联考 2018」制胡窜

    题解 老了,国赛之前敲一个后缀树上LCT和线段树都休闲的很 现在后缀树上线段树合并差点把我写死 主要思路就是后缀树+线段树合并+容斥,我相信熟练的OIer看到这已经会了 但就是不想写 但是由于我过于老 ...

  6. 关于spark standalone模式下的executor问题

    1.spark standalone模式下,worker与executor是一一对应的. 2.如果想要多个worker,那么需要修改spark-env的SPARK_WORKER_INSTANCES为2 ...

  7. 送你一套纯净版的 SSM 架构

    大致介绍一下,目前 Java 中使用比较多的框架组合就是 Spring .Springmvc .Mybatis ,这 3 个框架也就是我们常说的 SSM. 前面陆陆续续也已经介绍完了这 3 个框架,今 ...

  8. Web服务器指纹识别工具httprint

    Web服务器指纹识别工具httprint   在Web渗透测试中,准确判断服务器的类型直接影响后期渗透测试的成功率.Kali Linux提供了专门的Web服务器指纹识别工具Httprint.该工具根据 ...

  9. ArduinoYun教程之OpenWrt-Yun与CLI配置Arduino Yun

    ArduinoYun教程之OpenWrt-Yun与CLI配置Arduino Yun OpenWrt-Yun OpenWrt-Yun是基于OpenWrt的一个Linux发行版.有所耳闻的读者应该听说他是 ...

  10. zoj 3659 第37届ACM/ICPC 长春赛区现场赛E题 (并查集)

    题意:给出一棵树,找出一个点,求出所有点到这个点的权值和最大,权值为路径上所有边权的最小值. 用神奇的并查集,把路按照权值从大到小排序,然后用类似Kruskal的方法不断的加入边. 对于要加入的一条路 ...