当前位置: 首页 > article >正文

ios swift5 UITextView占位字符,记录限制字数

文章目录

  • 截图
  • 代码:具体使用
  • 代码:CustomTextView

截图

请添加图片描述

请添加图片描述

代码:具体使用

 scrollView.addSubview(contentTextView)
        contentTextView.placeholderLabel.text = LocalizableManager.localValue("write_comment")
        contentTextView.maxCharacters = 500
        contentTextView.backgroundColor = newUIBackColor
        contentTextView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(20)
            make.top.equalTo(problemLabel.snp_bottomMargin).offset(20)
            make.height.equalTo(150)
        }
        contentTextView.characterCountChanged = { [self] count in
            print("当前输入了 \(count) 个字符")
            textCountLabel.text = String(count) + "/500"
        }

代码:CustomTextView

class CustomTextView: UITextView {

    // 占位文字
     var placeholderLabel: UILabel!
    
    // 最大字符数
     var maxCharacters = 200

    // 输入字符数变化的回调
    var characterCountChanged: ((Int) -> Void)?
    
    // 初始化方法
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setup()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setup()
    }
    
    private func setup() {
        // 设置代理
        delegate = self
        
        //设置textView的字体大小
        self.font = UIFont.systemFont(ofSize: 15)
        
        // 创建并设置占位文字的标签
        placeholderLabel = UILabel()
        placeholderLabel.text = LocalizableManager.localValue("report_description_tips")
        placeholderLabel.font = self.font
        placeholderLabel.textColor = UIColor.lightGray
        placeholderLabel.numberOfLines = 0
        placeholderLabel.isHidden = !self.text.isEmpty
        addSubview(placeholderLabel)
        updatePlaceholderFrame()
        
       
    
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        updatePlaceholderFrame()    }
    
    private func updatePlaceholderFrame() {
        let placeholderSize = placeholderLabel.sizeThatFits(CGSize(width: self.frame.width - 10, height: CGFloat.greatestFiniteMagnitude))
        placeholderLabel.frame = CGRect(x: 5, y: 5, width: placeholderSize.width, height: placeholderSize.height)
    }
}

extension CustomTextView: UITextViewDelegate {
    func textViewDidChange(_ textView: UITextView) {
        // 更新占位文字的可见性
        placeholderLabel.isHidden = !textView.text.isEmpty
        
        // 限制输入的字符数
        if textView.text.count > maxCharacters {
            textView.text = String(textView.text.prefix(maxCharacters))
        }
        
        // 通知外部关于字符数的变化
        characterCountChanged?(textView.text.count)
    }
}

http://www.kler.cn/news/322558.html

相关文章:

  • MATLAB中的模型预测控制(MPC)实现详解
  • 我Github的问题解决了!
  • 吴恩达深度学习笔记:卷积神经网络(Foundations of Convolutional Neural Networks)2.3-2.4
  • 论文阅读:Omni-Kernel Network for Image Restoration
  • seL4 IPC(五)
  • (17)数据库neo4j数据备份
  • 鸿蒙​​​​​​保障应用开发安全的技术措施
  • Elasticsearch案例
  • Vue 23进阶面试题:(第六天)
  • 力扣面试150 寻找峰值 二分
  • msvcr100.dll丢失的解决方法,六种解决msvcr100.dll丢失使用技巧
  • 黄金短线交易策略:波动中的高效盈利之法
  • Python精选200Tips:176-180
  • Java中的Junit、类加载时机与机制、反射、注解及枚举
  • CMake set_property设置全局变量属性
  • 基于TSN的实时通信网络延迟评估技术
  • HDF5文件浏览软件--H5View
  • SpringCloud 2023各依赖版本选择、核心功能与组件、创建项目(注意事项、依赖)
  • 网站建设服务器租用的优点有哪些?
  • 苹果更新过时产品:三款 Mac 成“古董”,九款 Mac 彻底“停产”
  • 前端框架对比选择:如何在众多技术中找到最适合你的
  • 如何优化模型性能,探讨过拟合与欠拟合问题
  • 优数:助力更高效的边缘计算
  • 【刷题2—滑动窗口】最大连续1的个数lll、将x减到0的最小操作数
  • 傅里叶级数在机器人中的应用(动力学参数辨识)
  • ubuntu 设置静态IP
  • FileZilla Server 黑白单移除
  • 基于Spring Boot+Vue的减肥健康管理系统设计和实现【原创】(BMI算法,协同过滤算法、图形化分析)
  • 类和对象(3)
  • spring-boot web + vue