Skip to content

Commit

Permalink
Merge branch 'master' into TIMOB-25619
Browse files Browse the repository at this point in the history
  • Loading branch information
ssjsamir committed Feb 26, 2018
2 parents 4a1f85b + 15735a6 commit b8e3e6e
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 4 deletions.
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: [android, iphone, ipad]
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: [android, iphone, ipad]
since: "7.1.0"
description: |
Sets the color of the <Titanium.UI.SearchBar.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 Windows as well
// We have in in Ti.UI.Android.SearchView for Android, but need more parity here
it.windowsMissing('.hintTextColor', function () {
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 Windows as well
it.windowsMissing('.color', function () {
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');
});
});

0 comments on commit b8e3e6e

Please sign in to comment.