实现的效果如下:

 #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的封装和自定义的更多相关文章

  1. 使用xib封装一个自定义view的步骤

    使用xib封装一个自定义view的步骤 1> 新建一个继承UIView的自定义view,假设类名叫做(MJAppView) 2> 新建一个MJAppView.xib文件来描述MJAppVi ...

  2. OC - 31.通过封装的自定义布局快速实现商品展示

    概述 实现效果 设计思路 采用MVC架构,即模型—视图-控制器架构 使用MJExtension框架实现字典转模型 使用MJRefresh框架实现上拉和下拉刷新 上拉刷新,加载新的数据 下拉刷新,加载更 ...

  3. Springboot学习06-Spring AOP封装接口自定义校验

    Springboot学习06-Spring AOP封装接口自定义校验 关键字 BindingResult.Spring AOP.自定义注解.自定义异常处理.ConstraintValidator 前言 ...

  4. C#封装程序集自定义类方法注释提示

    一.为什么使用封装程序集: 在很多分布式应用程序开发中,针对每一种功能可能条用的接口不一样,往往习惯将需要被调用的接口,封装成DLL给调用方应用后使用,这样既规范了调用的方式,又避免了调用出现参数请求 ...

  5. Struts2 请求数据的自动封装 及 自定义转换器类

    请求数据自动封装: 实现原理:使用了参数拦截器.struts-default.xml中 <interceptor name="params" class="com. ...

  6. TextView加边框,自定义,上下左右四条线 颜色,想用哪个用哪个

    1.这是一个自定义的TextView ,看吧,底下就是代码,应该都可以看懂,这里就不多说了 package com.example.admin.myutilsborder;import android ...

  7. iOS-AFNetworking封装Get(自定义HTTP Header)和Post请求及文件下载

    前面提到AFNetworking是一个很强大的网络三方库,首先你需要引入AFNetworking三方库:如封装的有误还请指出,谢谢! 1.Get请求 /**Get请求 url 服务器请求地址 succ ...

  8. NoHttp封装--02 自定义请求

    bean实体类请求: 1.bean import java.io.Serializable; import com.alibaba.fastjson.annotation.JSONField; pub ...

  9. mybatis二(参数处理和map封装及自定义resultMap)

    .单个参数 mybatis不会做特殊处理. #{参数名/任意名}:取出参数值. .多个参数 mybatis会做特殊处理. 多个参数会被封装成 一个map. key:param1...paramN,或者 ...

随机推荐

  1. vs看源代码

    资源地址:https://www.cnblogs.com/HouZhiHouJueBlogs/p/4274197.html 资源地址:http://referencesource.microsoft. ...

  2. CF802C Heidi and Library (hard) 最小费用流

    你有一个容量为k的空书架,现在共有n个请求,每个请求给定一本书ai,如果你的书架里没有这本书,你就必须以ci的价格购买这本书放入书架. 当然,你可以在任何时候丢掉书架里的某本书.请求出完成这n个请求所 ...

  3. Pycharm----显示tab制表符

    设置前: 设置后: 操作方法:

  4. 「Django」Django内置email发送邮件

    Django内置email发送邮件 1.首先在settings.py文件设置相关参数 STATIC_URL = '/static/' # 设置邮件域名 EMAIL_HOST = 'smtp.163.c ...

  5. 执行mysql脚本文件

    一般都是连接mysql执行sql语句: 在命令行下输入 mysql -h localhost -u root -p回车,然后输入密码即可; 或直接运行mysql自带的连接工具,然后输入密码即可. 执行 ...

  6. Java内存模型与垃圾回收笔记

    内存模型 栈. 局部变量(基本类型)与对象引用:线程隔离.每个方法执行时会创建一个栈帧,存储局部变量等. 堆. 对象实例:线程共享. 方法区.类信息.常量(final).静态变量.符号引用: 线程共享 ...

  7. mysql 1040 连接数太多 mysql Error 1040 too many connection解决办法

    近在用SpringMVC开发的时候,突然出现1040 too many connection的错误,看错误的意思是连接的人数太多了.百度经验:jingyan.baidu.com 方法/步骤   1 当 ...

  8. P2461 [SDOI2008]递归数列 矩阵乘法+构造

    还好$QwQ$ 思路:矩阵快速幂 提交:1次 题解: 如图: 注意$n,m$如果小于$k$就不要快速幂了,直接算就行... #include<cstdio> #include<ios ...

  9. pyecharts 开发文档

    pyechart 新 版本 https://pyecharts.org/#/zh-cn/quickstart pyecharts 老版本 https://05x-docs.pyecharts.org/ ...

  10. Web service stop after running serveral hours

    Error Message: 1. Error:Web service call "Test" execution failed 2. Error:<CENTER>&l ...