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. MySQL JOIN原理

    先看一下实验的两张表: 表comments,总行数28856 表comments_for,总行数57,comments_id是有索引的,ID列为主键. 以上两张表是我们测试的基础,然后看一下索引,co ...

  2. Connect to DB2 database in eclipse via jdbc

    https://stackoverflow.com/questions/23801841/connect-to-db2-database-in-eclipse-via-jdbc

  3. Js数组里剔除指定的元素(不是指定的位置)

    s数组里删除指定的元素(不是指定的位置)之前一直是做后端的,从来也没有写过js,但是却一直想学学,也只是基于兴趣而已!现在到了这个公司,确实大量的写js.但也一直都是没有系统的去看过js!都是搞什么查 ...

  4. Z字形变换

    将字符串 "PAYPALISHIRING" 以 Z 字形排列成给定的行数: P A H N A P L S I I G Y I R 之后从左往右,逐行读取字符:"PAHN ...

  5. 【LOJ】#2532. 「CQOI2018」社交网络

    题解 基尔霍夫矩阵,外向树是入度矩阵-邻接矩阵 必须删掉第一行第一列然后再求行列式 代码 #include <bits/stdc++.h> #define fi first #define ...

  6. Codeforces 336D Dima and Trap Graph 并查集

    Dima and Trap Graph 枚举区间的左端点, 然后那些左端点比枚举的左端点小的都按右端点排序然后并查集去check #include<bits/stdc++.h> #defi ...

  7. .NET工作准备--04ASP.NET

    (已过时) ASP.NET 1.开发基础 *asp.net以什么形式运行?.net宿主的概念,ISAPI的概念,ASP.NET基本运行机制; .net宿主的概念:CLR被实现为一个标准的COM服务器组 ...

  8. poj-2421-最小生成树刷题

    title: poj-2421-最小生成树刷题 date: 2018-11-20 20:30:29 tags: acm 刷题 categories: ACM-最小生成树 概述 做了几道最小生成树的题, ...

  9. 【Ray Tracing The Next Week 超详解】 光线追踪2-1

     Preface 博主刚放假回家就进了医院,今天刚完事儿,来续写第二本书  Ready 我们来总结一下上一本书的笔记中我们的一些规定 1. 数学表达式 我们采用小写粗黑体代表向量,大写粗黑体代表矩阵, ...

  10. SpringMVC(九) RequestMapping请求参数

    通过在控制器方法中使用@RequestParam(value="参数名",require=true/false,defaultvalue="")的方式,使在UR ...