@@ -148,7 +148,8 @@ class CommentDepthCell: MarginedTableViewCell, UIViewControllerPreviewingDelegat
148148 $0. isUserInteractionEnabled = true
149149 $0. accessibilityIdentifier = " Comment body "
150150 $0. ignoreHeight = true
151- $0. firstTextView. textContainerInset = UIEdgeInsets ( top: 3 , left: 0 , bottom: 0 , right: 0 )
151+ // TODOjon:
152+ // $0.firstTextView.textContainerInset = UIEdgeInsets(top: 3, left: 0, bottom: 0, right: 0)
152153 } )
153154
154155 self . title = YYLabel ( ) . then ( {
@@ -2342,8 +2343,8 @@ extension CommentDepthCell: UIContextMenuInteractionDelegate {
23422343 } else if self . commentBody. overflow. frame. contains ( location) {
23432344 let innerLocation = self . commentBody. convert ( location, to: self . commentBody. overflow)
23442345 for view in self . commentBody. overflow. subviews {
2345- if view. frame. contains ( innerLocation) && view is YYLabel {
2346- return UITargetedPreview ( view: self . commentBody, parameters: self . getLocationForPreviewedText ( view as! YYLabel , innerLocation, self . previewedURL? . absoluteString, self . commentBody) ?? parameters)
2346+ if let view = view as? UILabel , view . frame. contains ( innerLocation) {
2347+ return UITargetedPreview ( view: self . commentBody, parameters: self . getLocationForPreviewedText ( view, innerLocation, self . previewedURL? . absoluteString, self . commentBody) ?? parameters)
23472348 }
23482349 }
23492350 }
@@ -2362,8 +2363,8 @@ extension CommentDepthCell: UIContextMenuInteractionDelegate {
23622363 } else if self . commentBody. overflow. frame. contains ( location) {
23632364 let innerLocation = self . commentBody. convert ( location, to: self . commentBody. overflow)
23642365 for view in self . commentBody. overflow. subviews {
2365- if view. frame. contains ( innerLocation) && view is YYLabel {
2366- return getConfigurationForTextView ( view as! YYLabel , innerLocation)
2366+ if let view = view as? UILabel , view . frame. contains ( innerLocation) {
2367+ return getConfigurationForTextView ( view, innerLocation)
23672368 }
23682369 }
23692370 } else if self . commentBody. links. frame. contains ( location) {
@@ -2380,44 +2381,34 @@ extension CommentDepthCell: UIContextMenuInteractionDelegate {
23802381 self . previewedVC = nil
23812382 }
23822383
2383- func getLocationForPreviewedText( _ label : YYLabel , _ location: CGPoint , _ inputURL: String ? , _ changeRectTo: UIView ? = nil ) -> UIPreviewParameters ? {
2384+ func getLocationForPreviewedText( _ textView : CoolTextView , _ location: CGPoint , _ inputURL: String ? , _ changeRectTo: UIView ? = nil ) -> UIPreviewParameters ? {
23842385 if inputURL == nil {
23852386 return nil
23862387 }
2387- let point = label. superview? . convert ( location, to: label) ?? location
2388+ // TODOjon:
2389+ let point = textView. superview? . convert ( location, to: textView) ?? location // Convert touch point from frame space to label space
23882390 var params : UIPreviewParameters ?
2389- if let attributedText = label. attributedText, let layoutManager = YYTextLayout ( containerSize: label. frame. size, text: attributedText) {
2390- let locationFinal = layoutManager. textPosition ( for: point, lineIndex: layoutManager. lineIndex ( for: point) )
2391- if locationFinal < 1000000 {
2392- attributedText. enumerateAttribute (
2393- . link,
2394- in: NSRange ( location: 0 , length: attributedText. length)
2395- ) { ( value, range, _) in
2396- if let url = value as? NSURL {
2397- if url. absoluteString == inputURL! {
2398- let baseRects = layoutManager. selectionRects ( for: YYTextRange ( range: range) )
2399- var cgs = [ NSValue] ( )
2400- for rect in baseRects {
2401- if changeRectTo != nil {
2402- cgs. append ( NSValue ( cgRect: changeRectTo!. convert ( rect. rect, from: label) ) )
2403- } else {
2404- cgs. append ( NSValue ( cgRect: rect. rect) )
2405- }
2406- }
2407- params = UIPreviewParameters ( textLineRects: cgs)
2408- params? . backgroundColor = . clear
2409- }
2391+ if let attributedText = textView. attributedText {
2392+ attributedText. enumerateAttribute (
2393+ . link,
2394+ in: NSRange ( location: 0 , length: attributedText. length)
2395+ ) { ( value, range, _) in
2396+ if let url = value as? NSURL {
2397+ if url. absoluteString == inputURL! {
2398+ let rects = textView. selectionRects ( for: range. toTextRange ( textInput: textView) !) . map { NSValue ( cgRect: $0. rect) }
2399+ params = UIPreviewParameters ( textLineRects: rects)
2400+ params? . backgroundColor = . clear
24102401 }
24112402 }
24122403 }
24132404 }
24142405 return params
24152406 }
24162407
2417- func getConfigurationForTextView( _ label : YYLabel , _ location: CGPoint ) -> UIContextMenuConfiguration ? {
2418- let point = label . superview? . convert ( location, to: label) ?? location
2408+ func getConfigurationForTextView( _ textView : CoolTextView , _ location: CGPoint ) -> UIContextMenuConfiguration ? {
2409+ // let point = textView .superview?.convert(location, to: label) ?? location
24192410
2420- if let attributedText = label . attributedText, let layoutManager = YYTextLayout ( containerSize: label . frame. size, text: attributedText) {
2411+ if let attributedText = textView . attributedText, let layoutManager = YYTextLayout ( containerSize: textView . frame. size, text: attributedText) {
24212412 let locationFinal = layoutManager. textPosition ( for: point, lineIndex: layoutManager. lineIndex ( for: point) )
24222413 if locationFinal < 1000000 {
24232414 let attributes = attributedText. attributes ( at: Int ( locationFinal) , effectiveRange: nil )
@@ -2547,3 +2538,37 @@ extension CommentDepthCell: UIContextMenuInteractionDelegate {
25472538 return UIContextMenuConfiguration ( identifier: nil , previewProvider: nil , actionProvider: nil )
25482539 }
25492540}
2541+
2542+ extension UILabel {
2543+ func boundingRect( forCharacterRange range: NSRange ) -> CGRect ? {
2544+
2545+ guard let attributedText = attributedText else { return nil }
2546+
2547+ let textStorage = NSTextStorage ( attributedString: attributedText)
2548+ let layoutManager = NSLayoutManager ( )
2549+
2550+ textStorage. addLayoutManager ( layoutManager)
2551+
2552+ let textContainer = NSTextContainer ( size: bounds. size)
2553+ textContainer. lineFragmentPadding = 0.0
2554+
2555+ layoutManager. addTextContainer ( textContainer)
2556+
2557+ var glyphRange = NSRange ( )
2558+
2559+ // Convert the range for glyphs.
2560+ layoutManager. characterRange ( forGlyphRange: range, actualGlyphRange: & glyphRange)
2561+
2562+ return layoutManager. boundingRect ( forGlyphRange: glyphRange, in: textContainer)
2563+ }
2564+ }
2565+
2566+ extension NSRange {
2567+ func toTextRange( textInput: UITextInput ) -> UITextRange ? {
2568+ if let rangeStart = textInput. position ( from: textInput. beginningOfDocument, offset: location) ,
2569+ let rangeEnd = textInput. position ( from: rangeStart, offset: length) {
2570+ return textInput. textRange ( from: rangeStart, to: rangeEnd)
2571+ }
2572+ return nil
2573+ }
2574+ }
0 commit comments