Skip to content

Commit

Permalink
fix(ios): use searchtextfield exposed in iOS 13 to set color (#11566)
Browse files Browse the repository at this point in the history
Co-authored-by: Samir Mohammed <ssjsamir@users.noreply.github.com>
  • Loading branch information
vijaysingh-axway and ssjsamir committed Apr 8, 2020
1 parent 1c0d85f commit f881591
Showing 1 changed file with 20 additions and 7 deletions.
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];
} 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

0 comments on commit f881591

Please sign in to comment.