PlaceholderTextView
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的更多相关文章
- 简易封装一个带有占位文字的TextView
在实际iOS应用开发中我们经常会用到类似于下图所示的界面,即带有占位文字的文本框:
- 设计带有placeHolder的TextView
设计带有placeHolder的TextView 效果: 源码: PlaceholderTextView.h 与 PlaceholderTextView.m // // PlaceholderText ...
- UINavigationbar跳转黑色
bug效果:导航栏过渡出现黑色
随机推荐
- STM32 串口通信使用奇偶校验
STM32串口通信如果使用奇偶校验,需要设置数据位长度为9bit USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USAR ...
- 【AtCoder】ARC098题解
C - Attention 枚举,计算前缀和即可 代码 #include <bits/stdc++.h> #define fi first #define se second #defin ...
- 020 shuffle的重要作用,以及分区的实践
一:学shuffle原理的必要性 1.说明 学习shuffle的作用是可以对程序进行优化. 在shuffle这个部分有三个部分需要注意: 分区 排序 分组 这个可以进行优化. 二:分区的实践 1.说明 ...
- word2013 如何设置从第三页开始编码 或 如何设置封面页和正文页页码不连续
首先说明一下 “分节符”作用,它就是用来将整个文档分节的,添加一个分节符,文档就分成1.2两节:添加两个分节符,文档就分成1.2.3节. 当前页面具体是第几节,可以通过点击页眉页脚来查看: 从第三页开 ...
- 利用 gdb 探究main(int argc, char *argv[]){} 中的char *argv[]
在 Linux 系统中编写小程序 代码如下 编译并采用gdb调试 在调试之前设置三个参数 a bb ccc 输入 start 执行代码到 return 0; 从这里可以看到 argc = ...
- 003.LVM扩容
一 LVM扩容步骤 创建分区 创建PV 扩容VG 扩容LV 载大小 二 创建分区 使用分区工具(如fdisk等)创建LVM分区,却将分区标识为LVM的分区类型8e. [root@kauai ~]# f ...
- leetcode 两数之和 python
两数之和 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 1 ...
- 【BZOJ-3730】震波 动态点分治 + 树状数组
3730: 震波 Time Limit: 15 Sec Memory Limit: 256 MBSubmit: 626 Solved: 149[Submit][Status][Discuss] D ...
- 微信小程序自定义音频组件,自定义滚动条,单曲循环,循环播放
小程序自定义音频组件,带滚动条 摘要:首先自定义音频组件,是因为产品有这样的需求,需要如下样式的 而微信小程序API给我们提供的就是这样的 而且产品需要小程序有后台播放功能,所以我们不考虑小程序的 a ...
- HTML解析利器HtmlAgilityPack
一个.NET下的HTML解析类库HtmlAgilityPack.HtmlAgilityPack是一个支持用XPath来解析HTML的类库,在花了一点时间学习了解HtmlAgilityPack的API和 ...