Simple implementation of personalized signature edit box

In the development process, the input box should be designed to demand all sorts of strange things. Here to introduce personalized signature type edit box. This edit box can be used on many occasions: messages, feedback, complaints, etc..

Simple implementation of personalized signature edit box
personalized signature edit box

Thank you, and I suggest you download the sample to see. There are a lot of details I haven’t written here.

I. conceiving

This edit box, I’m mostly implemented in UITableViewCell. Only two controls are used:

  • UITextView: input box
  • UILabel: information tip: 1, the upper left placeholder. 2, the lower right of the text count.

The layout is simple, and it won’t take 1 minutes. However, for the upper left placeholder, the Label needs careful fine-tuning.

Two, association attributes

The code reads as follows:

/ / edit box @property (weak, nonatomic) IBOutlet UITextView *textView; / / placeholder text controls @property (weak, nonatomic) IBOutlet UILabel *placeholderLabel; / / count (weak, nonatomic) @property controls IBOutlet UILabel *countLabel;

Where UITextView needs to set the proxy.

Three, the core code

The implementation of this edit box function is mainly the implementation of the UITextViewDelegate proxy method. The specific implementation of the core agent method is as follows:

#pragma mark - #pragma mark - UITextViewDelegate - (void) textViewDidChange: (UITextView * textView) {/ / must be in this mess. (textView.text.length > self.placeholderLabel.hidden = 0); / / UITextRange; {highlight *selectedRange = [textView markedTextRange]; / / positionFromPosition acquisition based on from offset offset cursor position. / / return to another location of the text in a given offset of the location of the text. UITextPosition *pos = [textView positionFromPosition:selectedRange.start offset:0]; / / if change is highlighted in the part of change, not counting the characters of if (selectedRange & & POS) {return}}; / / agent if (self.delegate & & [self.delegate; respondsToSelector:@selector (setupSignatureCell:didChangedValue:)] [self.delegate) {setupSignatureCell:self didChangedValue:textView.text];} / / self.signatureTEXT = textView.text retention};

Of course, in actual development, it is not enough to just implement this proxy method. It should be determined by demand.

Next, the implementation of the two UILabel spaces, the core implementation follows:

/ / set the content - (void) setSignatureTEXT: (NSString * signatureTEXT) {_signatureTEXT = signatureTEXT.copy; self.placeholderLabel.hidden = (signatureTEXT.length > 0); self.countLabel.text [NSString = "%zd/%zd" stringWithFormat:@, self.signatureTEXT.length, self.maxCount]; self.textView.text = self.signatureTEXT;} / / maximum count (void) - setMaxCount: (NSInteger maxCount) {_maxCount = maxCount; self.countLabel.text = [NSString stringWithFormat:@%zd/%zd, self.signatureTEXT.length, self.maxCount];}

Simple function, and here it is over. There’s a better plan for you to share.

Thank you ~!