[Basic iOS control and basic ios Control
A. make a chat interface similar to QQ. each cell contains the sending time, sender (Avatar), and sending information. place the other party's profile picture on the left and our profile picture on the right. the Recipient Information uses the white background dialog box, and our information uses the blue background Dialog Box 4. hide the same sending time. button at the bottom: voice button, message input box, emoticon button, and additional button 6. in response to Keyboard Events, the above view is moved up when the keyboard is called out or hidden. 7. handle keyboard sending events 1 @ implementation NSString (Extension) 2 3/** measure text size */4-(CGSize) sizeWithFont :( UIFont *) font maxSize :( CGSize) maxSize {5 NSDictionary * attrs =@{ NSFontAttributeName: font}; 6 CGSize size = [self boundingRectWithSize: maxSize options: NSStringDrawingUsesLineFragmentOrigin attributes: attrs context: nil]. size; 7 8 return size; 9} 10 11 @ endCalculate the position and size of the Information text box
1 // 3. information, variable size 2 CGFloat screenWidth = [UIScreen mainScreen]. bounds. size. width; 3 // 3.1 set the maximum size 4 CGSize textMaxSize = CGSizeMake (screenWidth-iconWidth-padding * 5, MAXFLOAT); 5 // 3.2 calculate the actual size 6 CGSize textRealSize = [message. text sizeWithFont: MESSAGE_TEXT_FONT maxSize: textMaxSize]; 7 8 // 3.3 adjust the information location 9 CGFloat textX; 10 if (MessageTypeMe = message. type) {11 // we place it on the right side of 12 textX = CGRectGetMinX (_ iconFrame)-textRealSize. width-padding; 13} else {14 // peer, placed on the left side of 15 textX = CGRectGetMaxX (_ iconFrame) + padding; 16} 17 18 CGFloat textY = iconY; 19 _ textFrame = CGRectMake (textX, textY, textRealSize. width, textRealSize. height );
4. Use the information type in the data to determine whether the information is sent by us or sent by the other party, and calculate the position of the profile picture and information.
1 // 2. profile picture 2 CGFloat iconWidth = 40; 3 CGFloat iconHeight = 40; 4 5 // 2.1 adjust the profile position 6 CGFloat iconX Based on the message sender; 7 if (MessageTypeMe = message. type) {8 // we place it on the right side of 9 iconX = [UIScreen mainScreen]. bounds. size. width-padding-iconWidth; 10} else {11 // peer, which is placed on the left side of 12 iconX = padding; 13} 14 15 CGFloat iconY = CGRectGetMaxY (_ timeFrame) + padding; 16 _ iconFrame = CGRectMake (iconX, iconY, iconWidth, iconHeight );
1 @ implementation UIImage (Extension) 2 3 + (UIImage *) resizableImage :( NSString *) imageName {4 UIImage * image = [UIImage imageNamed: imageName]; 5 // take 1x1 in the middle of the image and stretch 6 UIEdgeInsets insets = UIEdgeInsetsMake (image. size. height/2, image. size. width/2, image. size. height/2 + 1, image. size. width/2 + 1); 7 return [image resizableImageWithCapInsets: insets]; 8} 9 10 @ endSet image:
1 // 3.1 set the chat box 2 NSString * chatImageNormalName; 3 NSString * chatImageHighlightedName; 4 if (MessageTypeMe = messageFrame. message. type) {5 chatImageNormalName = @ "chat_send_nor"; 6 response = @ "chat_send_press_pic"; 7} else {8 chatImageNormalName = @ "chat_receive_nor"; 9 response = @ "response "; 10} 11 12 UIImage * chatImageNormal = [UIImage resizableImage: chatImageNormalName]; 13 UIImage * chatImageHighlighted = [UIImage resizableImage: chatImageHighlightedName]; 14 [self. textView setBackgroundImage: chatImageNormal forState: UIControlStateNormal]; 15 [self. textView setBackgroundImage: chatImageHighlighted forState: UIControlStateHighlighted];
1 // 3.2 adjust the text padding 2 textView. contentEdgeInsets = UIEdgeInsetsMake (TEXT_INSET, TEXT_INSET );The margin of the background image is blank. After the text padding is changed, the height increases. You need to change the frame size of the view with the loading information:
1 // 3. information, variable size 2 CGFloat screenWidth = [UIScreen mainScreen]. bounds. size. width; 3 // 3.1 set the maximum text size 4 CGSize textMaxSize = CGSizeMake (screenWidth-iconWidth-padding * 10, MAXFLOAT ); 5 // 3.2 calculate the actual size of the text. 6 CGSize textRealSize = [message. text sizeWithFont: MESSAGE_TEXT_FONT maxSize: textMaxSize]; 7 8 // 3.3 button size 9 CGSize btnSize = CGSizeMake (textRealSize. width + TEXT_INSET * 2, textRealSize. height + TEXT_INSET * 2); 10 11 // 3.4 adjust the information location 12 CGFloat textX; 13 if (MessageTypeMe = message. type) {14 // we place it on the right side of 15 textX = CGRectGetMinX (_ iconFrame)-btnSize. width-padding; 16} else {17 // peer, placed on the left 18 textX = CGRectGetMaxX (_ iconFrame) + padding; 19} 20 21 CGFloat textY = iconY; 22 _ textFrame = CGRectMake (textX, textY, btnSize. width, btnSize. height );
1/** whether to hide the sending time */2 @ property (nonatomic, assign) BOOL hideTime;(2) initialize this flag when the Controller loads information from the plist File
1 // determine whether the sending time is the same as the sending time of the previous message. if yes, 2 MessageFrame * lastMessageFrame = [mdictArray lastObject] is not displayed; 3 if (lastMessageFrame & [message. time isEqualToString: lastMessageFrame. message. time]) {4 message. hideTime = YES; 5}
(3) only hideTime = NO. During frame calculation, the frame with the sending time needs to be calculated for a long time.
1 // 1. sending time 2 if (NO = message. hideTime) {3 CGFloat timeWidth = [UIScreen mainScreen]. bounds. size. width; 4 CGFloat timeHeight = 40; 5 CGFloat timeX = 0; 6 CGFloat timeY = 0; 7 _ timeFrame = CGRectMake (timeX, timeY, timeWidth, timeHeight); 8}
1 [[nsicationcenter center defacenter center] addObserver: self selector: @ selector (keyboardWillChangeFrame :) name: UIKeyboardWillChangeFrameNotification object: nil];(2) Compile the listening method
1/** click to drag the preview area and scale back the keyboard */2-(void) scrollViewWillBeginDragging :( UIScrollView *) scrollView {3 // 1. shrink the keyboard 4 [self. view endEditing: YES]; 5} 6 7 8 # pragma mark-listening event 9-(void) keyboardWillChangeFrame :( NSNotification *) note {10 // 1. obtain the pop-up keyboard frame11 CGRect keyboardFrame = [note. userInfo [UIKeyboardFrameEndUserInfoKey] CGRectValue]; 12 13 // 2. the time elapsed for keyboard pop-up 14 CGFloat duration = [note. userInfo [UIKeyboardAnimationDurationUserInfoKey] floatValue]; 15 16 // 3. the shift of the view when the keyboard changes, including the move up/move down 17 CGFloat transformY = keyboardFrame. origin. y-self. view. frame. size. height; 18 19 [UIView animateWithDuration: duration animations: ^ {20 self. view. transform = CGAffineTransformMakeTranslation (0, transformY); 21}]; 22 23 24}
1 // set proxy 2 self. inputView. delegate = self in the information input box;(2) response to the carriage return event
1 # pragma mark-TextField proxy method 2/** press enter response event */3-(BOOL) textFieldShouldReturn :( UITextField *) textField {4 // we send message 5 [self sendMessageWithContent: textField. text andType: MessageTypeMe]; 6 7 // automatically replies 8 [self sendMessageWithContent: [NSString stringWithFormat: @ "% @ \ n % @", textField. text, @ "your sister !!! "] AndType: MessageTypeOhter]; 9 10 // eliminate the message box content 11 self. inputView. text = nil; 12 13 [self. tableView reloadData]; 14 15 // scroll to the latest message 16 NSIndexPath * lastIndexPath = [NSIndexPath indexPathForRow: self. messages. count-1 inSection: 0]; 17 [self. tableView scrollToRowAtIndexPath: lastIndexPath atScrollPosition: Unknown animated: YES]; 18 19 return YES; // The returned value is unknown 20} 21 22 // send the message 23-(void) sendMessageWithContent :( NSString *) text andType :( MessageType) type {24 // get the current time 25 NSDate * date = [NSDate date]; 26 NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; 27 formatter. dateFormat = @ "yyyy-MMM-dd hh: mm: ss"; 28 NSString * dateStr = [formatter stringFromDate: date]; 29 30 // we sent the message 31 NSDictionary * dict =@{ @ "text": text, 32 @ "time": dateStr, 33 @ "type": [NSString stringWithFormat: @ "% d", type]}; 34 35 Message * message = [[Message alloc] init]; 36 [message setValuesForKeysWithDictionary: dict]; 37 MessageFrame * messageFrame = [[MessageFrame alloc] init]; 38 messageFrame. message = message; 39 40 [self. messages addObject: messageFrame]; 41} 42
(3) automatically scroll to the bottom
1 // scroll to the latest message 2 NSIndexPath * lastIndexPath = [NSIndexPath indexPathForRow: self. messages. count-1 inSection: 0]; 3 [self. tableView scrollToRowAtIndexPath: lastIndexPath atScrollPosition: UITableViewScrollPositionBottom animated: YES];