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

fix(iOS)(9_0_X): use searchtextfield exposed in iOS 13 to set color #11566

Merged
merged 3 commits into from
Apr 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 20 additions & 7 deletions iphone/Classes/TiUISearchBar.m
Original file line number Diff line number Diff line change
Expand Up @@ -118,31 +118,44 @@ - (void)setHintText_:(id)value
[[self searchBar] setPlaceholder:[TiUtils stringValue:value]];

if ([[self proxy] valueForUndefinedKey:@"hintTextColor"]) {
[self setHintTextColor_:[[self proxy] valueForUndefinedKey:@"hintTextColor"]];
if ([TiUtils isIOSVersionOrGreater:@"13.0"]) {
// Need to call a bit later to get searchTextField loaded
[self performSelector:@selector(setHintTextColor_:) withObject:[[self proxy] valueForUndefinedKey:@"hintTextColor"] afterDelay:.01];
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you have more info why this is required? I'd love to know why this only works after adding the delay.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In debugging I found that SearchTextField inside searchbar takes sometime to load and before loading the searchTextField setting this property is not working.

} else {
[self setHintTextColor_:[[self proxy] valueForUndefinedKey:@"hintTextColor"]];
}
}
}

- (void)setHintTextColor_:(id)value
{
id hintText = [[self proxy] valueForUndefinedKey:@"hintText"] ?: @"";
id hintText = [self.proxy valueForUndefinedKey:@"hintText"] ?: @"";

NSAttributedString *placeholder = [[NSAttributedString alloc] initWithString:[TiUtils stringValue:hintText] attributes:@{ NSForegroundColorAttributeName : [[TiUtils colorValue:value] _color] }];
[[UITextField appearanceWhenContainedInInstancesOfClasses:@ [[UISearchBar class]]] setAttributedPlaceholder:placeholder];
if ([TiUtils isIOSVersionOrGreater:@"13.0"]) {
#if IS_SDK_IOS_13
self.searchBar.searchTextField.attributedPlaceholder = placeholder;
#endif
} else {
[UITextField appearanceWhenContainedInInstancesOfClasses:@ [[UISearchBar class]]].attributedPlaceholder = placeholder;
}
RELEASE_TO_NIL(placeholder);
}

- (void)setColor_:(id)value
{
if ([TiUtils isIOSVersionOrGreater:@"13.0"]) {
[[[self searchBar] searchTextField] setTextColor:[[TiUtils colorValue:value] _color]];
#if IS_SDK_IOS_13
self.searchBar.searchTextField.textColor = [[TiUtils colorValue:value] _color];
#endif
} else {
// TIMOB-10368
// Remove this hack again once iOS exposes this as a public API
UIView *searchContainerView = [[[self searchBar] subviews] firstObject];
UIView *searchContainerView = self.searchBar.subviews.firstObject;

[[searchContainerView subviews] enumerateObjectsUsingBlock:^(__kindof UIView *_Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
[searchContainerView.subviews enumerateObjectsUsingBlock:^(__kindof UIView *_Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
if ([obj isKindOfClass:[UITextField class]]) {
[(UITextField *)obj setTextColor:[[TiUtils colorValue:value] _color]];
((UITextField *)obj).textColor = [[TiUtils colorValue:value] _color];
*stop = YES;
}
}];
Expand Down