1.gif演示
代码button
import UIKit
class ButtonVC: UIViewController {
@IBOutlet var placeholderLabel: UILabel!
@IBOutlet var textView: UITextView!
@IBOutlet var buttonView: UIView!
typealias StrClosure = (String) -> Void
var backClosure: StrClosure?
override func viewDidAppear(_ animated: Bool) {
addBtn()
}
func addRightBarBtnItem() {
let item = UIBarButtonItem(title: "完成", style: .plain, target: self, action: #selector(rightBarBtnItemClick));
self.navigationItem.rightBarButtonItem = item
}
@objc func rightBarBtnItemClick() {
backClosure!(textView.text)
navigationController?.popViewController(animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
title = "编辑(button)"
textView.delegate = self
textView.becomeFirstResponder()
textView.inputView = UIView()
addRightBarBtnItem()
}
func addBtn() {
let width = buttonView.width/4
let height = buttonView.height/4
let contentStr = "0123456789abcdef"
for i in 0...15 {
let row = i % 4
let list = i / 4
let btn = UIButton(frame: CGRect.init(x: width*CGFloat(row), y: height*CGFloat(list), width: width, height: height))
let index: String.Index = contentStr.index(contentStr.startIndex, offsetBy: i)
btn.tag = i
btn.setTitle(""+contentStr[index...index], for: .normal)
btn.addTarget(self, action: #selector(btnClick(btn:)), for: .touchUpInside)
buttonView.addSubview(btn)
}
let btn = UIButton(frame: CGRect(x: view.width - 50, y: buttonView.y - 60, width: 50, height: 50))
btn.setImage(UIImage(named: "删除"), for: .normal)
view.addSubview(btn)
btn.tag = -1
btn.addTarget(self, action: #selector(btnClick(btn:)), for: .touchUpInside)
}
@objc func btnClick(btn: UIButton) {
textView.text += btn.titleLabel?.text ?? ""
if btn.tag == -1 {
if textView.text.count > 0 {
textView.text.removeLast()
}else{
placeholderLabel.isHidden = false
}
}
if placeholderLabel.isHidden == false && textView.text.count > 0 {
placeholderLabel.isHidden = true
}
}
}
extension ButtonVC: UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
if textView.text.count > 0 {
placeholderLabel.isHidden = true
}else {
placeholderLabel.isHidden = false
}
}
}
代码 UICollectionView
参考博客:
iOS swift UICollectionView的基本使用
//
// collectionViewVC.swift
// 十六进制键盘
//
// Created by macvivi on 2021/1/8.
//
import UIKit
class CollectionViewVC: UIViewController {
@IBOutlet var textView: UITextView!
@IBOutlet var placeholderLabel: UILabel!
@IBOutlet var collectionView: UICollectionView!
typealias StrClosure = (String) -> Void
var backClosure: StrClosure?
func addRightBarBtnItem() {
let item = UIBarButtonItem(title: "完成", style: .plain, target: self, action: #selector(rightBarBtnItemClick));
self.navigationItem.rightBarButtonItem = item
}
@objc func rightBarBtnItemClick() {
backClosure!(textView.text)
navigationController?.popViewController(animated: true)
}
let content = "0123456789abcdef"
override func viewDidLoad() {
super.viewDidLoad()
title = "编辑(collectionView)"
addRightBarBtnItem()
setupUI()
collectionView.isHidden = true
}
override func viewDidAppear(_ animated: Bool) {
collectionView.isHidden = false
collectionView.collectionViewLayout = layout
addCancelBtn()
}
func setupUI() {
textView.delegate = self
textView.becomeFirstResponder()
textView.inputView = UIView()
collectionView.dataSource = self
collectionView.delegate = self
}
func addCancelBtn() {
let btn = UIButton(frame: CGRect(x: view.width - 50, y: collectionView.y - 60, width: 50, height: 50))
btn.setImage(UIImage(named: "删除"), for: .normal)
view.addSubview(btn)
btn.addTarget(self, action: #selector(btnClick(btn:)), for: .touchUpInside)
}
@objc func btnClick(btn: UIButton) {
if textView.text.count > 0 {
textView.text.removeLast()
}
if textView.text.count > 0 {
placeholderLabel.isHidden = true
}else {
placeholderLabel.isHidden = false
}
}
lazy var layout: UICollectionViewFlowLayout = {
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 1
layout.minimumInteritemSpacing = 1
layout.sectionInset = UIEdgeInsets(top: 2, left: 2, bottom: 2, right: 2)
layout.itemSize = CGSize(width: collectionView.width/4.1, height: collectionView.height/4.1)
return layout
}()
}
extension CollectionViewVC: UICollectionViewDataSource,UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return content.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell: ContentCell = collectionView.dequeueReusableCell(withReuseIdentifier: "ContentCell", for: indexPath) as! ContentCell
let index = content.index(content.startIndex, offsetBy: indexPath.row)
cell.contentLabel.text = String(content[index...index])
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell: ContentCell = collectionView.cellForItem(at: indexPath)! as! ContentCell
textView.text += cell.contentLabel.text!
placeholderLabel.isHidden = true
}
}
extension CollectionViewVC: UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
print(#function)
if textView.text.count > 0 {
placeholderLabel.isHidden = true
}else {
placeholderLabel.isHidden = false
}
}
}
2.demo的github网址
3. 相关知识点
3.1 获取焦点但不弹出键盘
textView.becomeFirstResponder()
textView.inputView = UIView()
相关博客:
这个比较全:多功能自定义键盘 - cocoachina
iOS开发小技巧–自定义带有占位文字的TextView(两种方式)
Swift编写的字母和数字键盘
iOS中收起键盘的几种方式