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

[TIMOB-10368] iOS: Expose "color" and "hintTextColor" to Ti.UI.SearchBar #9772

Merged
merged 11 commits into from
Feb 26, 2018
17 changes: 17 additions & 0 deletions apidoc/Titanium/UI/SearchBar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,28 @@ properties:
since: "5.4.0"
platforms: [iphone,ipad]

- name: color
summary: Color of the text in this text field, as a color name or hex triplet.
description: |
For information about color values, see the "Colors" section of <Titanium.UI>.
type: String
platforms: [iphone, ipad]
Copy link
Contributor

Choose a reason for hiding this comment

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

Android already supports this, just wasn't documented. Would you mind adding "android" to the supported platform list please?

since: "7.1.0"

- name: hintText
summary: Text to show when the search bar field is not focused.
type: String
default: On iOS, "Search"; on Android, no hint text.

- name: hintTextColor
summary: Hint text color to display when the field is empty.
platforms: [iphone, ipad]
Copy link
Contributor

Choose a reason for hiding this comment

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

Android already supports this property too.

since: "7.1.0"
description: |
Sets the color of the <Titanium.UI.TextField.hintText>.
type: String
default: The platform's default hint text color.

- name: hinttextid
summary: |
Key identifying a string from the locale file to use for the
Expand Down
32 changes: 32 additions & 0 deletions iphone/Classes/TiUISearchBar.m
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,38 @@ - (void)setShowCancel_:(id)value
- (void)setHintText_:(id)value
{
[[self searchBar] setPlaceholder:[TiUtils stringValue:value]];

if ([[self proxy] valueForUndefinedKey:@"hintTextColor"]) {
[self setHintTextColor_:[[self proxy] valueForUndefinedKey:@"hintTextColor"]];
}
}

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

NSAttributedString *placeHolder = [[NSAttributedString alloc] initWithString:[TiUtils stringValue:hintText] attributes:@{ NSForegroundColorAttributeName : [[TiUtils colorValue:value] _color] }];

if ([TiUtils isIOS9OrGreater]) {
[[UITextField appearanceWhenContainedInInstancesOfClasses:@[ [UISearchBar class] ]] setAttributedPlaceholder:placeHolder];
} else {
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setAttributedPlaceholder:placeHolder];
}
RELEASE_TO_NIL(placeHolder);
}

- (void)setColor_:(id)value
{
// TIMOB-10368
// Remove this hack again once iOS exposes this as a public API
UIView *searchContainerView = [[[self searchBar] subviews] firstObject];

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

- (void)setKeyboardType_:(id)value
Expand Down
14 changes: 14 additions & 0 deletions iphone/Classes/TiUISearchBarProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ - (UISearchBar *)searchBar

- (void)setSearchBar:(UISearchBar *)searchBar
{
// We need to manually handle this property as it will be overwritten
// by the search controller otherwise (TIMOB-10368)
if ([self valueForKey:@"color"] != nil) {
UIView *searchContainerView = [[searchBar subviews] firstObject];
UIColor *color = [TiUtils colorValue:[self valueForKey:@"color"]].color;

[[searchContainerView subviews] enumerateObjectsUsingBlock:^(__kindof UIView *_Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
if ([obj isKindOfClass:[UITextField class]]) {
[(UITextField *)obj setTextColor:color];
*stop = YES;
}
}];
}

// In UISearchController searchbar is readonly. We have to replace that search bar with existing search bar of proxy.
[(TiUISearchBar *)[self view] setSearchBar:searchBar];
}
Expand Down
5 changes: 1 addition & 4 deletions iphone/Classes/TiUITextField.m
Original file line number Diff line number Diff line change
Expand Up @@ -405,11 +405,8 @@ - (void)setHintText_:(id)value

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

if (!hintText) {
hintText = @"";
}
NSAttributedString *placeHolder = [[NSAttributedString alloc] initWithString:[TiUtils stringValue:hintText] attributes:@{ NSForegroundColorAttributeName : [[TiUtils colorValue:value] _color] }];
[(TiTextField *)[self textWidgetView] setAttributedPlaceholder:placeHolder];
RELEASE_TO_NIL(placeHolder);
Expand Down
50 changes: 50 additions & 0 deletions tests/Resources/ti.ui.searchbar.addontest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Appcelerator Titanium Mobile
* Copyright (c) 2011-Present by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
/* eslint-env mocha */
/* global Ti */
/* eslint no-unused-expressions: "off" */
'use strict';
var should = require('./utilities/assertions');

describe('Titanium.UI.SearchBar', function () {
var win;

afterEach(function () {
if (win) {
win.close();
}
win = null;
});

// TODO: Expose on Android / Windows as well
// We have in in Ti.UI.Android.SearchView for Android, but need more parity here
it.ios('.hintTextColor', function () {
Copy link
Contributor

Choose a reason for hiding this comment

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

Android can run this unit test too.
Change to it.windowsMissing?

var searchBar = Ti.UI.createSearchBar({
hintText: 'Enter E-Mail ...',
hintTextColor: 'red'
});
should(searchBar.getHintTextColor).be.a.Function;
should(searchBar.hintTextColor).eql('red');
should(searchBar.getHintTextColor()).eql('red');
searchBar.hintTextColor = 'blue';
should(searchBar.hintTextColor).eql('blue');
should(searchBar.getHintTextColor()).eql('blue');
});

// TODO: Expose on Android / Windows as well
it.ios('.color', function () {
Copy link
Contributor

Choose a reason for hiding this comment

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

Android can run this unit test.
Change to it.windowsMissing?

var searchBar = Ti.UI.createSearchBar({
color: 'red'
});
should(searchBar.getColor).be.a.Function;
should(searchBar.color).eql('red');
should(searchBar.getColor()).eql('red');
searchBar.color = 'blue';
should(searchBar.color).eql('blue');
should(searchBar.getColor()).eql('blue');
});
});