Skip to content

Commit be40c74

Browse files
committed
A second pass through 6.0
1 parent 7cbb1d6 commit be40c74

File tree

7 files changed

+72
-22
lines changed

7 files changed

+72
-22
lines changed

images/chapter6/android/1.png

227 KB
Loading

images/chapter6/android/2.gif

-43.3 KB
Binary file not shown.

images/chapter6/ios/1.png

276 KB
Loading

images/chapter6/ios/2.gif

-58.2 KB
Binary file not shown.

images/chapter6/ios/2.png

310 KB
Loading

index.html

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,15 +1667,16 @@ <h2 id="accessing-native-apis">Accessing native APIs</h2>
16671667
<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&#39;t expose through a NativeScript module or plugin?</p>
16681668
<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&#39;s tweak a few of the UI elements in your app using native code.</p>
16691669
<h3 id="accessing-ios-apis">Accessing iOS APIs</h3>
1670-
<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 <a href="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><img src="images/chapter6/android/1.png" alt="Bad contrast on Android">
1672+
<img src="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 <a href="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 <a href="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 <a href="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>
16741675
<h4 class="exercise-start">
1675-
<b>Exercise</b>: ???
1676+
<b>Exercise</b>: Change hint colors on iOS
16761677
</h4>
16771678

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>
16791680
<pre><code class="lang-TypeScript">import {Color} from &quot;color&quot;;
16801681
import {TextField} from &quot;ui/text-field&quot;;
16811682

@@ -1692,7 +1693,8 @@ <h4 class="exercise-start">
16921693
args.view.hint, dictionary);
16931694
}
16941695
</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>&lt;TextField&gt;</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>
16961698
<pre><code class="lang-TypeScript">import {setHintColor} from &quot;../../utils/hint-util&quot;;
16971699
import {TextField} from &quot;ui/text-field&quot;;
16981700
</code></pre>
@@ -1710,14 +1712,36 @@ <h4 class="exercise-start">
17101712
setHintColor({ view: password, color: hintColor });
17111713
}
17121714
</code></pre>
1713-
<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>
17141716
<pre><code class="lang-TypeScript">this.setTextFieldColors();
17151717
</code></pre>
17161718
<div class="exercise-end"></div>
17171719

1718-
<p>Talk about what just happened.</p>
1719-
<p>TODO: Insert gifs</p>
1720-
<p>Transition to Android API discussion.</p>
1720+
<p>After your app refreshes with this change, you should now see a far more readable hint color:</p>
1721+
<p><img src="images/chapter6/ios/2.png" alt="Better contrast on iOS"></p>
1722+
<p>Let’s back up to the contents of the <code>setHintColor()</code> function so we can discuss what’s going on here.</p>
1723+
<pre><code class="lang-TypeScript">var dictionary = new NSDictionary(
1724+
[args.color.ios],
1725+
[NSForegroundColorAttributeName]
1726+
);
1727+
args.view.ios.attributedPlaceholder = NSAttributedString.alloc().initWithStringAttributes(
1728+
args.view.hint, dictionary);
1729+
</code></pre>
1730+
<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 <a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIColor_Class/"><code>UIColor</code></a>, and <code>args.view.ios</code> resolves to a <a href="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 <a href="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>
1737+
<li><a href="https://raw.githubusercontent.com/NativeScript/NativeScript/master/ios.d.ts">iOS TypeScript declaration file</a></li>
1738+
<li><a href="https://raw.githubusercontent.com/NativeScript/NativeScript/master/android17.d.ts">Android TypeScript declaration file</a></li>
1739+
</ul>
1740+
</li>
1741+
<li>For detailed information on how NativeScript makes native APIs globally available, read about <a href="http://developer.telerik.com/featured/nativescript-works/">“How NativeScript Works”</a> on our blog, and <a href="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>
17211745
<h3 id="accessing-android-apis">Accessing Android APIs</h3>
17221746
<p>Talk about Googling the same sort of thing for Android APIs (setting the hint color).</p>
17231747
<h4 class="exercise-start">

src/chapters/chapter6.md

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@ NativeScript gives you the option to dig into native code as needed, and to do s
66

77
### Accessing iOS APIs
88

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).
1010

11-
Show images.
11+
![Bad contrast on Android](images/chapter6/android/1.png)
12+
![Bad contrast on iOS](images/chapter6/ios/1.png)
1213

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.
1415

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.
1617

1718
<h4 class="exercise-start">
18-
<b>Exercise</b>: ???
19+
<b>Exercise</b>: Change hint colors on iOS
1920
</h4>
2021

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:
2223

2324
``` TypeScript
2425
import {Color} from "color";
@@ -38,7 +39,9 @@ export function setHintColor(args: { view: TextField, color: Color }) {
3839
}
3940
```
4041

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:
4245

4346
``` TypeScript
4447
import {setHintColor} from "../../utils/hint-util";
@@ -62,19 +65,42 @@ setTextFieldColors() {
6265
}
6366
```
6467

65-
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:
6669

6770
``` TypeScript
6871
this.setTextFieldColors();
6972
```
7073

7174
<div class="exercise-end"></div>
7275

73-
Talk about what just happened.
76+
After your app refreshes with this change, you should now see a far more readable hint color:
7477

75-
TODO: Insert gifs
78+
![Better contrast on iOS](images/chapter6/ios/2.png)
79+
80+
Let’s back up to the contents of the `setHintColor()` function so we can discuss what’s going on here.
81+
82+
``` TypeScript
83+
var dictionary = new NSDictionary(
84+
[args.color.ios],
85+
[NSForegroundColorAttributeName]
86+
);
87+
args.view.ios.attributedPlaceholder = NSAttributedString.alloc().initWithStringAttributes(
88+
args.view.hint, dictionary);
89+
```
90+
91+
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.
99+
> * [iOS TypeScript declaration file](https://raw.githubusercontent.com/NativeScript/NativeScript/master/ios.d.ts)
100+
> * [Android TypeScript declaration file](https://raw.githubusercontent.com/NativeScript/NativeScript/master/android17.d.ts)
101+
> * 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.
76102
77-
Transition to Android API discussion.
103+
Let’s move onto how to accomplish this same hint color task on Android.
78104

79105
### Accessing Android APIs
80106

0 commit comments

Comments
 (0)