From adfcf05fe498b5ab6554e9b3fd399d7dd3ade79b Mon Sep 17 00:00:00 2001 From: Hailey Date: Thu, 18 Jan 2024 21:50:21 -0800 Subject: [PATCH] onTextLayout event --- .../ios/RNUITextView.swift | 24 ++++++++++++++++--- .../ios/RNUITextViewShadow.swift | 18 -------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/modules/react-native-ui-text-view/ios/RNUITextView.swift b/modules/react-native-ui-text-view/ios/RNUITextView.swift index ba3ab8257a..453838a3eb 100644 --- a/modules/react-native-ui-text-view/ios/RNUITextView.swift +++ b/modules/react-native-ui-text-view/ios/RNUITextView.swift @@ -3,10 +3,15 @@ class RNUITextView: UIView { @objc var numberOfLines: Int = 0 { didSet { - print(numberOfLines) textView.textContainer.maximumNumberOfLines = numberOfLines } } + @objc var selectable: Bool = true { + didSet { + textView.isSelectable = selectable + } + } + @objc var onTextLayout: RCTDirectEventBlock? override init(frame: CGRect) { if #available(iOS 16.0, *) { @@ -52,6 +57,21 @@ class RNUITextView: UIView { self.textView.frame.size = size self.textView.textContainer.maximumNumberOfLines = numberOfLines self.textView.attributedText = string + + if let onTextLayout = self.onTextLayout { + var lines: [String] = [] + textView.layoutManager.enumerateLineFragments( + forGlyphRange: NSRange(location: 0, length: textView.attributedText.length)) + { (rect, usedRect, textContainer, glyphRange, stop) in + let characterRange = self.textView.layoutManager.characterRange(forGlyphRange: glyphRange, actualGlyphRange: nil) + let line = (self.textView.text as NSString).substring(with: characterRange) + lines.append(line) + } + + onTextLayout([ + "lines": lines + ]) + } } @IBAction func callOnPress(_ sender: UITapGestureRecognizer) -> Void { @@ -82,8 +102,6 @@ class RNUITextView: UIView { fractionOfDistanceBetweenInsertionPoints: nil ) - let text = textView.attributedText.string - for child in self.subviews { guard let child = child as? RNUITextViewChild, let text = child.text else { continue diff --git a/modules/react-native-ui-text-view/ios/RNUITextViewShadow.swift b/modules/react-native-ui-text-view/ios/RNUITextViewShadow.swift index 1a0ae22274..200a99bc1a 100644 --- a/modules/react-native-ui-text-view/ios/RNUITextViewShadow.swift +++ b/modules/react-native-ui-text-view/ios/RNUITextViewShadow.swift @@ -105,24 +105,6 @@ class RNUITextViewShadow: RCTShadowView { range: NSMakeRange(0, string.length) ) -// if let textDecorationLine = child.textDecorationLine { -// if textDecorationLine == .underline || textDecorationLine == .underlineLineThrough { -// string.addAttribute( -// NSAttributedString.Key.underlineStyle, -// value: NSUnderlineStyle.single.rawValue, -// range: NSMakeRange(0, string.length) -// ) -// } -// -// if textDecorationLine == .lineThrough || textDecorationLine == .underlineLineThrough { -// string.addAttribute( -// NSAttributedString.Key.strikethroughStyle, -// value: NSUnderlineStyle.single.rawValue, -// range: NSMakeRange(0, string.length) -// ) -// } -// } - finalAttributedString.append(string) }