我正在给一个文本视图来推特一些字符串。
我正在应用以下方法将字符数限制为140个。
import UIKit
import SVProgressHUD
import Toaster
class ViewController: UIViewController , UITextViewDelegate ,
UITextFieldDelegate {
var checktoaster = false
@IBOutlet weak var txtDescription: UITextView!
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if txtDescription == textView
{
guard let tex = textView.text else { return true }
let newLength = tex.characters.count + text.characters.count - range.length
if !(newLength <= 140)
{
if checktoaster == false
{
checktoaster = true
let toast = Toast(text: "You have write only 140 character.", duration: Delay.long)
toast.show()
}
}
else
{
checktoaster = false
}
return newLength <= 140 //Bool
}
return true
}
}
这是textView类
import UIKit
public class LetsTextView : UITextView , UITextViewDelegate{
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
NotificationCenter.default.addObserver(self, selector: #selector(LetsTextView.refreshPlaceholder), name: NSNotification.Name.UITextViewTextDidChange, object: self)
}
override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
NotificationCenter.default.addObserver(self, selector: #selector(LetsTextView.refreshPlaceholder), name: NSNotification.Name.UITextViewTextDidChange, object: self)
}
override public func awakeFromNib() {
super.awakeFromNib()
NotificationCenter.default.addObserver(self, selector: #selector(LetsTextView.refreshPlaceholder), name: NSNotification.Name.UITextViewTextDidChange, object: self)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
@IBInspectable public var placeholder : String? {
get {
return placeholderLabel?.text
}
set {
if placeholderLabel == nil {
var frm = self.bounds.insetBy(dx: 5, dy: 6)
frm.size.height = 20
placeholderLabel = UILabel(frame:frm)
if let unwrappedPlaceholderLabel = placeholderLabel {
unwrappedPlaceholderLabel.autoresizingMask = [.flexibleWidth, .flexibleHeight]
unwrappedPlaceholderLabel.lineBreakMode = .byWordWrapping
unwrappedPlaceholderLabel.numberOfLines = 0
unwrappedPlaceholderLabel.font = self.font
unwrappedPlaceholderLabel.backgroundColor = UIColor.clear
unwrappedPlaceholderLabel.textColor = UIColor.gray//UIColor(red: 255/255, green: 190/255, blue: 70/255, alpha: 1.0)
unwrappedPlaceholderLabel.alpha = 0
addSubview(unwrappedPlaceholderLabel)
}
}
placeholderLabel?.text = newValue
refreshPlaceholder()
}
}
func refreshPlaceholder() {
if text.characters.count != 0 {
placeholderLabel?.alpha = 0
} else {
placeholderLabel?.alpha = 1
}
}
override public var text: String! {
didSet {
refreshPlaceholder()
}
}
override public var font : UIFont? {
didSet {
if let unwrappedFont = font {
placeholderLabel?.font = unwrappedFont
} else {
placeholderLabel?.font = UIFont.systemFont(ofSize: 12)
}
}
}
override public var delegate : UITextViewDelegate? {
get {
refreshPlaceholder()
return super.delegate
}
set {
}
}
}
// 任何解决方案
答案 0 :(得分:3)
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if txtTwitt.text.characters.count <= 140
{
// Code here
}
else
{
//Code Here
}
return true
}