@ -1168,8 +1168,22 @@ class CaptionView: UIView {
private let kMinTextViewHeight : CGFloat = 38
private var textViewHeightConstraint : NSLayoutConstraint !
// T O D O s h o w l e n g t h l i m i t l a b e l
private let lengthLimitLabel : UILabel = UILabel ( )
private lazy var lengthLimitLabel : UILabel = {
let lengthLimitLabel = UILabel ( )
// L e n g t h L i m i t L a b e l s h o w n w h e n t h e u s e r i n p u t s t o o l o n g o f a m e s s a g e
lengthLimitLabel . textColor = . white
lengthLimitLabel . text = NSLocalizedString ( " ATTACHMENT_APPROVAL_CAPTION_LENGTH_LIMIT_REACHED " , comment : " One-line label indicating the user can add no more text to the attachment caption. " )
lengthLimitLabel . textAlignment = . center
// A d d s h a d o w i n c a s e o v e r l a y e d o n w h i t e c o n t e n t
lengthLimitLabel . layer . shadowColor = UIColor . black . cgColor
lengthLimitLabel . layer . shadowOffset = CGSize ( width : 0.0 , height : 0.0 )
lengthLimitLabel . layer . shadowOpacity = 0.8
lengthLimitLabel . isHidden = true
return lengthLimitLabel
} ( )
// MARK: I n i t i a l i z e r s
@ -1196,6 +1210,13 @@ class CaptionView: UIView {
addSubview ( hStack )
hStack . autoPinEdgesToSuperviewMargins ( )
addSubview ( lengthLimitLabel )
lengthLimitLabel . autoPinEdge ( toSuperviewMargin : . left )
lengthLimitLabel . autoPinEdge ( toSuperviewMargin : . right )
lengthLimitLabel . autoPinEdge ( . bottom , to : . top , of : textView , withOffset : - 9 )
lengthLimitLabel . setContentHuggingHigh ( )
lengthLimitLabel . setCompressionResistanceHigh ( )
}
required init ? ( coder aDecoder : NSCoder ) {
@ -1285,6 +1306,7 @@ class CaptionView: UIView {
} ( )
}
let kMaxCaptionCharacterCount = 240
extension CaptionView : UITextViewDelegate {
public func textViewDidBeginEditing ( _ textView : UITextView ) {
updatePlaceholderTextViewVisibility ( )
@ -1306,8 +1328,9 @@ extension CaptionView: UITextViewDelegate {
let existingText : String = textView . text ? ? " "
let proposedText : String = ( existingText as NSString ) . replacingCharacters ( in : range , with : text )
guard proposedText . utf8 . count <= kOversizeTextMessageSizeThreshold else {
Logger . debug ( " long text was truncated " )
let kMaxCaptionByteCount = kOversizeTextMessageSizeThreshold / 4
guard proposedText . utf8 . count <= kMaxCaptionByteCount else {
Logger . debug ( " hit caption byte count limit " )
self . lengthLimitLabel . isHidden = false
// ` r a n g e ` r e p r e s e n t s t h e s e c t i o n o f t h e e x i s t i n g t e x t w e w i l l r e p l a c e . W e c a n r e - u s e t h a t s p a c e .
@ -1324,16 +1347,29 @@ extension CaptionView: UITextViewDelegate {
return false
}
self . lengthLimitLabel . isHidden = true
// T h o u g h w e c a n w r a p t h e t e x t , w e d o n ' t w a n t t o e n c o u r a g e m u l t l i n e c a p t i o n s , p l u s a " d o n e " b u t t o n
// a l l o w s t h e u s e r t o g e t t h e k e y b o a r d o u t o f t h e w a y w h i l e i n t h e a t t a c h m e n t a p p r o v a l v i e w .
if text = = " \n " {
textView . resignFirstResponder ( )
// A f t e r v e r i f y i n g t h e b y t e - l e n g t h i s s u f f i c i e n t l y s m a l l , v e r i f y t h e c h a r a c t e r c o u n t i s w i t h i n b o u n d s .
// N o r m a l l y t h i s c h a r a c t e r c o u n t s h o u l d e n t a i l * m u c h * l e s s b y t e c o u n t .
guard proposedText . count <= kMaxCaptionCharacterCount else {
Logger . debug ( " hit caption character count limit " )
self . lengthLimitLabel . isHidden = false
// ` r a n g e ` r e p r e s e n t s t h e s e c t i o n o f t h e e x i s t i n g t e x t w e w i l l r e p l a c e . W e c a n r e - u s e t h a t s p a c e .
let charsAfterDelete : Int = ( existingText as NSString ) . replacingCharacters ( in : range , with : " " ) . count
// A c c e p t a s m u c h o f t h e i n p u t a s w e c a n
let charBudget : Int = Int ( kMaxCaptionCharacterCount ) - charsAfterDelete
if charBudget >= 0 {
let acceptableNewText = String ( text . prefix ( charBudget ) )
textView . text = ( existingText as NSString ) . replacingCharacters ( in : range , with : acceptableNewText )
}
return false
} else {
return true
}
self . lengthLimitLabel . isHidden = true
return true
}
public func textViewDidChange ( _ textView : UITextView ) {
@ -1360,7 +1396,22 @@ class MediaMessageTextToolbar: UIView, UITextViewDelegate {
set { self . textView . text = newValue }
}
private let lengthLimitLabel : UILabel = UILabel ( )
private lazy var lengthLimitLabel : UILabel = {
let lengthLimitLabel = UILabel ( )
// L e n g t h L i m i t L a b e l s h o w n w h e n t h e u s e r i n p u t s t o o l o n g o f a m e s s a g e
lengthLimitLabel . textColor = . white
lengthLimitLabel . text = NSLocalizedString ( " ATTACHMENT_APPROVAL_MESSAGE_LENGTH_LIMIT_REACHED " , comment : " One-line label indicating the user can add no more text to the media message field. " )
lengthLimitLabel . textAlignment = . center
// A d d s h a d o w i n c a s e o v e r l a y e d o n w h i t e c o n t e n t
lengthLimitLabel . layer . shadowColor = UIColor . black . cgColor
lengthLimitLabel . layer . shadowOffset = CGSize ( width : 0.0 , height : 0.0 )
lengthLimitLabel . layer . shadowOpacity = 0.8
lengthLimitLabel . isHidden = true
return lengthLimitLabel
} ( )
// L a y o u t C o n s t a n t s
@ -1437,17 +1488,6 @@ class MediaMessageTextToolbar: UIView, UITextViewDelegate {
// I n c r e a s e h i t a r e a o f s e n d b u t t o n
sendButton . contentEdgeInsets = UIEdgeInsets ( top : 6 , left : 8 , bottom : 6 , right : 8 )
// L e n g t h L i m i t L a b e l s h o w n w h e n t h e u s e r i n p u t s t o o l o n g o f a m e s s a g e
lengthLimitLabel . textColor = . white
lengthLimitLabel . text = NSLocalizedString ( " ATTACHMENT_APPROVAL_CAPTION_LENGTH_LIMIT_REACHED " , comment : " One-line label indicating the user can add no more text to the attachment caption. " )
lengthLimitLabel . textAlignment = . center
// A d d s h a d o w i n c a s e o v e r l a y e d o n w h i t e c o n t e n t
lengthLimitLabel . layer . shadowColor = UIColor . black . cgColor
lengthLimitLabel . layer . shadowOffset = CGSize ( width : 0.0 , height : 0.0 )
lengthLimitLabel . layer . shadowOpacity = 0.8
self . lengthLimitLabel . isHidden = true
let contentView = UIView ( )
contentView . addSubview ( sendButton )
contentView . addSubview ( textView )