TextView的封装和自定义
实现的效果如下:

#import <UIKit/UIKit.h> @interface CustomTextView : UITextView @property (nonatomic , strong) UILabel *placeHolderLabel; // 默认的Label
@property (nonatomic , strong) NSString *placeholderStr; // 默认的文字显示
@property (nonatomic , strong) UIColor *palceHolderColor; //默认文字显示的颜色
@end
#import "CustomTextView.h"
@implementation CustomTextView
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self setPlaceholderStr:@""];
[self setPalceHolderColor:[UIColor lightGrayColor]];
}
return self;
}
// 接收数据
- (void)setPlaceholderStr:(NSString *)placeholderStr{
if (_placeholderStr != placeholderStr) {
_placeholderStr = placeholderStr;
// 防止创建多个
[self.placeHolderLabel removeFromSuperview];
self.placeHolderLabel = nil;
// 重新绘制 会调用drawRect方法
[self setNeedsDisplay];
}
}
- (void)drawRect:(CGRect)rect{
[super drawRect:rect];
if (self.placeholderStr.length > ) {
if (_placeHolderLabel == nil) {
_placeHolderLabel = [[UILabel alloc]initWithFrame:CGRectMake(, , self.bounds.size.width - , )];
_placeHolderLabel.lineBreakMode = NSLineBreakByWordWrapping;
_placeHolderLabel.numberOfLines = ;
_placeHolderLabel.font = self.font;
_placeHolderLabel.backgroundColor = [UIColor clearColor];
_placeHolderLabel.textColor = self.palceHolderColor;
_placeHolderLabel.alpha = ;
_placeHolderLabel.tag = ;
[self addSubview:_placeHolderLabel];
}
_placeHolderLabel.text = self.placeholderStr;
//自适应宽高
[_placeHolderLabel sizeToFit];
}
if ([[self text] length] == && [[self placeholderStr] length] >) {
[[self viewWithTag:] setAlpha:1.0];
}
}
使用如下:
#import "Button1Controller.h" #import "CustomTextView.h" #define kTextBorderColor RGBCOLOR(227,224,216)
#undef RGBCOLOR
#define RGBCOLOR(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1] @interface Button1Controller ()<UITextViewDelegate> @property (nonatomic,strong) CustomTextView *textView;
@property (nonatomic , strong) UIButton *commitButton; @end @implementation Button1Controller - (void)viewDidLoad {
[super viewDidLoad]; self.view.backgroundColor = [UIColor colorWithRed:229.0/ green:229.0/ blue:229.0/ alpha:1.0f]; [self.view addSubview:self.textView]; self.automaticallyAdjustsScrollViewInsets = NO; [self.view addSubview:self.commitButton]; } // TextView - (CustomTextView *)textView{ if (!_textView) {
_textView = [[CustomTextView alloc]initWithFrame:CGRectMake(, , self.view.frame.size.width - , )];
_textView.backgroundColor = [UIColor whiteColor];
_textView.delegate = self;
_textView.font = [UIFont systemFontOfSize:.f];
_textView.textColor = [UIColor blackColor];
_textView.textAlignment = NSTextAlignmentLeft;
_textView.editable = YES;
_textView.layer.cornerRadius = 4.0f;
_textView.layer.borderColor = kTextBorderColor.CGColor;
_textView.layer.borderWidth = 0.5;
_textView.palceHolderColor = RGBCOLOR(0x89, 0x89, 0x89);
_textView.placeholderStr = @"请输入您的宝贵意见,我们会尽快处理!";
} return _textView; } // CommutButton - (UIButton *)commitButton{ if (!_commitButton) {
_commitButton = [UIButton buttonWithType:UIButtonTypeCustom];
_commitButton.layer.cornerRadius = 2.0f;
_commitButton.frame = CGRectMake(, CGRectGetMaxY(self.textView.frame)+, self.view.frame.size.width - , );
_commitButton.backgroundColor = [self colorWithRGBHex:0x60cdf8];
[_commitButton setTitle:@"提交" forState:UIControlStateNormal];
[_commitButton addTarget:self action:@selector(sendFeedBack) forControlEvents:UIControlEventTouchUpInside];
} return _commitButton; } // 16进制转颜色 - (UIColor *)colorWithRGBHex:(UInt32)hex
{
int r = (hex >> ) & 0xFF;
int g = (hex >> ) & 0xFF;
int b = (hex) & 0xFF; return [UIColor colorWithRed:r / 255.0f
green:g / 255.0f
blue:b / 255.0f
alpha:1.0f];
} // 提交按钮被点击 - (void)sendFeedBack{ NSLog(@"提交..."); } // 判断如果用户输入\n,则收回键盘 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{ if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
} - (void)textViewDidBeginEditing:(UITextView *)textView{ self.textView.placeholderStr = @"";
}
TextView的封装和自定义的更多相关文章
- 使用xib封装一个自定义view的步骤
使用xib封装一个自定义view的步骤 1> 新建一个继承UIView的自定义view,假设类名叫做(MJAppView) 2> 新建一个MJAppView.xib文件来描述MJAppVi ...
- OC - 31.通过封装的自定义布局快速实现商品展示
概述 实现效果 设计思路 采用MVC架构,即模型—视图-控制器架构 使用MJExtension框架实现字典转模型 使用MJRefresh框架实现上拉和下拉刷新 上拉刷新,加载新的数据 下拉刷新,加载更 ...
- Springboot学习06-Spring AOP封装接口自定义校验
Springboot学习06-Spring AOP封装接口自定义校验 关键字 BindingResult.Spring AOP.自定义注解.自定义异常处理.ConstraintValidator 前言 ...
- C#封装程序集自定义类方法注释提示
一.为什么使用封装程序集: 在很多分布式应用程序开发中,针对每一种功能可能条用的接口不一样,往往习惯将需要被调用的接口,封装成DLL给调用方应用后使用,这样既规范了调用的方式,又避免了调用出现参数请求 ...
- Struts2 请求数据的自动封装 及 自定义转换器类
请求数据自动封装: 实现原理:使用了参数拦截器.struts-default.xml中 <interceptor name="params" class="com. ...
- TextView加边框,自定义,上下左右四条线 颜色,想用哪个用哪个
1.这是一个自定义的TextView ,看吧,底下就是代码,应该都可以看懂,这里就不多说了 package com.example.admin.myutilsborder;import android ...
- iOS-AFNetworking封装Get(自定义HTTP Header)和Post请求及文件下载
前面提到AFNetworking是一个很强大的网络三方库,首先你需要引入AFNetworking三方库:如封装的有误还请指出,谢谢! 1.Get请求 /**Get请求 url 服务器请求地址 succ ...
- NoHttp封装--02 自定义请求
bean实体类请求: 1.bean import java.io.Serializable; import com.alibaba.fastjson.annotation.JSONField; pub ...
- mybatis二(参数处理和map封装及自定义resultMap)
.单个参数 mybatis不会做特殊处理. #{参数名/任意名}:取出参数值. .多个参数 mybatis会做特殊处理. 多个参数会被封装成 一个map. key:param1...paramN,或者 ...
随机推荐
- cookies, session, token
Cookie 是由客户端(通常是浏览器)保存的小型文本信息,其内容是一系列的键值对,是由 HTTP 服务器设置并保存在浏览器上的信息. 在post请求的瞬间,cookie会被浏览器自动添加到请求头中. ...
- Oracle中查询表中数据的上次更新时间
目前找到的是以下方式,但是这种方式在表的数据量比较大的时候效率会比较慢. select to_char(scn_to_timestamp(max(ora_rowscn)),'YYYY-MM-DD HH ...
- springbatch
springbatch job的创建使用 job:作业,是批处理中的核心概念,是batch操作的基础单元,每个job由多个step组成 step:步骤,任务完成的节点 每个job是由JobBuildF ...
- Autodesk Maya 2019 安装
为什么我接触到建模了呢,我也不知道.只会弄点桌椅板凳简单动画,希望有时间更进一步学习,毕竟我还有一个开发游戏的梦想. 步骤:下载-安装-激活 Maya吧各版本合集下载 地址:https://pan.b ...
- 多线程锁:Mutex互斥体,Semaphore信号量,Monitor监视器,lock,原子操作InterLocked
Mutex类 “mutex”是术语“互相排斥(mutually exclusive)”的简写形式,也就是互斥量.互斥量跟临界区中提到的Monitor很相似,只有拥有互斥对象的线程才具有访问资源的权限, ...
- EMF中复制对象属性
1.简单的场景就是复制一个EObject,可以用工具类中的方法EcoreUtil.copy(). 2.场景:自己的TO类继承了EMF创建出的类,需要复制父类中的所有属性. /** * 将父类所有的属性 ...
- Educational Codeforces Round 72 (Rated for Div. 2) B题
Problem Description: You are fighting with Zmei Gorynich — a ferocious monster from Slavic myths, a ...
- checkbox选中的行数
$('[name=roomcheck]').each(function(){ if($(this).prop("checked")==true){ alert(this.id);/ ...
- java上传文件夹
我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用.此控件PC全平台支持包括mac,linux系统的文件上传,文章末尾将附上控件下载与教程链接 ...
- 【线性代数】5-1:行列式性质(The Properties of Determinants)
title: [线性代数]5-1:行列式性质(The Properties of Determinants) categories: Mathematic Linear Algebra keyword ...