Skip to content

Commit be85af4

Browse files
author
vakrilov
committed
Text transform - no default value
1 parent 86b9be3 commit be85af4

File tree

11 files changed

+147
-39
lines changed

11 files changed

+147
-39
lines changed

apps/app/ui-tests-app/tab-view/main-page.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export function pageLoaded(args: EventData) {
1919
examples.set("tabmore", "tab-view/tab-view-more");
2020
examples.set("tabViewCss", "tab-view/tab-view-css");
2121
examples.set("tab-view-icons", "tab-view/tab-view-icon");
22+
examples.set("text-transform", "tab-view/text-transform");
2223

2324
let viewModel = new SubMainPageViewModel(wrapLayout, examples);
2425
page.bindingContext = viewModel;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Page } from "tns-core-modules/ui/page";
2+
3+
const CSS = `
4+
#tab1 { text-transform: none; }
5+
#tab2 { text-transform: lowercase; }
6+
#tab3 { text-transform: uppercase; }
7+
#tab4 { text-transform: capitalize; }
8+
`
9+
10+
export function applyTap(args) {
11+
var page = <Page>args.object.page;
12+
page.css = CSS;
13+
}
14+
15+
export function resetTap(args) {
16+
var page = <Page>args.object.page;
17+
page.css = "";
18+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="onNavigate">
2+
<TabView>
3+
<TabView.items>
4+
<TabViewItem title="tAb ViEw" id="tab1">
5+
<TabViewItem.view>
6+
<StackLayout>
7+
<Button text="apply" tap="applyTap"/>
8+
<Button text="reset" tap="resetTap"/>
9+
</StackLayout>
10+
</TabViewItem.view>
11+
</TabViewItem>
12+
<TabViewItem title="tAb ViEw" id="tab2">
13+
<TabViewItem.view>
14+
<Label text="empty tab" />
15+
</TabViewItem.view>
16+
</TabViewItem>
17+
<TabViewItem title="tAb ViEw" id="tab3">
18+
<TabViewItem.view>
19+
<Label text="empty tab" />
20+
</TabViewItem.view>
21+
</TabViewItem>
22+
<TabViewItem title="tAb ViEw" id="tab4">
23+
<TabViewItem.view>
24+
<Label text="empty tab" />
25+
</TabViewItem.view>
26+
</TabViewItem>
27+
</TabView.items>
28+
</TabView>
29+
</Page>

tests/app/ui/text-field/text-field-tests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ export function test_IntegrationTest_Transform_Decoration_Spacing_WithoutFormatt
542542
let view = new textFieldModule.TextField();
543543
helper.buildUIAndRunTest(view, function (views: Array<viewModule.View>) {
544544
TKUnit.assertEqual(view.text, "", "Text");
545-
TKUnit.assertEqual(view.style.textTransform, enums.TextTransform.none, "TextTransform default value");
545+
TKUnit.assertNull(view.style.textTransform, "TextTransform default value");
546546
TKUnit.assertEqual(view.style.textDecoration, enums.TextDecoration.none, "TextDecoration default value");
547547
TKUnit.assertTrue(view.style.letterSpacing === 0, "LetterSpacing default value");
548548

tns-core-modules/ui/tab-view/tab-view-common.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
} from "../core/view";
66

77
export * from "../core/view";
8+
import { TextTransform } from "../text-base";
89

910
export const traceCategory = "TabView";
1011

@@ -13,6 +14,13 @@ export abstract class TabViewItemBase extends ViewBase implements TabViewItemDef
1314
private _view: View;
1415
private _iconSource: string;
1516

17+
get textTransform(): TextTransform {
18+
return this.style.textTransform;
19+
}
20+
set textTransform(value: TextTransform) {
21+
this.style.textTransform = value;
22+
}
23+
1624
public _addChildFromBuilder(name: string, value: any): void {
1725
if (value instanceof View) {
1826
this.view = value;
@@ -76,6 +84,13 @@ export class TabViewBase extends View implements TabViewDefinition, AddChildFrom
7684
public androidOffscreenTabLimit: number;
7785
public iosIconRenderingMode: "automatic" | "alwaysOriginal" | "alwaysTemplate";
7886

87+
get androidSelectedTabHighlightColor(): Color {
88+
return this.style.androidSelectedTabHighlightColor;
89+
}
90+
set androidSelectedTabHighlightColor(value: Color) {
91+
this.style.androidSelectedTabHighlightColor = value;
92+
}
93+
7994
get tabTextColor(): Color {
8095
return this.style.tabTextColor;
8196
}

tns-core-modules/ui/tab-view/tab-view.android.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,26 @@ export class TabViewItem extends TabViewItemBase {
191191
nativeView: android.widget.TextView;
192192
public tabItemSpec: org.nativescript.widgets.TabItemSpec;
193193
public index: number;
194+
private _defaultTransformationMethod: android.text.method.TransformationMethod;
195+
196+
public initNativeView(): void {
197+
super.initNativeView();
198+
if (this.nativeView) {
199+
this._defaultTransformationMethod = this.nativeView.getTransformationMethod();
200+
}
201+
}
202+
203+
public resetNativeView(): void {
204+
super.resetNativeView();
205+
if (this.nativeView) {
206+
// We reset it here too because this could be changed by multiple properties - whiteSpace, secure, textTransform
207+
this.nativeView.setTransformationMethod(this._defaultTransformationMethod);
208+
}
209+
}
210+
211+
public createNativeView() {
212+
return this.nativeView;
213+
}
194214

195215
public setNativeView(textView: android.widget.TextView): void {
196216
this.nativeView = textView;
@@ -225,13 +245,19 @@ export class TabViewItem extends TabViewItemBase {
225245
this.nativeView.setTypeface(value instanceof Font ? value.getAndroidTypeface() : value);
226246
}
227247

228-
[textTransformProperty.getDefault](): TextTransform {
229-
return "none";
248+
[textTransformProperty.getDefault](): "default" {
249+
return "default";
230250
}
231-
[textTransformProperty.setNative](value: TextTransform) {
251+
[textTransformProperty.setNative](value: TextTransform | "default") {
232252
const tv = this.nativeView;
233-
const result = getTransformedText(this.title, value);
234-
tv.setText(result);
253+
if (value === "default") {
254+
tv.setTransformationMethod(this._defaultTransformationMethod);
255+
tv.setText(this.title);
256+
} else {
257+
const result = getTransformedText(this.title, value);
258+
tv.setText(result);
259+
tv.setTransformationMethod(null);
260+
}
235261
}
236262
}
237263

tns-core-modules/ui/tab-view/tab-view.d.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Contains the TabView class, which represents a standard content component with tabs.
33
*/
44
import { View, ViewBase, Property, CssProperty, Style, EventData, Color } from "../core/view";
5-
5+
import { TextTransform } from "../text-base";
66
/**
77
* Represents a tab view entry.
88
*/
@@ -21,6 +21,11 @@ export class TabViewItem extends ViewBase {
2121
* Gets or sets the icon source of the TabViewItem. This could either be a a file name or resource id.
2222
*/
2323
public iconSource: string;
24+
25+
/**
26+
* Gets or sets the text transform of the tab titles.
27+
*/
28+
textTransform: TextTransform;
2429
}
2530

2631
/**
@@ -52,6 +57,26 @@ export class TabView extends View {
5257
*/
5358
selectedIndex: number;
5459

60+
/**
61+
* Gets or sets the text color of the tabs titles.
62+
*/
63+
tabTextColor: Color;
64+
65+
/**
66+
* Gets or sets the background color of the tabs.
67+
*/
68+
tabBackgroundColor: Color;
69+
70+
/**
71+
* Gets or sets the text color of the selected tab title.
72+
*/
73+
selectedTabTextColor: Color;
74+
75+
/**
76+
* Gets or sets the color of the horizontal line drawn below the currently selected tab on Android.
77+
*/
78+
androidSelectedTabHighlightColor: Color;
79+
5580
/**
5681
* Gets the native [android widget](http://developer.android.com/reference/android/support/v4/view/ViewPager.html) that represents the user interface for this component. Valid only when running on Android OS.
5782
*/

tns-core-modules/ui/tab-view/tab-view.ios.ts

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import {
44
TabViewBase, TabViewItemBase, itemsProperty, selectedIndexProperty,
55
tabTextColorProperty, tabBackgroundColorProperty, selectedTabTextColorProperty, iosIconRenderingModeProperty,
6-
View, fontInternalProperty, layout, traceEnabled, traceWrite, traceCategories, Color
6+
View, fontInternalProperty, layout, traceEnabled, traceWrite, traceCategories, Color, initNativeView
77
} from "./tab-view-common"
88

99
import { textTransformProperty, TextTransform, getTransformedText } from "../text-base";
@@ -120,15 +120,28 @@ function updateItemTitlePosition(tabBarItem: UITabBarItem): void {
120120
}
121121

122122
export class TabViewItem extends TabViewItemBase {
123+
private _iosViewController: UIViewController;
124+
125+
public setViewController(controller: UIViewController) {
126+
this._iosViewController = controller;
127+
(<any>this)._nativeView = this.nativeView = controller.view;
128+
initNativeView(this);
129+
}
130+
131+
public disposeNativeView() {
132+
this._iosViewController = undefined;
133+
this.nativeView = undefined;
134+
}
123135

124-
_iosViewController: UIViewController;
125136
public _update() {
126137
const parent = <TabView>this.parent;
127138
let controller = this._iosViewController;
128139
if (parent && controller) {
129140
const icon = parent._getIcon(this.iconSource);
130141
const index = parent.items.indexOf(this);
131-
const tabBarItem = UITabBarItem.alloc().initWithTitleImageTag(this.title, icon, index);
142+
const title = getTransformedText(this.title, this.style.textTransform);
143+
144+
const tabBarItem = UITabBarItem.alloc().initWithTitleImageTag(title, icon, index);
132145
if (!icon) {
133146
updateItemTitlePosition(tabBarItem);
134147
}
@@ -140,6 +153,10 @@ export class TabViewItem extends TabViewItemBase {
140153
controller.tabBarItem = tabBarItem;
141154
}
142155
}
156+
157+
[textTransformProperty.setNative](value: TextTransform) {
158+
this._update();
159+
}
143160
}
144161

145162
export class TabView extends TabViewBase {
@@ -248,7 +265,7 @@ export class TabView extends TabViewBase {
248265
newController.view.addSubview(item.view.ios);
249266
}
250267

251-
item._iosViewController = newController;
268+
item.setViewController(newController);
252269

253270
const icon = this._getIcon(item.iconSource);
254271
const tabBarItem = UITabBarItem.alloc().initWithTitleImageTag((item.title || ""), icon, i);
@@ -356,18 +373,6 @@ export class TabView extends TabViewBase {
356373
}
357374
}
358375

359-
private _updateIOSTabBarTextTransform(newValue: TextTransform): void {
360-
if (!this.items) {
361-
return;
362-
}
363-
364-
const tabBar = this.ios.tabBar;
365-
for (let i = 0, count = tabBar.items.count; i < count; i++) {
366-
const item = tabBar.items[i];
367-
item.title = getTransformedText(item.title, newValue);
368-
}
369-
}
370-
371376
[selectedIndexProperty.getDefault](): number {
372377
return -1;
373378
}
@@ -410,14 +415,6 @@ export class TabView extends TabViewBase {
410415
this._updateIOSTabBarColorsAndFonts();
411416
}
412417

413-
// TODO: Move this to TabViewItem
414-
[textTransformProperty.getDefault](): TextTransform {
415-
return "none";
416-
}
417-
[textTransformProperty.setNative](value: TextTransform) {
418-
this._updateIOSTabBarTextTransform(value);
419-
}
420-
421418
// TODO: Move this to TabViewItem
422419
[fontInternalProperty.getDefault](): Font {
423420
return null;

tns-core-modules/ui/text-base/text-base-common.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,6 @@ export namespace TextTransform {
229229
export const textTransformProperty = new CssProperty<Style, TextTransform>({
230230
name: "textTransform",
231231
cssName: "text-transform",
232-
defaultValue: "none",
233232
valueConverter: TextTransform.parse
234233
});
235234
textTransformProperty.register(Style);

tns-core-modules/ui/text-base/text-base.android.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,16 +300,15 @@ function getCapitalizedString(str: string): string {
300300

301301
export function getTransformedText(text: string, textTransform: TextTransform): string {
302302
switch (textTransform) {
303-
case "none":
304-
return text;
305303
case "uppercase":
306304
return text.toUpperCase();
307305
case "lowercase":
308306
return text.toLowerCase();
309307
case "capitalize":
310308
return getCapitalizedString(text);
309+
case "none":
311310
default:
312-
throw new Error(`Invalid text transform value: ${textTransform}. Valid values are: 'none', 'capitalize', 'uppercase', 'lowercase'.`);
311+
return text;
313312
}
314313
}
315314

@@ -324,7 +323,7 @@ function createSpannableStringBuilder(formattedString: FormattedString): android
324323
const text = span.text;
325324
const textTransform = (<TextBase>formattedString.parent).textTransform;
326325
let spanText = (text === null || text === undefined) ? '' : text.toString();
327-
if (textTransform !== "none") {
326+
if (textTransform && textTransform !== "none") {
328327
spanText = getTransformedText(spanText, textTransform);
329328
}
330329

tns-core-modules/ui/text-base/text-base.ios.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,16 +282,15 @@ export class TextBase extends TextBaseCommon {
282282

283283
export function getTransformedText(text: string, textTransform: TextTransform): string {
284284
switch (textTransform) {
285-
case "none":
286-
return text;
287285
case "uppercase":
288286
return NSStringFromNSAttributedString(text).uppercaseString;
289287
case "lowercase":
290288
return NSStringFromNSAttributedString(text).lowercaseString;
291289
case "capitalize":
292290
return NSStringFromNSAttributedString(text).capitalizedString;
291+
case "none":
293292
default:
294-
throw new Error(`Invalid text transform value: ${textTransform}. Valid values are: 'none', 'capitalize', 'uppercase', 'lowercase'.`);
293+
return text;
295294
}
296295
}
297296

0 commit comments

Comments
 (0)