QMUITextView

@interface QMUITextView : UITextView

自定义 UITextView,提供的特性如下:

  1. 支持 placeholder 并支持更改 placeholderColor;若使用了富文本文字,则 placeholder 的样式也会跟随文字的样式(除了 placeholder 颜色)
  2. 支持在文字发生变化时计算内容高度并通知 delegate。
  3. 支持限制输入框最大高度,一般配合第 2 点使用。
  4. 支持限制输入的文本的最大长度,默认不限制。
  5. 修正系统 UITextView 在输入时自然换行的时候,contentOffset 的滚动位置没有考虑 textContainerInset.bottom
  • Undocumented

    Declaration

    Objective-C

    @property(nonatomic, weak) id<QMUITextViewDelegate> delegate

    Swift

    weak var delegate: QMUITextViewDelegate! { get set }
  • 当通过 setText:setAttributedText:等方式修改文字时,是否应该自动触发 UITextViewDelegate 里的 textView:shouldChangeTextInRange:replacementText:textViewDidChange: 方法

    默认为YES(注意系统的 UITextView 对这种行为默认是 NO)

    Declaration

    Objective-C

    @property (nonatomic) BOOL shouldResponseToProgrammaticallyTextChanges;

    Swift

    var shouldResponseToProgrammaticallyTextChanges: Bool { get set }
  • 显示允许输入的最大文字长度,默认为 NSUIntegerMax,也即不限制长度。

    Declaration

    Objective-C

    @property (nonatomic) NSUInteger maximumTextLength;

    Swift

    var maximumTextLength: UInt { get set }
  • 在使用 maximumTextLength 功能的时候,是否应该把文字长度按照 [NSString (QMUI) qmui_lengthWhenCountingNonASCIICharacterAsTwo] 的方法来计算。 默认为 NO。

    Declaration

    Objective-C

    @property (nonatomic) BOOL shouldCountingNonASCIICharacterAsTwo;

    Swift

    var shouldCountingNonASCIICharacterAsTwo: Bool { get set }
  • placeholder 的文字

    Declaration

    Objective-C

    @property (nonatomic, copy) NSString *placeholder;

    Swift

    var placeholder: String! { get set }
  • placeholder 文字的颜色

    Declaration

    Objective-C

    @property (nonatomic, strong) UIColor *placeholderColor;

    Swift

    var placeholderColor: UIColor! { get set }
  • placeholder 在默认位置上的偏移(默认位置会自动根据 textContainerInset、contentInset 来调整)

    Declaration

    Objective-C

    @property (nonatomic) UIEdgeInsets placeholderMargins;

    Swift

    var placeholderMargins: UIEdgeInsets { get set }
  • 最大高度,当设置了这个属性后,超过这个高度值的 frame 是不生效的。默认为 CGFLOAT_MAX,也即无限制。

    Declaration

    Objective-C

    @property (nonatomic) CGFloat maximumHeight;

    Swift

    var maximumHeight: CGFloat { get set }
  • 控制输入框是否要出现“粘贴”menu

    Declaration

    Objective-C

    @property (nonatomic, copy) BOOL (^)(id, BOOL) canPerformPasteActionBlock;

    Swift

    var canPerformPasteActionBlock: ((Any?, Bool) -> Bool)! { get set }

    Parameters

    sender

    触发这次询问事件的来源

    superReturnValue

    [super canPerformAction:withSender:] 的返回值,当你不需要控制这个 block 的返回值时,可以返回 superReturnValue

    Return Value

    控制是否要出现“粘贴”menu,YES 表示出现,NO 表示不出现。当你想要返回系统默认的结果时,请返回参数 superReturnValue

  • 当输入框的“粘贴”事件被触发时,可通过这个 block 去接管事件的响应。

    Declaration

    Objective-C

    @property (nonatomic, copy) BOOL (^)(id) pasteBlock;

    Swift

    var pasteBlock: ((Any?) -> Bool)! { get set }

    Parameters

    sender

    “粘贴”事件触发的来源,例如可能是一个 UIMenuController

    Return Value

    返回值用于控制是否要调用系统默认的 paste: 实现,YES 表示执行完 block 后继续调用系统默认实现,NO 表示执行完 block 后就结束了,不调用 super。