实现的效果如下:

 #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. 如何在jupyter中安装R

    地址:(http://irkernel.github.io/installation/) 第一步:在R中安装必备包 install.packages(c('repr', 'IRdisplay', 'e ...

  2. halcon基础数据类型详解

    #if defined(__CHAR_UNSIGNED__) || defined(__sgi) #define INT1 signed char /* integer, signed 1 Byte ...

  3. python协程初步--gevent库使用以及解释什么是猴子补丁monkey_patch

    协程工作的特点是遇到阻塞或耗时的任务时就切换,协程的生存依赖于线程,线程依赖于进程 一个似乎有点问题的例子 import gevent,time def kisscpc(num): for i in ...

  4. ACM-ICPC 2018 徐州赛区现场赛 I. Rikka with Sorting Networks (思维+DFS)

    题目链接:https://codeforces.com/gym/102012/problem/I 题意:问有多少个 1 到 n 的排列,使得用给定的 k 个比较器(使 au 和 av 有序)排序后,整 ...

  5. BZOJ 3672[NOI2014]购票(树链剖分+线段树维护凸包+斜率优化) + BZOJ 2402 陶陶的难题II (树链剖分+线段树维护凸包+分数规划+斜率优化)

    前言 刚开始看着两道题感觉头皮发麻,后来看看题解,发现挺好理解,只是代码有点长. BZOJ 3672[NOI2014]购票 中文题面,题意略: BZOJ 3672[NOI2014]购票 设f(i)f( ...

  6. using来定义类的别名,typedef,#define

    宏定义:其实就是替换作用 #define TRUE 1    //结尾无分号,宏名TRUE,计算机会把所有TRUE替换为1. typedef:定义类的别名 tpyedef unsigned int U ...

  7. 【题解】[Usaco2007 Open]Catch That Cow 抓住那只牛-C++

    题目DescriptionFarmer John has been informed of the location of a fugitive cow and wants to catch her ...

  8. 现在有没有一种富文本编辑器能够直接从 word 中复制粘贴公式的?

    tinymce是很优秀的一款富文本编辑器,可以去官网下载.https://www.tiny.cloud 这里分享的是它官网的一个收费插件powerpaste的旧版本源码,但也不影响功能使用. http ...

  9. 安装包设计-------安装(QT)---------知识总结

    1.判断文件是否存在 QFile file(path): file.exists(); 2.复制文件 bool copy(const QString &fileName, const QStr ...

  10. Solr 7.X 安装和配置--Linux篇

    1. 关闭防火墙和Selinux 2. 安装所需环境JDK 3. 下载Solr7.4版本 4. 下载并配置solr的中文分词器IK Analyzer 5. 启动Solr 6. 注意事项以及说明 1. ...