有点潦草简单贴一下,主要是继承UITextView,重写init方法添加placeholderLabel然后根据监听的文字控制显隐
//
// LFTextView.swift
//
// Created by 刘刘峰 on 2019/11/25.
// Copyright © 2019 刘刘峰. All rights reserved.
//
import UIKit
@objc protocol LFTextViewDelegate {
@objc optional func textDidChange(text:String?)->Void
}
class LFTextView: UITextView {
weak var textDelegate:LFTextViewDelegate?
///占位标签
public lazy var placeholderLabel = UILabel()
///定义最大高度
var maxHeight:CGFloat = 100
//设置占位符y坐标 默认0
var placeHolderMarginY:CGFloat? {
didSet {
placeholderLabel.snp_remakeConstraints{ (make) in
make.left.equalToSuperview().offset(10)
make.right.equalToSuperview().offset(-10)
make.height.equalTo(20)
make.top.equalToSuperview().offset(self.placeHolderMarginY!)
}
}
}
var maxLength:Int = 60
var currentLength:Int = 0
lazy var lengthLab:UILabel = {
var label = UILabel()
label.font = FONT(font: 12)
label.textAlignment = .right
label.textColor = AppColor.darkgGray
return label
}()
var showLength:Bool = true
override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame,textContainer:textContainer)
setupUI()
setupInputAccessoryView()
}
override func layoutSubviews() {
super.layoutSubviews()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
NotificationCenter.default.removeObserver(self)
}
// MARK: - 监听text改变方法
@objc fileprivate func textChanged(noti:Notification) {
placeholderLabel.isHidden = self.hasText
DLog(noti.object)
let textView = noti.object as? LFTextView
if self.textDelegate != nil {
self.textDelegate?.textDidChange?(text:textView?.text)
}
}
@objc fileprivate func finishInput() {
self.resignFirstResponder()
}
}
fileprivate extension LFTextView {
func setupUI() {
NotificationCenter.default.addObserver(self, selector: #selector(textChanged(noti:)), name:UITextView.textDidChangeNotification, object: self)
let contentView = UIView()
addSubview(contentView)
//否则会拦截输入手势
contentView.isUserInteractionEnabled = false
contentView.snp_makeConstraints { (make) in
make.edges.equalToSuperview()
make.width.equalToSuperview()
make.height.equalToSuperview()
}
placeholderLabel.font = self.font
placeholderLabel.textColor = AppColor.lightGray
contentView.addSubview(placeholderLabel)
placeholderLabel.snp_makeConstraints { (make) in
make.left.equalToSuperview().offset(10)
make.right.equalToSuperview().offset(-10)
make.height.equalTo(20)
make.centerY.equalToSuperview()
}
delegate = self
}
func setupInputAccessoryView() {
let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 44.0))
let doneItem = UIBarButtonItem(title: "完成", style: .done, target: self, action: #selector(finishInput))
let flexibleItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
toolbar.items = [flexibleItem, doneItem]
self.inputAccessoryView = toolbar
}
}
// MARK: - UITextViewDelegate
extension LFTextView: UITextViewDelegate {
public func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if range.location > maxLength-1 {
return false
} else {
return true
}
}
func textViewDidChange(_ textView: UITextView) {
//获取frame的值
let frame = textView.frame
//定义一个constrainSize值用于计算textview的高度
let constrainSize = CGSize(width:frame.size.width,height:CGFloat(MAXFLOAT))
//获取textview的真实高度
var size = textView.sizeThatFits(constrainSize)
//如果textview的高度大于最大高度高度就为最大高度并可以滚动,否则不能滚动
if size.height >= maxHeight{
size.height = maxHeight
textView.isScrollEnabled = true
}else{
textView.isScrollEnabled = false
}
//重新设置textview的高度
textView.frame.size.height = size.height
currentLength = textView.text.length
self.lengthLab.text = "\(currentLength)/\(maxLength)"
}
}












网友评论