IOS custom view-SJTextView, iossjtextview

Source: Internet
Author: User

IOS custom view-SJTextView, iossjtextview
### Requirements: 1. textView requires placeholder to prompt input 2. textView limit 3. textView prohibiting emoticon input ### thinking: because the requirements are more common, you want to use custom SJTextView: 1. placeholder adds a transparent label to the textView and hides the label after entering the label. 2. the word limit can be implemented in the proxy method. If the number of words reaches the maximum, no input is allowed. emoticon prohibit input (this is not commonly used, because we do not receive the server, so the input is limited), with the github [SearchEmojiOnString-iOS] (https://github.com/GabrielMassana/SearchEmojiOnString-iOS) you can easily implement NSString + EMOEmoji: if you use the UITextView proxy method when customizing SJTextView, if you use the proxy method when using this custom TextView, the proxy method in SJTextView is overwritten. Solution. ### Implementation: #### add an external attribute '''/** placeholder string */@ property (nonatomic, strong) NSString * placeholder; /** placeholder string color */@ property (nonatomic, strong) UIColor * placeholderColor;/** string length limit */@ property (nonatomic, assign) NSUInteger limitedLength; /** whether to input emoticon */@ property (nonatomic, assign) BOOL emojiDisable; ''' #### add method #### 1. add the placeholder character label '''/** placeholder string @ param placeholder string */-(void) setPlaceholder :( NSString *) pla Ceholder {if (placeholder) {_ placeholder = placeholder; self. placeholderLabel = [[UILabel alloc] initWithFrame: CGRectMake (5, 8, self. frame. size. width-10, 0)]; self. placeholderLabel. numberOfLines = 0; self. placeholderLabel. text = placeholder; self. placeholderLabel. textColor = [UIColor lightGrayColor]; self. placeholderLabel. font = [UIFont systemFontOfSize: 13]; // The default UITextView font size is 13 [self adju StLabelFrameHeight: self. placeholderLabel]; // The height of placeholder to adapt to [self addSubview: self. placeholderLabel] ;}}''' ##### 2. restrict emoticon input '''/** to restrict emoticon input. NSString + EMOEmoji classification is required. You can also copy the Method */-(void) disableEmoji {if ([self. text emo_containsEmoji]) {// The emo_disableEmoji method is to rewrite a method in NSString + emoemoemoji. For details, refer to the Code [self setText: [self. text emo_disableEmoji] ;}}''' ##### 3. add character length limit ''' // Add an attribute record to SJTextView last time displayed on t The internal attribute of the string in extView is the key to completing the limit on the length of the string. // The last time the character string @ property (nonatomic, strong) NSString * lastText is displayed in textView; // during initialization, add the listener [[NSNotificationCenter defacenter center] addObserver: self selector: @ selector (textViewEditChanged) name: UITextViewTextDidChangeNotification object: self]; // listener method implementation-(void) textViewEditChanged {// ----- display and hide of placeholderLabel ----- if (self. text. length = 0) {[self. placeholderLabel se THidden: NO];} else {[self. placeholderLabel setHidden: YES];} // ----- do not enter emojis ----- if (self. emojiDisable) {[self disableEmoji];} // ----- word limit ----- // obtain the highlighted UITextRange * selectedRange = [self markedTextRange]; UITextPosition * pos = [self positionFromPosition: selectedRange. start offset: 0]; // If the input word can change, no restriction is imposed, self. lastText does not record the text in the changing state if (selectedRange & pos) {return;} if (self. text. l Ength> self. lastText. length) {NSString * newInputText = [self. text substringFromIndex: self. lastText. length]; NSUInteger canInputLength = self. limitedLength-self. lastText. length; // If the length exceeds the input length, it is restored to the string entered last time. That is to say, if you are copying a large text segment, the length exceeds the input length, this text won't be entered into TextView (You have to edit it and paste it again, enter a part, or delete it) if (newInputText. length> canInputLength | canInputLength = 0) {[self setText: self. lastText]; // a prompt is provided here. The input exceeds NSLog (@ "% @", [NSString stringWithFormat: @ "cannot exceed % lu. ", (Unsigned long) self. limitedLength]) ;}} self. lastText = self. text ;}'''

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.