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. LeetCode(21):合并两个有序链表

    Easy! 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1- ...

  2. CF1064A 【Make a triangle!】

    要让这个三角形合法,只需满足三角形不等式 即$a+b>c$,设$c=max\left\{a,b,c\right\}$,上式转化为$c<a+b$ 如果已经满足,不需消耗代价 否则消耗$c-a ...

  3. MAC下代理工具Charles使用

    一.跟踪HTTPS 1.下载官方的证书ssl.zip证书,解压成*.crt 2.可以通过邮箱或者发布到自己的服务器的方式,然后用手机去下载安装crt文件. 3.charles设置Proxy--> ...

  4. Java编程的逻辑 (28) - 剖析包装类 (下)

    ​本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...

  5. #ASP.NET 自制免费.NET代码生成器KevinCodeBuilder

    简单的.NET三层架构会有很多重复的代码,如果手敲会比较浪费时间.前段时间在互联网找了下.NET的代码生成器,发现要么太复杂甚至生成的代码会报错,要么要收费而且效果不是自己想要的. 所以,干脆自己做一 ...

  6. 解决vue项目打包后背景图片找不到的问题

    在build->webpack.base.conf.js里添加一句代码: 具体位置在module->rules下 publicPath:"../../",

  7. python3之Django模型(一)

    1.模型概述 模型是关于您的数据的唯一,明确的信息来源,它包含您正在存储的数据的重要字段和行为.通常,每个模型映射到单个数据库表. 每个模型都是一个子类的python类django.db.models ...

  8. javascript模拟flash头像裁切上传

    是的,jq已经有类似的插件了,或者干脆用flash算了,为什么我还要自己写?因为造(wo)轮(bu)子(hui)也(flash)是一个学习的过程,轮子不会造,将来怎么造飞机?先来一张最终效果图: 一. ...

  9. odoo权限管理(二.记录管理)

    规则保存在ir.rule模型表里,需要设置关联某个模型,关联很多组,访问权限控制和domian. 通过domain_force过滤出的一些记录来执行约束. 例子:经理只能删除状态为'cancel'的客 ...

  10. Xamarin-Android_BaseAdapter 简单的复用

    Xamarin-Android_BaseAdapter 简单的复用 缘由: 本人是一枚 小菜 初学Xamarin-Android  正在学习ListView 控件 发现这个控件的自定义布局 用的那叫一 ...