You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>The beauty of NativeScript is that you can write a native iOS or Android app in JavaScript, XML, and CSS without touching Swift, Objective-C, or Java, if you choose. But what if you want to present a different, more platform-specific UI to your users? Or if you want to access an iOS or Android API that NativeScript doesn't expose through a NativeScript module or plugin?</p>
1668
1668
<p>NativeScript gives you the option to dig into native code as needed, and to do so without leaving JavaScript. To show how this works in action, let's tweak a few of the UI elements in your app using native code.</p>
<p>Ask the reader to remember that the hint text color in the app doesn’t look good.</p>
1671
-
<p>Show images.</p>
1672
-
<p>Explain how NativeScript doesn’t currently expose a way to style a text field’s hint color (although <ahref="https://github.com/NativeScript/NativeScript/issues/712">there is an open issue requesting the feature</a>).</p>
1673
-
<p>Show how you can Google something like “style UITextField hint text” and find iOS code that accomplishes that task.</p>
1670
+
<p>You may recall from earlier chapters that the hint color on your sign up screen could use a little more contrast. Notice the unappealing black on brown color of the text in the images below (if you can see the text at all).</p>
1671
+
<p><imgsrc="images/chapter6/android/1.png" alt="Bad contrast on Android">
1672
+
<imgsrc="images/chapter6/ios/1.png" alt="Bad contrast on iOS"></p>
1673
+
<p>At the time of this writing, NativeScript doesn’t expose a way to style a text field’s hint color through CSS—although <ahref="https://github.com/NativeScript/NativeScript/issues/712">there is an open issue requesting the feature</a>—however, both iOS and Android have ways to accomplish this task, and with NativeScript you have direct access to these native APIs.</p>
1674
+
<p>Let’s start with iOS. If you run a <ahref="https://www.google.com/#q=style%20ios%20text%20field%20hint%20text">generic search for “style iOS text field hint text”</a>, the first result is a <ahref="http://stackoverflow.com/questions/1340224/iphone-uitextfield-change-placeholder-text-color">Stack Overflow post</a> that recommends setting a <code>UITextField</code>’s <code>attributedPlaceholder</code> property. Let’s look at how to do that.</p>
1674
1675
<h4class="exercise-start">
1675
-
<b>Exercise</b>: ???
1676
+
<b>Exercise</b>: Change hint colors on iOS
1676
1677
</h4>
1677
1678
1678
-
<p>Open <code>app/utils/hint-util.ts</code> and paste in the following code:</p>
1679
+
<p>Because there are multiple text fields in Groceries, we’ll write the functionality to change hint colors as a utility that lives in your app’s <code>utils</code> folder. Open <code>app/utils/hint-util.ts</code> and paste in the following code:</p>
1679
1680
<pre><codeclass="lang-TypeScript">import {Color} from "color";
1680
1681
import {TextField} from "ui/text-field";
1681
1682
@@ -1692,7 +1693,8 @@ <h4 class="exercise-start">
1692
1693
args.view.hint, dictionary);
1693
1694
}
1694
1695
</code></pre>
1695
-
<p>Next, open up <code>app/pages/login/login.component.ts</code> and paste in the following imports at the top of the file:</p>
1696
+
<p>This creates a function called <code>setHintColor()</code> that accepts a <code><TextField></code> and <code>Color</code>. We’ll talk about the contents of this function momentarily; first let’s look at how to use it.</p>
1697
+
<p>Open up <code>app/pages/login/login.component.ts</code> and add the following two imports to the top of the file:</p>
1696
1698
<pre><codeclass="lang-TypeScript">import {setHintColor} from "../../utils/hint-util";
1697
1699
import {TextField} from "ui/text-field";
<p>Finally, add a call to the new <code>setTextFieldColors()</code> in <code>toggleDisplay()</code>, immediately after the existing <code>this.isLoggingIn = !this.isLoggingIn</code> line:</p>
1715
+
<p>Finally, add a call to the new <code>setTextFieldColors()</code> in your <code>LoginPage</code>’s existing <code>toggleDisplay()</code> method—ideally immediately after the existing <code>this.isLoggingIn = !this.isLoggingIn</code> line:</p>
<p>By convention, NativeScript controls make their iOS and Android native implementations available via <code>ios</code> and <code>android</code> properties, respectively. In this code that means that <code>args.color.ios</code> resolves to a <ahref="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIColor_Class/"><code>UIColor</code></a>, and <code>args.view.ios</code> resolves to a <ahref="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextField_Class/"><code>UITextField</code></a>. Once you have a reference to these controls you can set native properties on them directly in TypeScript, which this code does with the <code>UITextField</code>’s <ahref="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextField_Class/#//apple_ref/occ/instp/UITextField/attributedPlaceholder"><code>attributedPlaceholder</code> property</a>.</p>
1731
+
<p>The power with NativeScript is you can perform these customizations in TypeScript—there’s no need to jump into Xcode and write Objective-C or Swift. And this doesn’t apply just to attributes. Notice the global <code>NSDictionary</code>, <code>NSAttributedString</code>, and <code>NSForegroundColorAttributeName</code> attributes. In NativeScript, all iOS and Android APIs are globally available to use—again, directly in TypeScript code.</p>
1732
+
<p>Admittedly, this code can seem a bit arcane if you’ve never written an iOS app before, but the key here is that you’re never limited by the APIs that NativeScript provides out of the box. Most of the time you’ll be able to solve problems using the NativeScript module APIs, but if you hit a scenario your app needs that NativeScript doesn’t provide a module for, you can always hit the native APIs directly.</p>
1733
+
<blockquote>
1734
+
<p><strong>TIP</strong>:</p>
1735
+
<ul>
1736
+
<li>NativeScript provides TypeScript declaration files (<code>.d.ts</code> files) for all iOS and Android APIs. You can download the files using the links below. One word of warning though: because the declaration files include the entirety of the iOS and Android SDKs, they’re quite large, and can slow TypeScript builds to a crawl because of their sheer size. Nevertheless, the files can be invaluable during development, as they make accessing native APIs a whole lot easier.<ul>
<li>For detailed information on how NativeScript makes native APIs globally available, read about <ahref="http://developer.telerik.com/featured/nativescript-works/">“How NativeScript Works”</a> on our blog, and <ahref="http://docs.nativescript.org/core-concepts/accessing-native-apis-with-javascript">“Accessing Native APIs with JavaScript”</a> on our documentation.</li>
1742
+
</ul>
1743
+
</blockquote>
1744
+
<p>Let’s move onto how to accomplish this same hint color task on Android.</p>
Copy file name to clipboardExpand all lines: src/chapters/chapter6.md
+37-11Lines changed: 37 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,19 +6,20 @@ NativeScript gives you the option to dig into native code as needed, and to do s
6
6
7
7
### Accessing iOS APIs
8
8
9
-
Ask the reader to remember that the hint text color in the app doesn’t look good.
9
+
You may recall from earlier chapters that the hint color on your sign up screen could use a little more contrast. Notice the unappealing black on brown color of the text in the images below (if you can see the text at all).
10
10
11
-
Show images.
11
+

12
+

12
13
13
-
Explain how NativeScript doesn’t currently expose a way to style a text field’s hint color (although [there is an open issue requesting the feature](https://github.com/NativeScript/NativeScript/issues/712)).
14
+
At the time of this writing, NativeScript doesn’t expose a way to style a text field’s hint color through CSS—although [there is an open issue requesting the feature](https://github.com/NativeScript/NativeScript/issues/712)—however, both iOS and Android have ways to accomplish this task, and with NativeScript you have direct access to these native APIs.
14
15
15
-
Show how you can Google something like “style UITextField hint text” and find iOS code that accomplishes that task.
16
+
Let’s start with iOS. If you run a [generic search for “style iOS text field hint text”](https://www.google.com/#q=style%20ios%20text%20field%20hint%20text), the first result is a [Stack Overflow post](http://stackoverflow.com/questions/1340224/iphone-uitextfield-change-placeholder-text-color)that recommends setting a `UITextField`’s `attributedPlaceholder` property. Let’s look at how to do that.
16
17
17
18
<h4class="exercise-start">
18
-
<b>Exercise</b>: ???
19
+
<b>Exercise</b>: Change hint colors on iOS
19
20
</h4>
20
21
21
-
Open `app/utils/hint-util.ts` and paste in the following code:
22
+
Because there are multiple text fields in Groceries, we’ll write the functionality to change hint colors as a utility that lives in your app’s `utils` folder. Open `app/utils/hint-util.ts` and paste in the following code:
22
23
23
24
```TypeScript
24
25
import {Color} from"color";
@@ -38,7 +39,9 @@ export function setHintColor(args: { view: TextField, color: Color }) {
38
39
}
39
40
```
40
41
41
-
Next, open up `app/pages/login/login.component.ts` and paste in the following imports at the top of the file:
42
+
This creates a function called `setHintColor()` that accepts a `<TextField>` and `Color`. We’ll talk about the contents of this function momentarily; first let’s look at how to use it.
43
+
44
+
Open up `app/pages/login/login.component.ts` and add the following two imports to the top of the file:
Finally, add a call to the new `setTextFieldColors()` in `toggleDisplay()`, immediately after the existing `this.isLoggingIn = !this.isLoggingIn` line:
68
+
Finally, add a call to the new `setTextFieldColors()` in your `LoginPage`’s existing `toggleDisplay()` method—ideally immediately after the existing `this.isLoggingIn = !this.isLoggingIn` line:
66
69
67
70
```TypeScript
68
71
this.setTextFieldColors();
69
72
```
70
73
71
74
<divclass="exercise-end"></div>
72
75
73
-
Talk about what just happened.
76
+
After your app refreshes with this change, you should now see a far more readable hint color:
74
77
75
-
TODO: Insert gifs
78
+

79
+
80
+
Let’s back up to the contents of the `setHintColor()` function so we can discuss what’s going on here.
By convention, NativeScript controls make their iOS and Android native implementations available via `ios` and `android` properties, respectively. In this code that means that `args.color.ios` resolves to a [`UIColor`](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIColor_Class/), and `args.view.ios` resolves to a [`UITextField`](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextField_Class/). Once you have a reference to these controls you can set native properties on them directly in TypeScript, which this code does with the `UITextField`’s [`attributedPlaceholder` property](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextField_Class/#//apple_ref/occ/instp/UITextField/attributedPlaceholder).
92
+
93
+
The power with NativeScript is you can perform these customizations in TypeScript—there’s no need to jump into Xcode and write Objective-C or Swift. And this doesn’t apply just to attributes. Notice the global `NSDictionary`, `NSAttributedString`, and `NSForegroundColorAttributeName` attributes. In NativeScript, all iOS and Android APIs are globally available to use—again, directly in TypeScript code.
94
+
95
+
Admittedly, this code can seem a bit arcane if you’ve never written an iOS app before, but the key here is that you’re never limited by the APIs that NativeScript provides out of the box. Most of the time you’ll be able to solve problems using the NativeScript module APIs, but if you hit a scenario your app needs that NativeScript doesn’t provide a module for, you can always hit the native APIs directly.
96
+
97
+
> **TIP**:
98
+
> * NativeScript provides TypeScript declaration files (`.d.ts` files) for all iOS and Android APIs. You can download the files using the links below. One word of warning though: because the declaration files include the entirety of the iOS and Android SDKs, they’re quite large, and can slow TypeScript builds to a crawl because of their sheer size. Nevertheless, the files can be invaluable during development, as they make accessing native APIs a whole lot easier.
> * For detailed information on how NativeScript makes native APIs globally available, read about [“How NativeScript Works”](http://developer.telerik.com/featured/nativescript-works/) on our blog, and [“Accessing Native APIs with JavaScript”](http://docs.nativescript.org/core-concepts/accessing-native-apis-with-javascript) on our documentation.
76
102
77
-
Transition to Android API discussion.
103
+
Let’s move onto how to accomplish this same hint color task on Android.
0 commit comments