123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- //
- // MCProxyTextViewRx.swift
- // HCQuanfangtong
- //
- // Created by Apple on 2022/4/11.
- // Copyright © 2022 Jyp. All rights reserved.
- //
- import Foundation
- import RxSwift
- import RxCocoa
- /// 自动注入
- @propertyWrapper class TextViewWrapper{
-
- var defaultValue : UITextView?
-
- /// 字数限制label
- weak var limitLabel : UILabel?
-
- /// 最大长度
- var maxLength : Int
-
- /// 输入结束事件
- var endBlock : ()->Void
-
-
- let disposeAutoRelease = DisposeBag()
- var wrappedValue : UITextView?{
- didSet{
- wrappedValue?.rx.bindDelegate(delegate: MCTextViewDelegate.init(maxLength: maxLength, limitLabel: limitLabel, endBlock: endBlock)).subscribe().disposed(by: disposeAutoRelease)
- }
- }
-
- init(wrappedValue defaultValue: UITextView? = nil, maxLength: Int, limitLabel: UILabel? = nil, endBlock: @escaping ()->Void){
- self.defaultValue = defaultValue
- self.maxLength = maxLength
- self.limitLabel = limitLabel
- self.endBlock = endBlock
- }
-
-
- }
- class MCTextViewDelegate : NSObject, UITextViewDelegate{
-
- /// 最大长度
- var maxLength : Int
-
- /// 字数限制label
- weak var limitLabel : UILabel?
-
- /// 输入结束事件
- var endBlock : ()->Void
-
-
- func textViewDidChangeSelection(_ textView: UITextView) {
- let toBeString = textView.text;
- let lang = textView.textInputMode?.primaryLanguage
- if lang == "zh-Hans"{
- let textRange = textView.markedTextRange
- if let range = textRange{
- if textView.position(from: range.start, offset: 0) != nil{
- return
- }
- }
- }
- if toBeString?.count ?? 0 > maxLength{
- let text = toBeString?.prefix(maxLength)
- textView.text = String(text ?? "")
- }
- limitLabel?.text = "\(textView.text.count)/\(maxLength)"
- }
-
- func textViewDidEndEditing(_ textView: UITextView){
- self.endBlock()
- }
-
-
- init(maxLength: Int, limitLabel: UILabel? = nil, endBlock: @escaping ()->Void) {
- self.maxLength = maxLength
- self.limitLabel = limitLabel
- self.endBlock = endBlock
- }
-
- }
- extension Reactive where Base:UITextView{
- var textDelegate : DelegateProxy<UITextView, UITextViewDelegate>{
- return TextViewDelegateProxy.proxy(for: base)
- }
-
- func bindDelegate(delegate:UITextViewDelegate)->RxSwift.Observable<[Any]>{
- self.base.delegate = delegate
- return textDelegate.methodInvoked(#selector(UITextViewDelegate.textViewDidChangeSelection(_:)))
- }
-
- }
- public class TextViewDelegateProxy : DelegateProxy<UITextView, UITextViewDelegate>,UITextViewDelegate,DelegateProxyType{
-
- public weak private(set) var textView : UITextView?
-
- init(textView: UITextView) {
- self.textView = textView
- super.init(parentObject: textView, delegateProxy: TextViewDelegateProxy.self)
- }
- public static func registerKnownImplementations() {
- self.register {
- TextViewDelegateProxy.init(textView: $0)
- }
- }
-
- public static func currentDelegate(for object: UITextView) -> UITextViewDelegate? {
- return object.delegate
- }
-
- public static func setCurrentDelegate(_ delegate: UITextViewDelegate?, to object: UITextView) {
- object.delegate = delegate
- }
-
- public override func setForwardToDelegate(_ delegate: DelegateProxy<UITextView, UITextViewDelegate>.Delegate?, retainDelegate: Bool) {
- super.setForwardToDelegate(delegate, retainDelegate: true)
- }
-
-
- }
|