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
Copy file name to clipboardExpand all lines: index.html
+45-7Lines changed: 45 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -428,19 +428,57 @@ <h4 class="exercise-start">
428
428
border-color: black;
429
429
}
430
430
</code></pre>
431
+
<blockquote>
432
+
<p><strong>NOTE</strong>: We’ll leave the <code>platform.android.css</code> file empty as we have no Android-specific changes to make yet.</p>
433
+
</blockquote>
431
434
<divclass="exercise-end"></div>
432
435
433
436
<p>NativeScript supports CSS's <code>@import</code> statement for importing one CSS file into another. So this new line of code imports the CSS rules from <code>platform.css</code> into <code>app.css</code>. But, you might have noticed that Groceries does not have a file named <code>platform.css</code>—only <code>app/platform.android.css</code> and <code>app/platform.ios.css</code> exist. What's going on here?</p>
434
437
<p><aid="platform-specific-files"></a>When you execute <code>tns run</code>, or <code>tns livesync</code>, the NativeScript CLI takes your code from the <code>app</code> folder and places it in the native projects located in the <code>platforms/ios</code> and <code>platforms/android</code> folders. Here the naming convention comes in: while moving files, the CLI intelligently selects <code>.android.*</code> and <code>.ios.*</code> files. To give a specific example, the CLI moves <code>platform.ios.css</code> into <code>platforms/ios</code> and renames it to <code>platform.css</code>; similarly, the CLI moves <code>platform.android.css</code> into <code>platforms/android</code>, and again renames it to <code>platform.css</code>. This convention provides a convenient way to branch your code to handle iOS and Android separately, and it's supported for any type of file in NativeScript—not just CSS files. You'll see a few more examples of this convention later in this guide.</p>
435
-
<blockquote>
436
-
<p><strong>Note</strong>: The <code>platform.android.css</code> file is currently empty as we have no Android-specific changes to make yet. We’ll make some Android-specific tweaks later in the guide.</p>
437
-
</blockquote>
438
-
<p>With these changes in place, you'll notice that the app has a bit more spacing, and also has a distinctly different look on iOS and Android:</p>
438
+
<p>With these changes in place, you'll notice that the app has a bit more spacing, and also that the text fields have borders on iOS but that Android:</p>
<p>Despite our changes, the app still doesn’t look great, and that’s because we’re going to apply another batch of styles at the component level.</p>
441
+
<p>Despite our changes the app still looks pretty ugly, and that’s because we’re going to apply another batch of styles at the component level. Let’s look at how that works.</p>
<p>Much like on the web, sometimes in your NativeScript apps you want to write CSS rules that apply to your entire application, and sometimes you want to write CSS rules that apply to a specific portion of the interface. In the previous section you saw how to use NativeScript’s <code>app.css</code> file to write global rules, and in this section you’ll learn how to use a component’s <code>styleUrls</code> property to apply rules that are scoped to individual components.</p>
444
+
<h4class="exercise-start">
445
+
<b>Exercise</b>: Add component-specific CSS
446
+
</h4>
447
+
448
+
<p>Open your app’s <code>app/app.component.ts</code> file and add a <code>styleUrls</code> property such that that full <code>@Component</code> declaration now looks like this:</p>
<p>In Angular 2, the <code>stylesUrl</code> points at an array of stylesheets that should be used to style a component. In this case, you’re telling Angular to use two stylesheets, <code>login-common.css</code>, and <code>login.css</code>.</p>
467
+
<p>Why two files? Much like you divided your global files into <code>app.css</code>, <code>platform.ios.css</code>, and <code>platform.android.css</code>, this structure gives you a similar ability to separate common login styling in <code>login-common.css</code>, iOS-specific login styling <code>login.ios.css</code>, and Android-specific login styling in <code>login.android.css</code>. Before we see what your app looks like now, there’s one small change you need to make. If you open <code>app/pages/login/login-common.css</code> you’ll see the final selector used is <code>#submit-button</code>. Much like using CSS on the web, in NativeScript you can both <code>id</code> and <code>class</code> attributes to target specific user interface components.</p>
468
+
<p>Let’s see how it works by adding an <code>id</code> to your app’s “Sign In” button.</p>
469
+
<h4class="exercise-start">
470
+
<b>Exercise</b>: Add an <code>id</code> attribute
471
+
</h4>
472
+
473
+
<p>Open your app’s <code>app/app.component.ts</code> file, find <code><Button text="Sign in"></Button></code> in your component’s <code>template</code>, and replace it with the code below:</p>
<p>As you can see, in NativeScript you have a lot of options for how you can apply CSS rules. You can apply the rules globally for both platforms, for iOS specifically, or for Android specifically. And you can also apply rules at the component level. With this last <code>id</code> change in place your app is starting to look a little nicer:</p>
<p>To continue polishing the visuals of this login screen, let’s look at how we can add an image of this app’s logo.</p>
444
482
<h3id="images">Images</h3>
445
483
<p>In NativeScript you use the <code><Image></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>
<h3id="structuring-your-app">Structuring your app</h3>
548
586
<p>There are many reasons to segment your application into modular units, and you can <ahref="https://en.wikipedia.org/wiki/Modular_programming">read about the various benefits on Wikipedia</a>. However, keeping NativeScript apps modular has one unique benefit: the ability to share the code you write between Angular-2-built web apps, and Angular-2-build native apps.</p>
549
587
<p>Even if you have no plans to create an Angular 2 web app, separating out your code is still advantageous for a number of other reasons—testability, ease of maintenance, and so forth—but if you <em>do</em> have plans to build an Angular 2 web app, having a chunk of functionality that you can reuse for your native and web apps can be an invaluable time saver.</p>
550
-
<p>To see how this works in action let’s create a few files.</p>
588
+
<p>To see how this works in action let’s edit some files in the <code>/shared</code> folder and set them up to be imported.</p>
Copy file name to clipboardExpand all lines: src/chapters/chapter2.md
+55-5Lines changed: 55 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -270,24 +270,74 @@ TextField {
270
270
}
271
271
```
272
272
273
+
> **NOTE**: We’ll leave the `platform.android.css` file empty as we have no Android-specific changes to make yet.
274
+
273
275
<divclass="exercise-end"></div>
274
276
275
277
NativeScript supports CSS's `@import` statement for importing one CSS file into another. So this new line of code imports the CSS rules from `platform.css` into `app.css`. But, you might have noticed that Groceries does not have a file named `platform.css`—only `app/platform.android.css` and `app/platform.ios.css` exist. What's going on here?
276
278
277
279
<aid="platform-specific-files"></a>When you execute `tns run`, or `tns livesync`, the NativeScript CLI takes your code from the `app` folder and places it in the native projects located in the `platforms/ios` and `platforms/android` folders. Here the naming convention comes in: while moving files, the CLI intelligently selects `.android.*` and `.ios.*` files. To give a specific example, the CLI moves `platform.ios.css` into `platforms/ios` and renames it to `platform.css`; similarly, the CLI moves `platform.android.css` into `platforms/android`, and again renames it to `platform.css`. This convention provides a convenient way to branch your code to handle iOS and Android separately, and it's supported for any type of file in NativeScript—not just CSS files. You'll see a few more examples of this convention later in this guide.
278
280
279
-
> **Note**: The `platform.android.css` file is currently empty as we have no Android-specific changes to make yet. We’ll make some Android-specific tweaks later in the guide.
280
-
281
-
With these changes in place, you'll notice that the app has a bit more spacing, and also has a distinctly different look on iOS and Android:
281
+
With these changes in place, you'll notice that the app has a bit more spacing, and also that the text fields have borders on iOS but that Android:
282
282
283
283

284
284

285
285
286
-
Despite our changes, the app still doesn’t look great, and that’s because we’re going to apply another batch of styles at the component level.
286
+
Despite our changes the app still looks pretty ugly, and that’s because we’re going to apply another batch of styles at the component level. Let’s look at how that works.
287
287
288
288
### Component-specific CSS
289
289
290
-
TODO: Write this!
290
+
Much like on the web, sometimes in your NativeScript apps you want to write CSS rules that apply to your entire application, and sometimes you want to write CSS rules that apply to a specific portion of the interface. In the previous section you saw how to use NativeScript’s `app.css` file to write global rules, and in this section you’ll learn how to use a component’s `styleUrls` property to apply rules that are scoped to individual components.
291
+
292
+
<h4class="exercise-start">
293
+
<b>Exercise</b>: Add component-specific CSS
294
+
</h4>
295
+
296
+
Open your app’s `app/app.component.ts` file and add a `styleUrls` property such that that full `@Component` declaration now looks like this:
In Angular 2, the `stylesUrl` points at an array of stylesheets that should be used to style a component. In this case, you’re telling Angular to use two stylesheets, `login-common.css`, and `login.css`.
318
+
319
+
Why two files? Much like you divided your global files into `app.css`, `platform.ios.css`, and `platform.android.css`, this structure gives you a similar ability to separate common login styling in `login-common.css`, iOS-specific login styling `login.ios.css`, and Android-specific login styling in `login.android.css`. Before we see what your app looks like now, there’s one small change you need to make. If you open `app/pages/login/login-common.css` you’ll see the final selector used is `#submit-button`. Much like using CSS on the web, in NativeScript you can both `id` and `class` attributes to target specific user interface components.
320
+
321
+
Let’s see how it works by adding an `id` to your app’s “Sign In” button.
322
+
323
+
<h4class="exercise-start">
324
+
<b>Exercise</b>: Add an `id` attribute
325
+
</h4>
326
+
327
+
Open your app’s `app/app.component.ts` file, find `<Button text="Sign in"></Button>` in your component’s `template`, and replace it with the code below:
328
+
329
+
```XML
330
+
<Buttontext="Sign in"id="submit-button"></Button>
331
+
```
332
+
333
+
<divclass="exercise-end"></div>
334
+
335
+
As you can see, in NativeScript you have a lot of options for how you can apply CSS rules. You can apply the rules globally for both platforms, for iOS specifically, or for Android specifically. And you can also apply rules at the component level. With this last `id` change in place your app is starting to look a little nicer:
336
+
337
+

338
+

339
+
340
+
To continue polishing the visuals of this login screen, let’s look at how we can add an image of this app’s logo.
0 commit comments