Skip to content

Commit 4ead43f

Browse files
committed
Finishing up an early draft of chapter 2
1 parent bf26284 commit 4ead43f

File tree

4 files changed

+62
-6
lines changed

4 files changed

+62
-6
lines changed

images/chapter2/android/3.png

-21.2 KB
Loading

images/chapter2/android/4.png

-20.3 KB
Loading

index.html

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,11 +440,32 @@ <h4 class="exercise-start">
440440
<p><strong>TIP</strong>: NativeScript also supports selecting elements by the <code>id</code> attribute. Refer to the docs for <a href="/styling#supported-selectors">a full list of the supported selectors</a>.</p>
441441
</blockquote>
442442
<p>With these changes in place, you&#39;ll notice that the app looks halfway decent now, and also has a distinctly different look on iOS and Android:</p>
443-
<p><img src="http://docs.nativescript.org/img/cli-getting-started/chapter2/ios/3.png" alt="login 1">
444-
<img src="http://docs.nativescript.org/img/cli-getting-started/chapter2/android/3.png" alt="login 1"></p>
443+
<p><img src="images/chapter2/ios/3.png" alt="login 1">
444+
<img src="images/chapter2/android/3.png" alt="login 1"></p>
445445
<p>Feel free to take some time to play with the look of this app before moving on. When you&#39;re ready, let&#39;s move on and add an image to this login screen.</p>
446446
<h3 id="images">Images</h3>
447-
<p>TODO: Write this!</p>
447+
<p>In NativeScript you use the <code>&lt;Image&gt;</code> UI element and its <code>src</code> attribute to add images to your pages. The <code>src</code> attribute lets you specify your image in three ways. The first (and simplest) way is to point at the URL of an image:</p>
448+
<pre><code class="lang-XML">&lt;Image src=&quot;https://www.nativescript.org/images/default-source/landingpages/logo.png&quot;&gt;&lt;/Image&gt;
449+
</code></pre>
450+
<p>The second way is to point at an image that lives within your app&#39;s <code>app</code> folder. For example if you have an image at <code>app/images/logo.png</code>, you can use it with:</p>
451+
<pre><code class="lang-XML">&lt;Image src=&quot;~/images/logo.png&quot;&gt;&lt;/Image&gt;
452+
</code></pre>
453+
<p>The third way, and the one Groceries uses, is to use platform-specific image resources. Let&#39;s add an image to the login screen and then discuss exactly what&#39;s happening.</p>
454+
<h4 class="exercise-start">
455+
<b>Exercise</b>: Add a logo
456+
</h4>
457+
458+
<p>In <code>login.xml</code>, add the <code>&lt;Image&gt;</code> below as the first child of the existing <code>&lt;StackLayout&gt;</code> tag:</p>
459+
<pre><code class="lang-XML">&lt;Image src=&quot;res://logo&quot; stretch=&quot;none&quot; horizontalAlignment=&quot;center&quot;&gt;&lt;/Image&gt;
460+
</code></pre>
461+
<div class="exercise-end"></div>
462+
463+
<p>The <code>res://</code> syntax tells NativeScript to use a platform-specific resource, in this case an image. Platform-specific resources go in your app&#39;s <code>app/App_Resources</code> folder. If you look there you&#39;ll find a few different image files, several of which are named <code>logo.png</code>.</p>
464+
<p>Although more complex than putting an image directly in the <code>app</code> folder, using platform-specific images gives you more control over image display on different device dimensions. For example iOS lets you provide three different image files for devices with different pixel densities. As such you&#39;ll find logos named <code>logo.png</code>, <code>logo@2x.png</code>, and <code>logo@3x.png</code> in your <code>App_Resources/iOS</code> folder. For Android you&#39;ll find similar image files in <code>App_Resources/Android/drawable-hdpi</code> (for &quot;high&quot; dpi, or high dots-per-inch), <code>App_Resources/Android/drawable-mdpi</code> (for medium-dpi), and <code>App_Resources/Android/drawable-ldpi</code> (for low-dpi).</p>
465+
<p>Once these files are in place the NativeScript framework knows how to pick the correct file; all you have to do is reference the image using <code>res://</code> and its base file name—i.e. <code>res://logo</code>. Here&#39;s what your login screen should look like on iOS and Android:</p>
466+
<p><img src="images/chapter2/ios/4.png" alt="login 4">
467+
<img src="images/chapter2/android/4.png" alt="login 4"></p>
468+
<p>At this point your UI looks good, but the app still doesn&#39;t actually do anything. Let&#39;s look at how you can use JavaScript to add some functionality.</p>
448469

449470
</div>
450471
</div>

src/chapters/chapter2.md

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,46 @@ NativeScript uses the `class` attribute for adding CSS class names to UI compone
300300
301301
With these changes in place, you'll notice that the app looks halfway decent now, and also has a distinctly different look on iOS and Android:
302302

303-
![login 1](http://docs.nativescript.org/img/cli-getting-started/chapter2/ios/3.png)
304-
![login 1](http://docs.nativescript.org/img/cli-getting-started/chapter2/android/3.png)
303+
![login 1](images/chapter2/ios/3.png)
304+
![login 1](images/chapter2/android/3.png)
305305

306306
Feel free to take some time to play with the look of this app before moving on. When you're ready, let's move on and add an image to this login screen.
307307

308308
### Images
309309

310-
TODO: Write this!
310+
In NativeScript you use the `<Image>` UI element and its `src` attribute to add images to your pages. The `src` attribute lets you specify your image in three ways. The first (and simplest) way is to point at the URL of an image:
311+
312+
``` XML
313+
<Image src="https://www.nativescript.org/images/default-source/landingpages/logo.png"></Image>
314+
```
315+
316+
The second way is to point at an image that lives within your app's `app` folder. For example if you have an image at `app/images/logo.png`, you can use it with:
317+
318+
``` XML
319+
<Image src="~/images/logo.png"></Image>
320+
```
321+
322+
The third way, and the one Groceries uses, is to use platform-specific image resources. Let's add an image to the login screen and then discuss exactly what's happening.
323+
324+
<h4 class="exercise-start">
325+
<b>Exercise</b>: Add a logo
326+
</h4>
327+
328+
In `login.xml`, add the `<Image>` below as the first child of the existing `<StackLayout>` tag:
329+
330+
``` XML
331+
<Image src="res://logo" stretch="none" horizontalAlignment="center"></Image>
332+
```
333+
334+
<div class="exercise-end"></div>
335+
336+
The `res://` syntax tells NativeScript to use a platform-specific resource, in this case an image. Platform-specific resources go in your app's `app/App_Resources` folder. If you look there you'll find a few different image files, several of which are named `logo.png`.
337+
338+
Although more complex than putting an image directly in the `app` folder, using platform-specific images gives you more control over image display on different device dimensions. For example iOS lets you provide three different image files for devices with different pixel densities. As such you'll find logos named `logo.png`, `logo@2x.png`, and `logo@3x.png` in your `App_Resources/iOS` folder. For Android you'll find similar image files in `App_Resources/Android/drawable-hdpi` (for "high" dpi, or high dots-per-inch), `App_Resources/Android/drawable-mdpi` (for medium-dpi), and `App_Resources/Android/drawable-ldpi` (for low-dpi).
339+
340+
Once these files are in place the NativeScript framework knows how to pick the correct file; all you have to do is reference the image using `res://` and its base file name—i.e. `res://logo`. Here's what your login screen should look like on iOS and Android:
341+
342+
![login 4](images/chapter2/ios/4.png)
343+
![login 4](images/chapter2/android/4.png)
344+
345+
At this point your UI looks good, but the app still doesn't actually do anything. Let's look at how you can use JavaScript to add some functionality.

0 commit comments

Comments
 (0)