IOS concise input box implementation

Source: Internet
Author: User

IOS concise input box implementation
The expected function has a placeholder text as a prompt. Click the input text and the text disappears. When the deleted text is empty, it appears again. There is a blue line under the text box to indicate the range of the input box. Similar to the green text box in our text box, the text box needs to move up and down with the keyboard, after entering the text, you can automatically adjust the size and position of the send button on the right. When there is no text, it is dimmed and the prompt cannot be sent. When the input text box is not empty, you can send it, it is represented in blue, and there is a line between the top and the bottom view according to the height of the input box on the left. After the input is complete, click "finish" on the keyboard or click "send" to output text. You can divide the view into three parts. The first part is the split line and the UIView with a width of 1, the second is similar to the placeHolder input box in html, and the last is the button. The input text box is implemented by UILabel + UITextView + UIImageView (Blue Line). The length of UITextView can be determined by listening to UITextViewTextDidChangeNotification. Create a class to inherit UITextView and add placeText and placeColor to the header file because the content of placetext is changed externally. Now add UILable-(void) PlaceTextLabel {if (self. placeText. length> 0) {if (! _ PlaceTextLab) {CGRect frame = CGRectMake (8, 8, self. bounds. size. width-16, 0); _ placeTextLab = [[UILabel alloc] initWithFrame: frame]; _ placeTextLab. font = self. font; _ placeTextLab. backgroundColor = [UIColor clearColor]; _ placeTextLab. textColor = self. placeColor; _ placeTextLab. tag = 999; _ placeTextLab. alpha = 0; _ placeTextLab. lineBreakMode = NSLineBreakByWordWrapping; _ placeTextLab. numberOfLines = 0; [self addSubview: _ PlaceTextLab];} _ placeTextLab. text = self. placeText; [_ placeTextLab sizeToFit]; [_ placeTextLab setFrame: CGRectMake (8, 8, CGRectGetWidth (self. bounds)-16, CGRectGetHeight (_ placeTextLab. frame)];} if (self. text. length = 0 & self. placeText. length> 0) {[[self viewWithTag: 999] setAlpha: 1.0] ;}} use Photoshop to create a line-pixel Blue Line-like image and save it in png format, you must set the Image Display Mode for the UIImageView of the image-(void) addLineView {[_ subLine removeFromSuperview]; _ SubLine = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @ "subline.png"]; [_ subLine setContentMode: offline]; [self addSubline: _ subLine];} UITextView settings are relatively cumbersome. Rewrite the initialization code-(instancetype) initWithFrame :( CGRect) frame PlaceText :( NSString *) placeText PlaceColor :( UIColor *) placeColor {self = [super initWithFrame: frame]; if (self) {self. scrollEnabled = NO; _ placeText = placeText; _ pl AceColor = placeColor; _ Textnil = YES; [[nsicationcenter center defacenter center] addObserver: self selector: @ selector (TextChange :) name: UITextViewTextDidChangeNotification object: nil];} return self ;} as the input box increases with the input text, we need to pass this value to the parent view so that the parent view can also increase based on the content, which can be delegated, however, the delegation is a bit heavyweight. here we can use a simpler block. Here, I first think of monitoring UITextViewTextDidChangeNotification to monitor input in real time. Real-time input changes cause high real-time changes. However, unfortunately, I fell into the trap. I found that when we enter a line to the header and then enter a line break, the height did not change. Enter the second character of this line before the height value changes. I think we should have sent a notification before the height change, so our height change is not real-time. If so, we cannot use this method, fortunately, we can also use a simple function CGSize = [self sizeThatFits: CGSizeMake (self. contentSize. width, 1000.0)]; so the size is the size we want. -(Void) TextChange :( NSNotification *) notification {if (self. text. length = 0) {_ Textnil = YES;} else {_ Textnil = NO;} [self addLineView]; if (self. placeText. length = 0) {return;} [UIView animateWithDuration: 0.5 animations: ^ {if (_ Textnil) {[[self viewWithTag: 999] setAlpha: 1.0];} else {[[self viewWithTag: 999] setAlpha: 0] ;}}] ;}- (void) addSubline :( UIView *) view {CGSize size = [self sizeThatFits: CGSizeMa Ke (self. contentSize. width, 1000.0)]; if (view) {CGRect frame = CGRectMake (2, size. height-3, self. bounds. size. width-4, 3.0); view. frame = frame;} [self addSubview: view]; self. viewSize (size); // you can judge it first ...} Here we use block to pass the size. First we declare a private variable of the block and then write a method to assign values. The implementation of the block is also created by the class that calls the object. Then we can call it like a delegate. In AFNetwork, we can see the block everywhere, instead of the delegate everywhere in ASIhttprequest, his predecessors. After the main input box is assembled, You can assemble it. First, create a new class of UIview. UIView must be consistent with the keyboard height. The notification uikeyboardwillchangeframenoication ication that needs to be monitored for keyboard slide, which changes the height of the input box in real time-(void) keyboardWillShow :( NSNotification *) notification {NSDictionary * userInfo = [notification userInfo]; NSTimeInterval boardAnimationDuration = [[userInfo objectForKey: role] doubleValue]; CGRect frame = [[userInfo objectForKey: role] CGRectValue]; [UIView animateWithDuration: boardAnima TionDuration delay: 0.0f options: UIViewAnimationOptionCurveEaseInOut animations: ^ {CGFloat keyBoardY = frame. origin. y; CGFloat keyBoardHeigh = frame. size. height; maxy = [UIScreen mainScreen]. bounds. size. height-keyBoardHeigh; CGRect frame = self. frame; frame. origin. y = keyBoardY-CGRectGetHeight (self. frame); self. frame = frame;} completion: ^ (BOOL finished) {nil ;}];} maxY refers to the standard position in the y direction of the input box. [UIScreen mainScreen] without a keyboard. bounds. size. height: in the case of a keyboard, It is the y value of the keyboard. When the character in the input box changes, its height is changed in real time, as shown in the figure below-(void) TextViewDidChange :( CGSize) size {self. frame = CGRectMake (0, self. frame. origin. y, self. frame. size. width, size. height + 6*2); self. frame = CGRectMake (0, maxy-self.frame.size.height, self. frame. size. width, self. frame. size. height); _ Inputview. frame = CGRectMake (10, 6, self. frame. size. width-K_right_padding, size. height);} The above is an important issue of highly adaptive changes to the UIView. Here, the call relationship between the button and the external class also uses the block method @ property (nonatomic, copy) void (^ sendText) (NSString *);-(void) sendMessage :( void (^) (NSString * text) inputText {if (inputText) {self. sendText = inputText ;}}- (void) buttonClick {[self sendInputText];}-(void) sendInputText {if (_ Inputview. textnil) {return;} else {if (self. sendText) {self. sendText (_ Inputview. text); _ Inputview. text = @"";}}}

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.