剩余的 UITextView 字符数
UITextView characters remaining
如何为 UITextView
设置 UILabel
上剩余的字符数?
我已经为 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
}
有什么建议吗?
试试这个
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
}
}
Swift 5
extension AddProperty2ViewController: UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
p_summary_line_textview.text = "\(5000 - textView.text.count)"
}
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
return textView.text.count + (text.count - range.length) <= 5000
}
}
func textViewDidBeginEditing(_ textView: UITextView) {
shrinkText()
}
func textViewDidChange(_ textView: UITextView) {
shrinkText()
}
func textViewDidEndEditing(_ textView: UITextView) {
shrinkText()
}
func shrinkText() {
sendButton.isEnabled = reviewTextField.text == "" ? false : true
var text = ""
var counter = 0
if reviewTextField.text.count > limit {
reviewTextField.text.forEach {
if counter < limit {
counter += 1
text.append([=10=])
} else {
return
}
}
reviewTextField.text = text
}
counterLabel.text = "\(reviewTextField.text.count) / \(limit)"
}
如何为 UITextView
设置 UILabel
上剩余的字符数?
我已经为 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
}
有什么建议吗?
试试这个
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
}
}
Swift 5
extension AddProperty2ViewController: UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
p_summary_line_textview.text = "\(5000 - textView.text.count)"
}
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
return textView.text.count + (text.count - range.length) <= 5000
}
}
func textViewDidBeginEditing(_ textView: UITextView) {
shrinkText()
}
func textViewDidChange(_ textView: UITextView) {
shrinkText()
}
func textViewDidEndEditing(_ textView: UITextView) {
shrinkText()
}
func shrinkText() {
sendButton.isEnabled = reviewTextField.text == "" ? false : true
var text = ""
var counter = 0
if reviewTextField.text.count > limit {
reviewTextField.text.forEach {
if counter < limit {
counter += 1
text.append([=10=])
} else {
return
}
}
reviewTextField.text = text
}
counterLabel.text = "\(reviewTextField.text.count) / \(limit)"
}