Swift3.0 UITextView写反馈界面

 

效果图  

适配用的 SnapKit

使用介绍:  http://www.hangge.com/blog/cache/detail_1097.html

 1 private func creationTextView(){
 2         
 3         let viewBg = UIView()
 4         self.view.addSubview(viewBg)
 5         viewBg.frame = CGRect(x:20, y:84, width:SCREEN_WITH - 40, height:220)
 6         viewBg.backgroundColor = UIColor.white
 7         viewBg.layer.borderColor = UIColor.init(red: 208/255.0, green: 208/255.0, blue: 208/255.0, alpha: 1).cgColor
 8         viewBg.layer.borderWidth = 1
 9         viewBg.layer.cornerRadius = 3
10         viewBg.clipsToBounds = true
11         
12         
13         let textView = UITextView()
14         viewBg.addSubview(textView)
15         
16         textView.delegate = self
17         textView.backgroundColor = UIColor.white
18         textView.tintColor = UIColor.red
19         textView.font = UIFont.systemFont(ofSize: 18)
20         textView.textAlignment = .left
21         textView.contentInset = UIEdgeInsetsMake(0, 0, 60, 0)
22         //可以滚动
23         textView.isScrollEnabled = true
24         //自适应高度
25         textView.autoresizingMask = UIViewAutoresizing.flexibleHeight
26         
27         textView.snp.makeConstraints { (mark) in
28             mark.top.equalTo(1)
29             mark.left.equalTo(1)
30             mark.right.equalTo(1)
31             mark.bottom.equalTo(-22)
32         }
33         
34         //反馈字符长度限制
35         viewBg.addSubview(label_length)
36         label_length.text = "150"
37         label_length.textColor = ColorViewBG
38         
39         label_length.textAlignment = .right
40         label_length.snp.makeConstraints { (mark) in
41             mark.bottom.equalTo(-10)
42             mark.right.equalTo(-8)
43             mark.height.equalTo(12)
44             mark.width.equalTo(100)
45         }
46         
47         
48     }

  简单的计算字符长度,在这里输入内容超过时没有做限制和提醒!

1  // MARK: -TextViewDelegate
2     
3     func textViewDidChange(_ textView: UITextView) {
4         //计算剩余可输入字符长度
5         let length = textView.text.characters.count
6         label_length.text = "\(150 - length)"
7         
8         
9     }

 

  

转载于:https://www.cnblogs.com/xingsmile/p/6122800.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。
好的,下面是一个使用Swift语言编的富文本编辑器用户界面的示例: ```swift import UIKit class RichTextEditorViewController: UIViewController, UITextViewDelegate { var textView: UITextView! var toolbar: UIToolbar! override func viewDidLoad() { super.viewDidLoad() // 设置文本视图 textView = UITextView(frame: view.bounds) textView.autoresizingMask = [.flexibleWidth, .flexibleHeight] textView.delegate = self view.addSubview(textView) // 设置工具栏 toolbar = UIToolbar(frame: CGRect(x: 0, y: view.bounds.height - 44, width: view.bounds.width, height: 44)) toolbar.autoresizingMask = [.flexibleWidth, .flexibleTopMargin] view.addSubview(toolbar) // 添加工具栏按钮 let boldButton = UIBarButtonItem(title: "B", style: .plain, target: self, action: #selector(boldButtonTapped)) let italicButton = UIBarButtonItem(title: "I", style: .plain, target: self, action: #selector(italicButtonTapped)) let underlineButton = UIBarButtonItem(title: "U", style: .plain, target: self, action: #selector(underlineButtonTapped)) let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil) toolbar.items = [boldButton, italicButton, underlineButton, flexibleSpace] // 设置文本视图的样式 let font = UIFont.systemFont(ofSize: 16) let attributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: font] textView.typingAttributes = attributes } // 处理工具栏按钮的点击事件 @objc func boldButtonTapped() { let range = textView.selectedRange let existingAttributes = textView.textStorage.attributes(at: range.location, effectiveRange: nil) var newAttributes = existingAttributes newAttributes[NSAttributedString.Key.font] = UIFont.boldSystemFont(ofSize: 16) textView.textStorage.addAttributes(newAttributes, range: range) } @objc func italicButtonTapped() { let range = textView.selectedRange let existingAttributes = textView.textStorage.attributes(at: range.location, effectiveRange: nil) var newAttributes = existingAttributes newAttributes[NSAttributedString.Key.font] = UIFont.italicSystemFont(ofSize: 16) textView.textStorage.addAttributes(newAttributes, range: range) } @objc func underlineButtonTapped() { let range = textView.selectedRange let existingAttributes = textView.textStorage.attributes(at: range.location, effectiveRange: nil) var newAttributes = existingAttributes newAttributes[NSAttributedString.Key.underlineStyle] = NSUnderlineStyle.single.rawValue textView.textStorage.addAttributes(newAttributes, range: range) } // UITextViewDelegate方法 func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { // 处理删除样式 if text == "" { let existingAttributes = textView.textStorage.attributes(at: range.location, effectiveRange: nil) var newAttributes = existingAttributes newAttributes[NSAttributedString.Key.font] = UIFont.systemFont(ofSize: 16) newAttributes[NSAttributedString.Key.underlineStyle] = NSUnderlineStyle.none.rawValue textView.textStorage.addAttributes(newAttributes, range: range) } return true } } ``` 这个示例代码包括一个文本视图和一个工具

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值