XZ_iOS之实现textView占位文本和占位图片
2018-02-02 14:41
501 查看
最近项目需求,实现 textView 左侧有一个类似于 label 的 leftView 的图片效果 且 能自适应高度,所以,自定义了一个 textView 实现了效果,顺带着完善了一下,实现了 textView 设置占位字符,修改占位字符颜色/字体大小,设置最大行数、设置圆角等功能;
1、实现左侧图片的效果
效果图如下:
实现代码:
2、实现自适应高度
实现原理:监听 textView 的值的改变,并动态计算高度,如果高度小于初始值,使用初始值,否则使用计算的高度 block 回调更新 textView 的高度
实现代码:
3、实现占位字符
占位字符的原理很简单,在自定义的 UITextView 中自定义一个 UILabel的属性作为占位字符的视图即可
效果展示:
实现代码:
4、修改占位字符颜色/字体大小
实现代码:
5、设置最大行数
需要注意的是,如果设置了最大行数,在到达最大行数之前,需要将 textView 的滚动关闭,即 isScrollEnabled = false,否则在写的过程中,可能会出现上移然后回落的现象
实现代码:
6、设置圆角
实现代码:
最后附上demo地址,欢迎大家指错。
1、实现左侧图片的效果
效果图如下:
实现代码:
/// 设置图片 var hasImage:Bool? { didSet { if (hasImage != nil) && (hasImage == true) { addSubview(imageView) attributedText = imageText(font: UIFont.systemFont(ofSize: fontSize!)) } } } /// 将当前的图像转换成图片为属性文本 func imageText(font: UIFont) -> NSAttributedString { // 1.判断图像是否存在 guard let image = image else { return NSAttributedString.init(string: "") } // 2.创建文本附件 let attchment = NSTextAttachment() attchment.image = image let height = font.lineHeight attchment.bounds = CGRect(x: 0, y: -4, width: height, height: height) // 3.返回图片属性文本 let attrStrM = NSMutableAttributedString(attributedString: NSAttributedString(attachment: attchment)) // 设置字体属性 attrStrM.addAttribute(NSAttributedStringKey.font, value: font, range: NSRange(location: 0, length: 1)) // 4.返回属性文本 return attrStrM }
2、实现自适应高度
实现原理:监听 textView 的值的改变,并动态计算高度,如果高度小于初始值,使用初始值,否则使用计算的高度 block 回调更新 textView 的高度
实现代码:
/// textView 的值改变 @objc func textDidChanged() { // 1.占位文字是否隐藏 placeHolderView.isHidden = (text.lengthOfBytes(using: .utf8) > 0) // 2.计算高度 var height = CGFloat(ceilf(Float(sizeThatFits(CGSize(width: bounds.size.width , height: CGFloat(MAXFLOAT))).height))) // 2.1 设置最小高度: 初始高度 height = (height < orginalHeight) ? orginalHeight : height // 3.适配高度 if textHeight != height { // 仅适配高度,不限制行数 if maxHeight == CGFloat(MAXFLOAT) { isScrollEnabled = false }else { isScrollEnabled = ((height > maxHeight) && (maxHeight > 0)) } textHeight = height if isScrollEnabled == false { blockHeightChanged?(text, height) superview?.layoutIfNeeded() } } }
3、实现占位字符
占位字符的原理很简单,在自定义的 UITextView 中自定义一个 UILabel的属性作为占位字符的视图即可
效果展示:
实现代码:
/// 创建占位视图 func placeholderView() { placeHolderView.frame = bounds placeHolderView.font = font placeHolderView.textColor = UIColor.lightGray placeHolderView.backgroundColor = UIColor.clear addSubview(placeHolderView) } /// 占位字符 var placeHolder: String? { didSet{ if let _ = placeHolder { placeholderView() placeHolderView.text = placeHolder } } }
4、修改占位字符颜色/字体大小
实现代码:
/// 占位字符颜色 var placeholderColor: UIColor? { didSet{ if let _ = placeHolder { placeHolderView.te b709 xtColor = placeholderColor } } } /// 占位字符大小 var placeholderFontSize: CGFloat? { didSet{ if let _ = placeHolder { placeHolderView.font = UIFont.systemFont(ofSize: placeholderFontSize ?? fontSize!) } } }
5、设置最大行数
需要注意的是,如果设置了最大行数,在到达最大行数之前,需要将 textView 的滚动关闭,即 isScrollEnabled = false,否则在写的过程中,可能会出现上移然后回落的现象
实现代码:
/// 行数 如果是0,只换行 var numOfLines: Int? { didSet { let font = UIFont.systemFont(ofSize: fontSize!) if numOfLines == 0 { maxHeight = CGFloat(MAXFLOAT) return } /// 最大行高 maxHeight = CGFloat(ceilf(Float(font.lineHeight * CGFloat(numOfLines ?? 6) + textContainerInset.top + textContainerInset.bottom))) } }
6、设置圆角
实现代码:
/// 设置圆角 var cornerRadius:CGFloat? { didSet { layer.cornerRadius = cornerRadius ?? 0.0 } }
最后附上demo地址,欢迎大家指错。
相关文章推荐
- XZ_iOS之使用textView实现占位文字
- TextView 部分文字高亮、点击、添加超链接、替换字体、富文本、图片替换文字等实现
- ios UITableView实现无数据加载占位图片
- 自定义TextView,实现图片在文字之前的富文本效果
- 猫猫学iOS 之微博项目实战(11)发送微博自定义TextView实现带占位文字
- iOS实现文字环绕图片textView布局
- AJ学IOS 之微博项目实战(11)发送微博自定义TextView实现带占位文字
- iOS【textView或者textField文本输入字数限制】
- Android TextView 富文本加载实现
- 老罗安卓教程 在TextView文本中实现走马灯效果
- iOS TextField 弹出键盘时实现view整体上移下移:防止弹出键盘遮挡输入框
- 简易实现 TextView单行文本水平触摸滑动效果
- TextView布局文件中实现文本过长时滚动显示效果
- Android中Textview显示带html文本三-------【Textview显示网络图片】
- 一个简单的Android富文本TextView实现
- iOS开发UI篇—UIScrollView控件实现图片轮播
- TextView添加链接实现富文本label带点击功能
- Android中Textview显示带html文本三-------【Textview显示网络图片】
- [iOS]实现UITableView头部带有图片并且下拉图片放大效果
- iOS为imageView添加图片实现动画