UIView atop the Keyboard similar to iMessage App

前端 未结 5 1164
眼角桃花
眼角桃花 2020-12-04 14:38

currently I\'m attempting to basically implement and exact copy of Apples iMessage App.

That means I need a UITextView that is docked at the bottom of the screen, an

5条回答
  •  失恋的感觉
    2020-12-04 15:29

    Here's a UITextView subclass that is working properly on iOS 9.3.1 and 8.3.1. It takes care of growing and shrinking with limits, while keeping the caret always in the right place and animating smoothly.

    Sticking the view over the keyboard is trivial, with many solutions to be found easily, so it's not covered...

    I could not find any made-solutions that were production ready so I ended up working on this from scratch. I had to work out a lot of little problems along the way.

    Code comments should give you an idea of what's going on.

    I have shared this on my Github, Contributions greatly appreciated.

    Notes

    • Not tested to support landscape
    • Not tested on i6+

    Demo

    (after max height element becomes scrollable. Forgot to drag the demo, but this is working as expected as well... )

    Subclass

    class ruuiDynamicTextView: UITextView {
        var dynamicDelegate: ruuiDynamicTextViewDelegate?
        var minHeight: CGFloat!
        var maxHeight: CGFloat?
        private var contentOffsetCenterY: CGFloat!
    
        init(frame: CGRect, offset: CGFloat = 0.0) {
            super.init(frame: frame, textContainer: nil)
            minHeight = frame.size.height
    
            //center first line
            let size = self.sizeThatFits(CGSizeMake(self.bounds.size.width, CGFloat.max))
            contentOffsetCenterY = (-(frame.size.height - size.height * self.zoomScale) / 2.0) + offset
    
            //listen for text changes
            NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(textChanged), name: UITextViewTextDidChangeNotification, object: nil)
    
            //update offsets
            layoutSubviews()
        }
    
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
        override func layoutSubviews() {
            super.layoutSubviews()
    
            //Use content size if more than min size, compensate for Y offset
            var height = max(self.contentSize.height - (contentOffsetCenterY * 2.0), minHeight)
            var updateContentOffsetY: CGFloat?
            //Max Height
            if maxHeight != nil && height > maxHeight {
                //Cap at maxHeight
                height = maxHeight!
            } else {
                //constrain Y to prevent odd skip and center content to view.
                updateContentOffsetY = contentOffsetCenterY
            }
            //update frame if needed & notify delegate
            if self.frame.size.height != height {
                self.frame.size.height = height
                dynamicDelegate?.dynamicTextViewDidResizeHeight(self, height: height)
            }
            //constrain Y must be done after setting frame
            if updateContentOffsetY != nil {
                self.contentOffset.y = updateContentOffsetY!
            }
        }
    
        func textChanged() {
            let caretRect = self.caretRectForPosition(self.selectedTextRange!.start)
            let overflow = caretRect.size.height + caretRect.origin.y - (self.contentOffset.y + self.bounds.size.height - self.contentInset.bottom - self.contentInset.top)
            if overflow > 0 {
                //Fix wrong offset when cursor jumps to next line un explisitly
                let seekEndY = self.contentSize.height - self.bounds.size.height
                if self.contentOffset.y != seekEndY {
                    self.contentOffset.y = seekEndY
                }
            }
        }
    }
    
    protocol ruuiDynamicTextViewDelegate {
        func dynamicTextViewDidResizeHeight(textview: ruuiDynamicTextView, height: CGFloat)
    }
    

提交回复
热议问题