美文网首页
自定义带占位符的LFTextView

自定义带占位符的LFTextView

作者: 菲特峰 | 来源:发表于2020-09-19 16:45 被阅读0次

有点潦草简单贴一下,主要是继承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)"
        
        
    }
}


相关文章

  • 自定义带占位符的LFTextView

    有点潦草简单贴一下,主要是继承UITextView,重写init方法添加placeholderLabel然后根据监...

  • IOS基本 控件系列

    自定义形状背景 带占位符的当行文本输入框

  • UITextView(占位字符,限制字数)

    请关注,防止你用了,我改了,有问题连个商量的人都找不到... 自定义带占位符的TextView 设置占位符方式千奇...

  • 第7章 格式修正

    7.1 自定义格式 自定义格式——0与文本 “0”是数字占位符 自定义格式——中文大写 自定义格式——文本占位符 ...

  • 莹莹

    占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符...

  • 莹莹

    占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符...

  • 语言元素及列表学习

    语言元素 占位符 %d是整数的占位符,%f是小数的占位符,%%表示百分号(因为百分号代表了占位符,所以带占位符的字...

  • android View预加载占位图

    导入依赖 添加带渐变动画的占位符 添加自定义动画的占位符 添加默认的占位图 最后在数据加载完成调用方法关闭占位图 ...

  • 三、@PathVariable

    3.1@PathVariable 映射 URL 绑定的占位符 带占位符的 **URL **是 **Spring3....

  • Excel操作--自定义格式与条件计数

    来源:猴子数据分析 自定义格式:@文本占位符,0 数字占位符 条件计数函数 countif(区域,条件) 条件平均...

网友评论

      本文标题:自定义带占位符的LFTextView

      本文链接:https://www.haomeiwen.com/subject/sdaeyktx.html