Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 @@ -168,6 +168,15 @@ - (void)setDisableKeyboardShortcuts:(BOOL)disableKeyboardShortcuts
[textAttributes removeObjectForKey:NSForegroundColorAttributeName];
}

// UITextField's placeholder label keeps the paragraph style of the previous placeholder and applies it to a
// placeholder that has none, so a line height that was removed from the text attributes would still be used.
// A paragraph style matching the label's initial one avoids that.
if (textAttributes[NSParagraphStyleAttributeName] == nil) {
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
textAttributes[NSParagraphStyleAttributeName] = paragraphStyle;
}

return textAttributes;
}

Expand Down
57 changes: 57 additions & 0 deletions packages/react-native/React/Tests/Text/RCTUITextFieldTests.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#import <React/RCTUITextField.h>
#import <XCTest/XCTest.h>

static NSParagraphStyle *RCTPlaceholderParagraphStyle(RCTUITextField *textField)
{
return [textField.attributedPlaceholder attribute:NSParagraphStyleAttributeName atIndex:0 effectiveRange:NULL];
}

@interface RCTUITextFieldTests : XCTestCase
@end

@implementation RCTUITextFieldTests

- (void)testPlaceholderDropsLineHeightRemovedFromTextAttributes
{
NSMutableParagraphStyle *lineHeightParagraphStyle = [NSMutableParagraphStyle new];
lineHeightParagraphStyle.minimumLineHeight = 34;
lineHeightParagraphStyle.maximumLineHeight = 34;
RCTUITextField *textField = [RCTUITextField new];
textField.placeholder = @"Search";
textField.defaultTextAttributes = @{
NSFontAttributeName : [UIFont systemFontOfSize:17],
NSParagraphStyleAttributeName : lineHeightParagraphStyle,
};

textField.defaultTextAttributes = @{NSFontAttributeName : [UIFont systemFontOfSize:17]};

NSParagraphStyle *paragraphStyle = RCTPlaceholderParagraphStyle(textField);
XCTAssertNotNil(paragraphStyle);
XCTAssertEqual(paragraphStyle.minimumLineHeight, 0);
XCTAssertEqual(paragraphStyle.maximumLineHeight, 0);
XCTAssertEqual(paragraphStyle.lineBreakMode, NSLineBreakByTruncatingTail);
}

- (void)testPlaceholderKeepsParagraphStyleFromTextAttributes
{
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.alignment = NSTextAlignmentCenter;
RCTUITextField *textField = [RCTUITextField new];
textField.placeholder = @"Search";

textField.defaultTextAttributes = @{
NSFontAttributeName : [UIFont systemFontOfSize:17],
NSParagraphStyleAttributeName : paragraphStyle,
};

XCTAssertEqual(RCTPlaceholderParagraphStyle(textField).alignment, NSTextAlignmentCenter);
}

@end
Loading