CR: more margin, match button size to default text field, fix layout

when rotated.

There was an issue with captions changing line-count when rotated.

// FREEBIE
pull/1/head
sdkjfhsdkjhfsdlkjhfsdf 7 years ago
parent 8141843f27
commit 42ea1dfbbe

@ -369,6 +369,7 @@ class CaptioningToolbar: UIView, UITextViewDelegate {
weak var captioningToolbarDelegate: CaptioningToolbarDelegate? weak var captioningToolbarDelegate: CaptioningToolbarDelegate?
private let sendButton: UIButton private let sendButton: UIButton
private let textView: UITextView private let textView: UITextView
private let bottomGradient: GradientView
// Layout Constants // Layout Constants
var maxTextViewHeight: CGFloat { var maxTextViewHeight: CGFloat {
@ -400,8 +401,9 @@ class CaptioningToolbar: UIView, UITextViewDelegate {
let kSendButtonShadowOffset: CGFloat = 1 let kSendButtonShadowOffset: CGFloat = 1
init() { init() {
self.textView = MessageTextView()
self.sendButton = UIButton(type: .system) self.sendButton = UIButton(type: .system)
self.bottomGradient = GradientView(from: UIColor.clear, to: UIColor.black)
self.textView = MessageTextView()
self.textViewHeight = kMinTextViewHeight self.textViewHeight = kMinTextViewHeight
super.init(frame: CGRect.zero) super.init(frame: CGRect.zero)
@ -435,12 +437,7 @@ class CaptioningToolbar: UIView, UITextViewDelegate {
// Increase hit area of send button // Increase hit area of send button
sendButton.contentEdgeInsets = UIEdgeInsets(top: 6, left: 8, bottom: 6, right: 8) sendButton.contentEdgeInsets = UIEdgeInsets(top: 6, left: 8, bottom: 6, right: 8)
let bottomGradient = GradientView(from: UIColor.clear, to: UIColor.black) addSubview(bottomGradient)
self.addSubview(bottomGradient)
bottomGradient.autoPinWidthToSuperview()
bottomGradient.autoPinEdge(toSuperviewEdge: .bottom)
bottomGradient.autoSetDimension(.height, toSize: ScaleFromIPhone5(100))
addSubview(sendButton) addSubview(sendButton)
addSubview(textView) addSubview(textView)
@ -457,44 +454,46 @@ class CaptioningToolbar: UIView, UITextViewDelegate {
// not seem to work with inputAccessory views, even when forcing a layout. // not seem to work with inputAccessory views, even when forcing a layout.
override func layoutSubviews() { override func layoutSubviews() {
super.layoutSubviews() super.layoutSubviews()
Logger.debug("\(self.logTag) in \(#function)")
Logger.debug("Before layout >>> self: \(self.frame) textView: \(self.textView.frame), sendButton:\(sendButton.frame)")
let kToolbarMargin: CGFloat = 4 let kToolbarHMargin: CGFloat = 8
let kToolbarVMargin: CGFloat = 8
let sendButtonWidth = sendButton.frame.size.width let sendButtonWidth = sendButton.frame.size.width
let kOriginalToolbarHeight = kMinTextViewHeight + 2 * kToolbarMargin let kOriginalToolbarHeight = kMinTextViewHeight + 2 * kToolbarVMargin
// Assume send button has proper size. // Assume send button has proper size.
let textViewWidth = frame.size.width - 3 * kToolbarMargin - sendButtonWidth let textViewWidth = frame.size.width - 3 * kToolbarHMargin - sendButtonWidth
// determine height given a fixed width // determine height given a fixed width
let textViewHeight = clampedTextViewHeight(fixedWidth: textViewWidth) let textViewHeight = clampedTextViewHeight(fixedWidth: textViewWidth)
textView.frame = CGRect(x: kToolbarMargin, y: kToolbarMargin, width: textViewWidth, height: textViewHeight) let newToolbarHeight = textViewHeight + 2 * kToolbarVMargin
assert(self.textViewHeight == textViewHeight, "textView.height inconsistent with what was computed in textViewDidChange") self.frame.size.height = newToolbarHeight
let toolbarHeightOffset = newToolbarHeight - kOriginalToolbarHeight
let newToolbarHeight = textViewHeight + 2 * kToolbarMargin
let textViewY = kToolbarVMargin - toolbarHeightOffset
// frame origin is with respect to the initial height of the toolbar, so we must offset the toolbar frame textView.frame = CGRect(x: kToolbarHMargin, y: textViewY, width: textViewWidth, height: textViewHeight)
// by the difference, else the toolbar will extend into and behind the keyboard. if (self.textViewHeight != textViewHeight) {
let toolbarHeightOffset = kOriginalToolbarHeight - newToolbarHeight // textViewHeight changed without textView's content changing, this can happen
self.frame = CGRect(x: 0, y: toolbarHeightOffset, width: frame.size.width, height: newToolbarHeight) // when the user flips their device orientation after writing a caption.
self.textViewHeight = textViewHeight
}
// Send Button // Send Button
// position in bottom right corner // position in bottom right corner
let sendButtonX = frame.size.width - kToolbarMargin - sendButton.frame.size.width let sendButtonX = frame.size.width - kToolbarHMargin - sendButton.frame.size.width
let sendButtonY = frame.size.height - kToolbarMargin - sendButton.frame.size.height - kSendButtonShadowOffset let sendButtonY = kOriginalToolbarHeight - kToolbarVMargin - sendButton.frame.size.height - kSendButtonShadowOffset
sendButton.frame = CGRect(origin: CGPoint(x: sendButtonX, y: sendButtonY), size: sendButton.frame.size) sendButton.frame = CGRect(origin: CGPoint(x: sendButtonX, y: sendButtonY), size: sendButton.frame.size)
sendButton.frame.size.height = kMinTextViewHeight - kSendButtonShadowOffset - textView.layer.borderWidth
Logger.debug("After layout >>> self: \(self.frame) textView: \(self.textView.frame), sendButton:\(sendButton.frame)") let bottomGradientHeight = ScaleFromIPhone5(100)
let bottomGradientY = kOriginalToolbarHeight - bottomGradientHeight
bottomGradient.frame = CGRect(x: 0, y: bottomGradientY, width: frame.size.width, height: bottomGradientHeight)
} }
// MARK: - UITextViewDelegate // MARK: - UITextViewDelegate
public func textViewDidChange(_ textView: UITextView) { public func textViewDidChange(_ textView: UITextView) {
Logger.debug("\(self.logTag) in \(#function)")
// compute new height assuming width is unchanged // compute new height assuming width is unchanged
let currentSize = textView.frame.size let currentSize = textView.frame.size
let newHeight = clampedTextViewHeight(fixedWidth: currentSize.width) let newHeight = clampedTextViewHeight(fixedWidth: currentSize.width)

Loading…
Cancel
Save