handle presses
This commit is contained in:
@@ -9,7 +9,11 @@ class RNUITextView: UIView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override init(frame: CGRect) {
|
override init(frame: CGRect) {
|
||||||
self.textView = UITextView()
|
if #available(iOS 16.0, *) {
|
||||||
|
textView = UITextView(usingTextLayoutManager: false)
|
||||||
|
} else {
|
||||||
|
textView = UITextView()
|
||||||
|
}
|
||||||
|
|
||||||
// Disable scrolling
|
// Disable scrolling
|
||||||
textView.isScrollEnabled = false
|
textView.isScrollEnabled = false
|
||||||
@@ -23,9 +27,14 @@ class RNUITextView: UIView {
|
|||||||
|
|
||||||
// Init
|
// Init
|
||||||
super.init(frame: frame)
|
super.init(frame: frame)
|
||||||
|
self.clipsToBounds = true
|
||||||
|
|
||||||
// Add the view
|
// Add the view
|
||||||
addSubview(textView)
|
addSubview(textView)
|
||||||
|
|
||||||
|
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(callOnPress(_:)))
|
||||||
|
tapGestureRecognizer.isEnabled = true
|
||||||
|
textView.addGestureRecognizer(tapGestureRecognizer)
|
||||||
}
|
}
|
||||||
|
|
||||||
required init?(coder: NSCoder) {
|
required init?(coder: NSCoder) {
|
||||||
@@ -39,10 +48,6 @@ class RNUITextView: UIView {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override func didUpdateReactSubviews() {
|
|
||||||
// Do nothing
|
|
||||||
}
|
|
||||||
|
|
||||||
func setText(string: NSAttributedString, size: CGSize, numberOfLines: Int) -> Void {
|
func setText(string: NSAttributedString, size: CGSize, numberOfLines: Int) -> Void {
|
||||||
self.textView.frame.size = size
|
self.textView.frame.size = size
|
||||||
self.textView.textContainer.maximumNumberOfLines = numberOfLines
|
self.textView.textContainer.maximumNumberOfLines = numberOfLines
|
||||||
@@ -51,13 +56,48 @@ class RNUITextView: UIView {
|
|||||||
|
|
||||||
@IBAction func callOnPress(_ sender: UITapGestureRecognizer) -> Void {
|
@IBAction func callOnPress(_ sender: UITapGestureRecognizer) -> Void {
|
||||||
// If we find a child, then call onPress
|
// If we find a child, then call onPress
|
||||||
// if let child = getPressed(sender) {
|
if let child = getPressed(sender) {
|
||||||
// if textView.selectedTextRange == nil {
|
if textView.selectedTextRange == nil, let onPress = child.onPress {
|
||||||
// child.onTextPress()
|
onPress(["": ""])
|
||||||
// } else {
|
} else {
|
||||||
// // Clear the selected text range if we are not pressing on a link
|
// Clear the selected text range if we are not pressing on a link
|
||||||
// textView.selectedTextRange = nil
|
textView.selectedTextRange = nil
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to get the pressed segment
|
||||||
|
func getPressed(_ sender: UITapGestureRecognizer) -> RNUITextViewChild? {
|
||||||
|
let layoutManager = textView.layoutManager
|
||||||
|
var location = sender.location(in: textView)
|
||||||
|
|
||||||
|
// Remove the padding
|
||||||
|
location.x -= textView.textContainerInset.left
|
||||||
|
location.y -= textView.textContainerInset.top
|
||||||
|
|
||||||
|
// Get the index of the char
|
||||||
|
let charIndex = layoutManager.characterIndex(
|
||||||
|
for: location,
|
||||||
|
in: textView.textContainer,
|
||||||
|
fractionOfDistanceBetweenInsertionPoints: nil
|
||||||
|
)
|
||||||
|
|
||||||
|
let text = textView.attributedText.string
|
||||||
|
|
||||||
|
for child in self.subviews {
|
||||||
|
guard let child = child as? RNUITextViewChild, let text = child.text else {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
let range = text.range(of: text)
|
||||||
|
|
||||||
|
if let lowerBound = range?.lowerBound, let upperBound = range?.upperBound {
|
||||||
|
if charIndex >= lowerBound.utf16Offset(in: text) && charIndex <= upperBound.utf16Offset(in: text) {
|
||||||
|
return child
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,21 +1,4 @@
|
|||||||
class RNUITextViewChild: UIView {
|
class RNUITextViewChild: UIView {
|
||||||
@objc var text: String = "" {
|
@objc var text: String?
|
||||||
didSet {
|
@objc var onPress: RCTDirectEventBlock?
|
||||||
print(text)
|
|
||||||
let superview = self.superview as? RNUITextView
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@objc var textStyle: TextStyle? {
|
|
||||||
didSet {
|
|
||||||
print(textStyle)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override init(frame: CGRect) {
|
|
||||||
super.init(frame: frame)
|
|
||||||
}
|
|
||||||
|
|
||||||
required init?(coder: NSCoder) {
|
|
||||||
fatalError("init(coder:) has not been implemented")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,4 +24,7 @@ RCT_REMAP_SHADOW_PROPERTY(fontWeight, fontWeight, NSString)
|
|||||||
RCT_REMAP_SHADOW_PROPERTY(letterSpacing, letterSpacing, CGFloat)
|
RCT_REMAP_SHADOW_PROPERTY(letterSpacing, letterSpacing, CGFloat)
|
||||||
RCT_REMAP_SHADOW_PROPERTY(lineHeight, lineHeight, CGFloat)
|
RCT_REMAP_SHADOW_PROPERTY(lineHeight, lineHeight, CGFloat)
|
||||||
RCT_REMAP_SHADOW_PROPERTY(pointerEvents, pointerEvents, NSString)
|
RCT_REMAP_SHADOW_PROPERTY(pointerEvents, pointerEvents, NSString)
|
||||||
|
|
||||||
|
RCT_EXPORT_VIEW_PROPERTY(text, NSString)
|
||||||
|
RCT_EXPORT_VIEW_PROPERTY(onPress, RCTBubblingEventBlock)
|
||||||
@end
|
@end
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ const textDefaults: TextProps = {
|
|||||||
selectable: true,
|
selectable: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
export function UITextView({style, children, onPress, ...rest}: TextProps) {
|
export function UITextView({style, children, ...rest}: TextProps) {
|
||||||
const [isAncestor, rootStyle] = useTextAncestorContext()
|
const [isAncestor, rootStyle] = useTextAncestorContext()
|
||||||
|
|
||||||
// Flatten the styles, and apply the root styles when needed
|
// Flatten the styles, and apply the root styles when needed
|
||||||
@@ -39,7 +39,6 @@ export function UITextView({style, children, onPress, ...rest}: TextProps) {
|
|||||||
key={index}
|
key={index}
|
||||||
style={flattenedStyle}
|
style={flattenedStyle}
|
||||||
text={c}
|
text={c}
|
||||||
onTextPress={onPress}
|
|
||||||
{...rest}
|
{...rest}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
@@ -60,7 +59,6 @@ export function UITextView({style, children, onPress, ...rest}: TextProps) {
|
|||||||
key={index}
|
key={index}
|
||||||
style={flattenedStyle}
|
style={flattenedStyle}
|
||||||
text={c}
|
text={c}
|
||||||
onTextPress={onPress}
|
|
||||||
{...rest}
|
{...rest}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user