Rod*_*kes 2 uitextview uitextviewdelegate ios swift
如何设置残留在人物UILabel的UITextView?
我已经为UITextField,但同样的代码不起作用..
这是我尝试过的:
func textView(textView: UITextView, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
if string == ""
{
if plainTextView.text!.characters.count == 0
{
charCount = 0
countLabel.text = String(format: "%i Characters Left", maxLength - charCount)
return false
}
charCount = (plainTextView.text!.characters.count - 1)
countLabel.text = String(format: "%i Characters Left", maxLength - charCount)
return true
}
else
{
charCount = (plainTextView.text!.characters.count + 1)
countLabel.text = String(format: "%i Characters Left", maxLength - charCount)
if charCount >= maxLength + 1
{
charCount = maxLength
countLabel.text = String(format: "%i Characters Left", maxLength - charCount)
return false;
}
}
return true
}
Run Code Online (Sandbox Code Playgroud)
有什么建议?
Anb*_*hik 10
尝试这个
import UIKit
class ViewController: UITextViewDelegate {
let maxLenghth = 200
func textViewDidChange(_ textView: UITextView) {
countLabel.text = "\(maxLength - textView.text.count)"
}
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
return textView.text.count + (text.count - range.length) <= maxLength
}
}
Run Code Online (Sandbox Code Playgroud)