当前位置:网站首页>Swift - enables textview to be highly adaptive
Swift - enables textview to be highly adaptive
2022-07-05 12:08:00 【RRJia】
Realize the idea :
Use UITextViewDelegate In the agency “textViewDidChange() Method ” Realization .
Implementation method :
Step one : establish TextViewAdapt.swift file , Add to TextView Control , And set up textView.delegate = self( Be sure to set up the proxy , Otherwise it doesn't work !!).
import UIKit
class TextViewAdapt: UIViewController {
private var label = UILabel()
private var textView = UITextView()
override func viewDidLoad() {
// add to label Control
self.label.frame = CGRect(x: 50, y: 150, width: 60, height: 30)
self.label.backgroundColor = .yellow
// Set the prompt text to “ Please enter ”
self.label.text = " Please enter "
self.view.addSubview(self.label)
// add to textView Control
self.textView.frame = CGRect(x: 130, y: 150, width: 200, height: 30)
// Set up textView The border width of
self.textView.layer.borderWidth = CGFloat(1)
// Set up textView The border color of
self.textView.layer.borderColor = UIColor.black.cgColor
self.view.addSubview(self.textView)
// ( important !! Must do !!) Set up textView Agent for , And implement the corresponding methods in the extension class !!!
self.textView.delegate = self
}
}
Step two : Because step 1 is set textView.delegate = self, So here we are TextViewAdapt In the extension class of Implementing agent methods .
// Realization TextView agent
extension TextViewAdapt: UITextViewDelegate {
// textView Every time it changes , Will call this method
func textViewDidChange(_ textView: UITextView) {
// Preset textView Size , Set the width to a fixed width , The height is set to CGFloat The maximum of
let presetSize = CGSize(width: textView.frame.size.width, height: CGFloat.greatestFiniteMagnitude)
// Recalculate textView Size
let newSize = textView.sizeThatFits(presetSize)
// to update textView Size
self.textView.frame = CGRect(origin: CGPoint(x: textView.frame.origin.x, y: textView.frame.origin.y), size: CGSize(width: textView.frame.size.width, height: newSize.height))
}
}
result :TextView Highly adaptive success
边栏推荐
- 全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
- 多表操作-自关联查询
- 13. (map data) conversion between Baidu coordinate (bd09), national survey of China coordinate (Mars coordinate, gcj02), and WGS84 coordinate system
- 语义分割实验:Unet网络/MSRC2数据集
- Codeworks 5 questions per day (1700 average) - day 5
- 【yolov3损失函数】
- 【Win11 多用户同时登录远程桌面配置方法】
- 简单解决redis cluster中从节点读取不了数据(error) MOVED
- July Huaqing learning-1
- codeforces每日5题(均1700)-第五天
猜你喜欢
[singleshotmultiboxdetector (SSD, single step multi frame target detection)]
redis主从中的Master自动选举之Sentinel哨兵机制
[loss functions of L1, L2 and smooth L1]
How to clear floating?
【Win11 多用户同时登录远程桌面配置方法】
Multi table operation - Auto Association query
《增长黑客》阅读笔记
liunx禁ping 详解traceroute的不同用法
你做自动化测试为什么总是失败?
[configuration method of win11 multi-user simultaneous login remote desktop]
随机推荐
What is the difference between canvas and SVG?
Uniapp + unicloud + Unipay realize wechat applet payment function
Principle of redis cluster mode
Time tools
1个插件搞定网页中的广告
【ijkplayer】when i compile file “compile-ffmpeg.sh“ ,it show error “No such file or directory“.
[yolov5.yaml parsing]
Yolov5 target detection neural network -- calculation principle of loss function
强化学习-学习笔记3 | 策略学习
报错ModuleNotFoundError: No module named ‘cv2.aruco‘
Riddle 1
Matlab imoverlay function (burn binary mask into two-dimensional image)
想问问,如何选择券商?在线开户是很安全么?
11.(地图数据篇)OSM数据如何下载使用
What is digital existence? Digital transformation starts with digital existence
Troubleshooting of high memory usage of redis in a production environment
Codeforces Round #804 (Div. 2)
redis主从模式
pytorch-softmax回归
[singleshotmultiboxdetector (SSD, single step multi frame target detection)]