From 0526344b0cb2c9b8fa0732d41f10609ed304beab Mon Sep 17 00:00:00 2001 From: nixzhu Date: Thu, 14 Jan 2016 10:41:52 +0800 Subject: [PATCH 1/7] strong guard in yep_mentionWordInIndex --- Yep/Extensions/String+Yep.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Yep/Extensions/String+Yep.swift b/Yep/Extensions/String+Yep.swift index 6d7d1ec5..03c3390f 100644 --- a/Yep/Extensions/String+Yep.swift +++ b/Yep/Extensions/String+Yep.swift @@ -47,6 +47,10 @@ extension String { return nil } + guard (self as NSString).length > index else { + return nil + } + let index = startIndex.advancedBy(index) var wordString: String? From b870a5c54656669b69cfdf09830ac020dcaa56d9 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Thu, 14 Jan 2016 13:01:58 +0800 Subject: [PATCH 2/7] limit top of messageToolbar --- .../Conversation/Conversation.storyboard | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/Yep/ViewControllers/Conversation/Conversation.storyboard b/Yep/ViewControllers/Conversation/Conversation.storyboard index dea5a184..4e595b81 100644 --- a/Yep/ViewControllers/Conversation/Conversation.storyboard +++ b/Yep/ViewControllers/Conversation/Conversation.storyboard @@ -14,11 +14,11 @@ - + - + @@ -28,16 +28,16 @@ - + - + @@ -54,11 +54,11 @@ - + @@ -73,10 +73,12 @@ + + @@ -115,7 +117,7 @@ - + @@ -131,7 +133,7 @@ - + From 99eeea54f3d8bc425db02c0c6dfc38ca381134c7 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Thu, 14 Jan 2016 13:03:48 +0800 Subject: [PATCH 3/7] adjust messageTextViewHeightConstraint.priority ... --- Yep/Views/MessageToolbar/MessageToolbar.swift | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Yep/Views/MessageToolbar/MessageToolbar.swift b/Yep/Views/MessageToolbar/MessageToolbar.swift index 314af2e5..fa387696 100644 --- a/Yep/Views/MessageToolbar/MessageToolbar.swift +++ b/Yep/Views/MessageToolbar/MessageToolbar.swift @@ -173,12 +173,16 @@ class MessageToolbar: UIToolbar { lazy var messageTextView: UITextView = { let textView = UITextView() textView.textContainerInset = UIEdgeInsets(top: 8, left: 4, bottom: 8, right: 4) + //textView.textContainerInset = UIEdgeInsetsZero textView.font = UIFont.systemFontOfSize(15) textView.layer.borderWidth = 1 textView.layer.borderColor = UIColor.yepMessageToolbarSubviewBorderColor().CGColor textView.layer.cornerRadius = self.normalCornerRadius textView.delegate = self - textView.scrollEnabled = false // 重要:若没有它,换行时可能有 top inset 不正确 + //textView.scrollEnabled = false // 重要:若没有它,换行时可能有 top inset 不正确 + //textView.layoutManager.allowsNonContiguousLayout = false + //textView.textContainer.widthTracksTextView = true + //textView.textContainer.heightTracksTextView = true return textView }() @@ -281,6 +285,7 @@ class MessageToolbar: UIToolbar { let textContainerInset = messageTextView.textContainerInset let constant = ceil(messageTextView.font!.lineHeight + textContainerInset.top + textContainerInset.bottom) messageTextViewHeightConstraint = NSLayoutConstraint(item: messageTextView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: constant) + messageTextViewHeightConstraint.priority = UILayoutPriorityDefaultHigh let constraintsH = NSLayoutConstraint.constraintsWithVisualFormat("H:|[micButton(48)][messageTextView][moreButton(==micButton)]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary) @@ -369,6 +374,16 @@ class MessageToolbar: UIToolbar { }, completion: { (finished) -> Void in }) } + + //messageTextView.scrollEnabled = false + //messageTextView.scrollRangeToVisible(messageTextView.selectedRange) + //messageTextView.scrollEnabled = true + /* + var range = messageTextView.selectedRange + range.location -= 1 + range.length = 1 + messageTextView.scrollRangeToVisible(range) + */ } // MARK: Actions From d87980023bebf894d27f600df242f351b1b521f5 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Thu, 14 Jan 2016 13:49:28 +0800 Subject: [PATCH 4/7] hack for scrollEnabled when input lots of text --- Yep/Views/MessageToolbar/MessageToolbar.swift | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/Yep/Views/MessageToolbar/MessageToolbar.swift b/Yep/Views/MessageToolbar/MessageToolbar.swift index fa387696..efd0b8fb 100644 --- a/Yep/Views/MessageToolbar/MessageToolbar.swift +++ b/Yep/Views/MessageToolbar/MessageToolbar.swift @@ -179,10 +179,7 @@ class MessageToolbar: UIToolbar { textView.layer.borderColor = UIColor.yepMessageToolbarSubviewBorderColor().CGColor textView.layer.cornerRadius = self.normalCornerRadius textView.delegate = self - //textView.scrollEnabled = false // 重要:若没有它,换行时可能有 top inset 不正确 - //textView.layoutManager.allowsNonContiguousLayout = false - //textView.textContainer.widthTracksTextView = true - //textView.textContainer.heightTracksTextView = true + textView.scrollEnabled = false // 重要:若没有它,换行时可能有 top inset 不正确 return textView }() @@ -371,19 +368,30 @@ class MessageToolbar: UIToolbar { UIView.animateWithDuration(0.1, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in self.messageTextViewHeightConstraint.constant = newHeight self.layoutIfNeeded() - }, completion: { (finished) -> Void in + + }, completion: { [weak self] finished in + + // hack for scrollEnabled when input lots of text + + if finished, let strongSelf = self { + println("messageToolbar.frame: \(strongSelf.frame)") + + if strongSelf.frame.origin.y < 100 { + strongSelf.messageTextView.scrollEnabled = true + } else { + strongSelf.messageTextView.scrollEnabled = false + } + } }) + + } else { } + //messageTextView.textContainerInset = UIEdgeInsets(top: 8, left: 4, bottom: 8, right: 4) //messageTextView.scrollEnabled = false - //messageTextView.scrollRangeToVisible(messageTextView.selectedRange) //messageTextView.scrollEnabled = true - /* - var range = messageTextView.selectedRange - range.location -= 1 - range.length = 1 - messageTextView.scrollRangeToVisible(range) - */ + + //messageTextView.textContainer.size = messageTextView.contentSize } // MARK: Actions From 1a7db89b5c8177e65d7295fb401af6838155566a Mon Sep 17 00:00:00 2001 From: nixzhu Date: Thu, 14 Jan 2016 13:51:27 +0800 Subject: [PATCH 5/7] clean code --- Yep/Views/MessageToolbar/MessageToolbar.swift | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/Yep/Views/MessageToolbar/MessageToolbar.swift b/Yep/Views/MessageToolbar/MessageToolbar.swift index efd0b8fb..604a1bb9 100644 --- a/Yep/Views/MessageToolbar/MessageToolbar.swift +++ b/Yep/Views/MessageToolbar/MessageToolbar.swift @@ -166,7 +166,7 @@ class MessageToolbar: UIToolbar { button.tintAdjustmentMode = .Normal button.addTarget(self, action: "toggleRecordVoice", forControlEvents: UIControlEvents.TouchUpInside) return button - }() + }() let normalCornerRadius: CGFloat = 6 @@ -181,7 +181,7 @@ class MessageToolbar: UIToolbar { textView.delegate = self textView.scrollEnabled = false // 重要:若没有它,换行时可能有 top inset 不正确 return textView - }() + }() lazy var voiceRecordButton: VoiceRecordButton = { let button = VoiceRecordButton() @@ -215,7 +215,7 @@ class MessageToolbar: UIToolbar { } return button - }() + }() lazy var moreButton: UIButton = { let button = UIButton() @@ -224,7 +224,7 @@ class MessageToolbar: UIToolbar { button.tintAdjustmentMode = .Normal button.addTarget(self, action: "moreMessageTypes", forControlEvents: UIControlEvents.TouchUpInside) return button - }() + }() lazy var sendButton: UIButton = { let button = UIButton() @@ -234,7 +234,7 @@ class MessageToolbar: UIToolbar { button.setTitleColor(UIColor.messageToolBarHighlightColor(), forState: .Normal) button.addTarget(self, action: "trySendTextMessage", forControlEvents: UIControlEvents.TouchUpInside) return button - }() + }() // MARK: UI @@ -383,15 +383,7 @@ class MessageToolbar: UIToolbar { } } }) - - } else { } - //messageTextView.textContainerInset = UIEdgeInsets(top: 8, left: 4, bottom: 8, right: 4) - - //messageTextView.scrollEnabled = false - //messageTextView.scrollEnabled = true - - //messageTextView.textContainer.size = messageTextView.contentSize } // MARK: Actions From 151265a198de67a44e87883ef081708ac882c472 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Thu, 14 Jan 2016 13:53:29 +0800 Subject: [PATCH 6/7] code style --- Yep/Views/MessageToolbar/MessageToolbar.swift | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/Yep/Views/MessageToolbar/MessageToolbar.swift b/Yep/Views/MessageToolbar/MessageToolbar.swift index 604a1bb9..57f4ce95 100644 --- a/Yep/Views/MessageToolbar/MessageToolbar.swift +++ b/Yep/Views/MessageToolbar/MessageToolbar.swift @@ -374,13 +374,9 @@ class MessageToolbar: UIToolbar { // hack for scrollEnabled when input lots of text if finished, let strongSelf = self { - println("messageToolbar.frame: \(strongSelf.frame)") - - if strongSelf.frame.origin.y < 100 { - strongSelf.messageTextView.scrollEnabled = true - } else { - strongSelf.messageTextView.scrollEnabled = false - } + //println("messageToolbar.frame: \(strongSelf.frame)") + let scrollEnabled = strongSelf.frame.origin.y < 100 + strongSelf.messageTextView.scrollEnabled = scrollEnabled } }) } From 25fde134fcb596d88e1dac97cafc7a04ab72e6d8 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Thu, 14 Jan 2016 13:56:42 +0800 Subject: [PATCH 7/7] detail --- Yep/Views/MessageToolbar/MessageToolbar.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Yep/Views/MessageToolbar/MessageToolbar.swift b/Yep/Views/MessageToolbar/MessageToolbar.swift index 57f4ce95..b9a056bb 100644 --- a/Yep/Views/MessageToolbar/MessageToolbar.swift +++ b/Yep/Views/MessageToolbar/MessageToolbar.swift @@ -365,7 +365,7 @@ class MessageToolbar: UIToolbar { //println("oldHeight: \(messageTextViewHeightConstraint.constant), newHeight: \(newHeight)") if newHeight != messageTextViewHeightConstraint.constant { - UIView.animateWithDuration(0.1, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in + UIView.animateWithDuration(0.1, delay: 0.0, options: .CurveEaseInOut, animations: { self.messageTextViewHeightConstraint.constant = newHeight self.layoutIfNeeded() @@ -375,8 +375,8 @@ class MessageToolbar: UIToolbar { if finished, let strongSelf = self { //println("messageToolbar.frame: \(strongSelf.frame)") - let scrollEnabled = strongSelf.frame.origin.y < 100 - strongSelf.messageTextView.scrollEnabled = scrollEnabled + let enabled = strongSelf.frame.origin.y < 100 + strongSelf.messageTextView.scrollEnabled = enabled } }) }