Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [iOS] add caretHeight and caretYoffset to TextInput component #37147

Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ const RCTTextInputViewConfig = {
editable: true,
inputAccessoryViewID: true,
caretHidden: true,
caretHeight: true,
caretYOffset: true,
enablesReturnKeyAutomatically: true,
placeholderTextColor: {
process: require('../../StyleSheet/processColor').default,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,20 @@ export interface TextInputProps
*/
caretHidden?: boolean | undefined;

/**
* Allows to adjust caret height.
* The default value is 0, which means the height of the caret will be calculated automatically
* @platform ios
*/
caretHeight?: number | undefined;
sammy-SC marked this conversation as resolved.
Show resolved Hide resolved

/**
* Allows to adjust caret position relative to the Y axis
* The default value is 0.
* @platform ios
*/
caretYOffset?: number | undefined;
sammy-SC marked this conversation as resolved.
Show resolved Hide resolved

/**
* If true, context menu is hidden. The default value is false.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,20 @@ export type Props = $ReadOnly<{|
*/
caretHidden?: ?boolean,

/**
* Allows to adjust caret height.
* The default value is 0, which means the height of the caret will be calculated automatically
* @platform ios
*/
caretYOffset?: ?number,
sammy-SC marked this conversation as resolved.
Show resolved Hide resolved

/**
* Allows to adjust caret position relative to the Y axis
* The default value is 0.
* @platform ios
*/
caretYHeight?: ?number,
sammy-SC marked this conversation as resolved.
Show resolved Hide resolved

/*
* If `true`, contextMenuHidden is hidden. The default value is `false`.
*/
Expand Down
14 changes: 14 additions & 0 deletions packages/react-native/Libraries/Components/TextInput/TextInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,20 @@ type IOSProps = $ReadOnly<{|
* @platform ios
*/
smartInsertDelete?: ?boolean,

/**
* Allows to adjust caret height.
* The default value is 0, which means the height of the caret will be calculated automatically
* @platform ios
*/
caretYOffset?: ?number,
sammy-SC marked this conversation as resolved.
Show resolved Hide resolved

/**
* Allows to adjust caret position relative to the Y axis
* The default value is 0.
* @platform ios
*/
caretHeight?: ?number,
sammy-SC marked this conversation as resolved.
Show resolved Hide resolved
|}>;

type AndroidProps = $ReadOnly<{|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, assign) UITextFieldViewMode clearButtonMode;

@property (nonatomic, assign) BOOL caretHidden;
@property (nonatomic, assign) CGFloat caretYOffset;
@property (nonatomic, assign) CGFloat caretHeight;

@property (nonatomic, strong, nullable) NSString *inputAccessoryViewID;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,33 @@
#import <React/RCTBackedTextInputDelegateAdapter.h>
#import <React/RCTTextAttributes.h>

//the UITextSelectionRect subclass needs to be created because the original version is not writable
@interface CustomTextSelectionRect : UITextSelectionRect
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UITextSelectionRect is explicitly documented as being an abstract base class so this makes sense. Maybe mention that instead of it not being writable?

Use RCT as prefix

Suggested change
@interface CustomTextSelectionRect : UITextSelectionRect
@interface RCTTextSelectionRect : UITextSelectionRect

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, changed


@property (nonatomic, assign) CGRect rect;
@property (nonatomic, assign) NSWritingDirection writingDirection;
@property (nonatomic, assign) BOOL containsStart; // Returns YES if the rect contains the start of the selection.
@property (nonatomic, assign) BOOL containsEnd; // Returns YES if the rect contains the end of the selection.
@property (nonatomic, assign) BOOL isVertical; // Returns YES if the rect is for vertically oriented text.

@end

@implementation CustomTextSelectionRect {
CGRect _customRect;
NSWritingDirection _customWritingDirection;
BOOL _customContainsStart;
BOOL _customContainsEnd;
BOOL _customIsVertical;
}

@synthesize rect = _customRect;
@synthesize writingDirection = _customWritingDirection;
@synthesize containsStart = _customContainsStart;
@synthesize containsEnd = _customContainsEnd;
@synthesize isVertical = _customIsVertical;

@end

@implementation RCTUITextView {
UILabel *_placeholderView;
UITextView *_detachedTextView;
Expand Down Expand Up @@ -307,11 +334,44 @@ - (void)_updatePlaceholder

- (CGRect)caretRectForPosition:(UITextPosition *)position
{
CGRect originalRect = [super caretRectForPosition:position];

if (_caretHidden) {
return CGRectZero;
}

return [super caretRectForPosition:position];
if(_caretYOffset != 0) {
originalRect.origin.y += _caretYOffset;
}

if(_caretHeight != 0) {
originalRect.size.height = _caretHeight;
}

return originalRect;
}

- (NSArray *)selectionRectsForRange:(UITextRange *)range {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this override needed? I don't understand what it is trying to achieve on top of handling _caretYOffset and _caretHeight in the method caretRectForPosition above.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The goal is to avoid the cursor touching the the symbols of the previous line in the second and subsequent lines:
Screenshot 2024-02-26 at 09 42 32

This is the default behavior derived from caretRectForPosition. To fix this, I added two new properties _caretYOffset and _caretHeight to calculate the caret position and caret height, thus preventing the cursor from overlapping the previous line of text.

To keep the same effect on selection selectionRectsForRange needs to be override as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a screenshot of what this looks like with a multi-line text selection?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, here is a screenshot for multi-line text selection:

Screenshot 2024-04-05 at 13 07 49

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- (NSArray *)selectionRectsForRange:(UITextRange *)range {
- (NSArray<UITextSelectionRect *> *)selectionRectsForRange:(UITextRange *)range {

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

NSArray *superRects = [super selectionRectsForRange:range];
if(_caretYOffset != 0 && _caretHeight != 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be ||, so this works if you specify either one of the properties?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The block should only be executed if both properties are set.

NSMutableArray *customTextSelectionRects = [NSMutableArray array];

for (UITextSelectionRect *rect in superRects) {
CustomTextSelectionRect *customTextRect = [[CustomTextSelectionRect alloc] init];

customTextRect.rect = CGRectMake(rect.rect.origin.x, rect.rect.origin.y + _caretYOffset, rect.rect.size.width, _caretHeight);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This then needs to handle _caretHeight being potentially 0.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The block should only be executed if both properties are set.

customTextRect.writingDirection = rect.writingDirection;
customTextRect.containsStart = rect.containsStart;
customTextRect.containsEnd = rect.containsEnd;
customTextRect.isVertical = rect.isVertical;
[customTextSelectionRects addObject:customTextRect];
}

return customTextSelectionRects;

}
return superRects;

}

#pragma mark - Utility Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, assign) BOOL contextMenuHidden;
@property (nonatomic, assign, getter=isEditable) BOOL editable;
@property (nonatomic, assign) BOOL caretHidden;
@property (nonatomic, assign) CGFloat caretYOffset;
@property (nonatomic, assign) CGFloat caretHeight;
@property (nonatomic, assign) BOOL enablesReturnKeyAutomatically;
@property (nonatomic, assign) UITextFieldViewMode clearButtonMode;
@property (nonatomic, getter=isScrollEnabled) BOOL scrollEnabled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ @implementation RCTBaseTextInputViewManager {
RCT_REMAP_VIEW_PROPERTY(selectionColor, backedTextInputView.tintColor, UIColor)
RCT_REMAP_VIEW_PROPERTY(spellCheck, backedTextInputView.spellCheckingType, UITextSpellCheckingType)
RCT_REMAP_VIEW_PROPERTY(caretHidden, backedTextInputView.caretHidden, BOOL)
RCT_REMAP_VIEW_PROPERTY(caretYOffset, backedTextInputView.caretYOffset, CGFloat)
RCT_REMAP_VIEW_PROPERTY(caretHeight, backedTextInputView.caretHeight, CGFloat)
RCT_REMAP_VIEW_PROPERTY(clearButtonMode, backedTextInputView.clearButtonMode, UITextFieldViewMode)
RCT_REMAP_VIEW_PROPERTY(scrollEnabled, backedTextInputView.scrollEnabled, BOOL)
RCT_REMAP_VIEW_PROPERTY(secureTextEntry, backedTextInputView.secureTextEntry, BOOL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, weak) id<RCTBackedTextInputDelegate> textInputDelegate;

@property (nonatomic, assign) BOOL caretHidden;
@property (nonatomic, assign) BOOL caretYOffset;
@property (nonatomic, assign) BOOL caretHeight;
@property (nonatomic, assign) BOOL contextMenuHidden;
@property (nonatomic, assign, readonly) BOOL textWasPasted;
@property (nonatomic, assign, readonly) BOOL dictationRecognizing;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &
_backedTextInputView.caretHidden = newTextInputProps.traits.caretHidden;
}

if(newTextInputProps.traits.caretYOffset != oldTextInputProps.traits.caretYOffset) {
_backedTextInputView.caretYOffset = newTextInputProps.traits.caretYOffset;
}

if(newTextInputProps.traits.caretHeight != oldTextInputProps.traits.caretHeight) {
_backedTextInputView.caretHeight = newTextInputProps.traits.caretHeight;
}

if (newTextInputProps.traits.clearButtonMode != oldTextInputProps.traits.clearButtonMode) {
_backedTextInputView.clearButtonMode =
RCTUITextFieldViewModeFromTextInputAccessoryVisibilityMode(newTextInputProps.traits.clearButtonMode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ void RCTCopyBackedTextInput(
toTextInput.keyboardAppearance = fromTextInput.keyboardAppearance;
toTextInput.spellCheckingType = fromTextInput.spellCheckingType;
toTextInput.caretHidden = fromTextInput.caretHidden;
toTextInput.caretYOffset = fromTextInput.caretYOffset;
toTextInput.caretHeight = fromTextInput.caretHeight;
toTextInput.clearButtonMode = fromTextInput.clearButtonMode;
toTextInput.scrollEnabled = fromTextInput.scrollEnabled;
toTextInput.secureTextEntry = fromTextInput.secureTextEntry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,18 @@ class TextInputTraits final {
*/
bool caretHidden{false};

/*
* iOS-only (inherently iOS-specific)
* Default value: 0 with a default font.
*/
int caretHeight{0};

/*
* iOS-only (inherently iOS-specific)
* Default value: 0 means that the caret offset will have the default value
*/
int caretYOffset{0};

/*
* Controls the visibility of a `Clean` button.
* iOS-only (implemented only on iOS for now)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ static TextInputTraits convertRawProp(
"caretHidden",
sourceTraits.caretHidden,
defaultTraits.caretHidden);
traits.caretYOffset = convertRawProp(
context,
rawProps,
"caretYOffset",
sourceTraits.caretYOffset,
defaultTraits.caretYOffset);
traits.caretHeight= convertRawProp(
context,
rawProps,
"caretHeight",
sourceTraits.caretHeight,
defaultTraits.caretHeight);
traits.clearButtonMode = convertRawProp(
context,
rawProps,
Expand Down