|
1 | 1 | # NativeScript Label widget
|
| 2 | + |
2 | 3 | [](https://www.npmjs.com/package/nativescript-htmllabel)
|
3 | 4 | [](https://www.npmjs.com/package/nativescript-htmllabel)
|
4 | 5 | [](https://www.npmjs.com/package/nativescript-htmllabel)
|
5 | 6 |
|
6 | 7 | A NativeScript Label widget. It is a direct replacement for the {N} Label widget.
|
7 | 8 |
|
8 | 9 | ## Installation
|
| 10 | + |
9 | 11 | Run the following command from the root of your project:
|
10 | 12 |
|
11 |
| -`tns plugin add nativescript-htmllabel` |
| 13 | +`tns plugin add @nativescript-community/ui-label` |
12 | 14 |
|
13 | 15 | This command automatically installs the necessary files, as well as stores nativescript-htmllabel as a dependency in your project's package.json file.
|
14 | 16 |
|
15 | 17 | ## Configuration
|
16 |
| -It works exactly the same way as the {N} plugin. However it adds a few improvements |
17 | 18 |
|
| 19 | +It works exactly the same way as the {N} plugin. However it adds a few improvements |
18 | 20 |
|
19 | 21 | ## iOS Performances
|
| 22 | + |
20 | 23 | On iOS generating html string can be slow using the system way.
|
21 | 24 | You can enable `DTCoreText` to make it faster.
|
22 | 25 |
|
23 |
| -* add pod `DTCoreText` in your app Podfile at `App_Resources/ios` |
| 26 | +- add pod `DTCoreText` in your app Podfile at `App_Resources/ios` |
| 27 | + |
24 | 28 | ```
|
25 | 29 | pod 'DTCoreText'
|
26 | 30 | ```
|
27 |
| -* enable it in your `app.(js|ts)` (as soon as possible) |
| 31 | + |
| 32 | +- enable it in your `app.(js|ts)` (as soon as possible) |
| 33 | + |
28 | 34 | ```javascript
|
29 | 35 | require('nativescript-htmlabel').enableIOSDTCoreText();
|
30 | 36 | ```
|
31 | 37 |
|
| 38 | +### Angular |
| 39 | + |
| 40 | +```typescript |
| 41 | +import { registerElement } from '@nativescript/angular'; |
| 42 | + |
| 43 | +registerElement('HTMLLabel', () => require('@nativescript-community/ui-label').Label); |
| 44 | +``` |
| 45 | + |
| 46 | +```html |
| 47 | +<!-- Normal Label Usage --> |
| 48 | +<HTMLLabel html="<b>Hello, I am BOLD!></b>" fontFamily="OpenSans" fontSize="16" margin="2 5 5 5" textWrap="true"></HTMLLabel> |
| 49 | + |
| 50 | +<!-- Clickable Link Usage --> |
| 51 | +<HTMLLabel |
| 52 | + [html]="someBindedUrl" |
| 53 | + linkColor="#336699" |
| 54 | + linkUnderline="true" |
| 55 | + (linkTap)="onLinkTap($event)" |
| 56 | + fontFamily="OpenSans" |
| 57 | + fontSize="16" |
| 58 | + margin="2 5 5 5" |
| 59 | + textWrap="true" |
| 60 | +></HTMLLabel> |
| 61 | +``` |
| 62 | + |
| 63 | +```typescript |
| 64 | +import { Utils } from '@nativescript/core'; |
| 65 | + |
| 66 | +export someComponentClass() { |
| 67 | + someBindedUrl = '<a href=\"https://youtube.com\">Open Youtube.com</a>' |
| 68 | + |
| 69 | + // Event binded to the linkTap function on the HTMLLabel |
| 70 | + onLinkTap(args) { |
| 71 | + const link = args.link; |
| 72 | + // expected to be https://youtube.com from the binded `<a>` tag href value |
| 73 | + // be sure to encodeURIComponent of any query string parameters if needed. |
| 74 | + Utils.openUrl(link); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +``` |
| 80 | + |
| 81 | +### Vue |
| 82 | + |
| 83 | +```typescript |
| 84 | +import Vue from 'nativescript-vue'; |
| 85 | + |
| 86 | +Vue.registerElement('HTMLLabel', () => require('@nativescript-community/ui-label').Label); |
| 87 | +``` |
| 88 | + |
| 89 | +```html |
| 90 | +<!-- Normal Label Usage --> |
| 91 | +<HTMLLabel |
| 92 | + fontSize="50" |
| 93 | + fontFamily="Cabin Sketch,res/cabinsketch" |
| 94 | + width="100%" |
| 95 | + paddingTop="5" |
| 96 | + color="#336699" |
| 97 | + textWrap="true" |
| 98 | + :html="someBindedValue" |
| 99 | + verticalAlignment="top" |
| 100 | +/> |
| 101 | + |
| 102 | +<!-- Clickable Link Usage --> |
| 103 | +<HTMLLabel |
| 104 | + html="<a href='https://youtube.com'>Open Youtube.com</a>" |
| 105 | + linkColor="pink" |
| 106 | + linkUnderline="false" |
| 107 | + @linkTap="onLinkTap($event)" |
| 108 | + fontFamily="OpenSans" |
| 109 | + fontSize="16" |
| 110 | + margin="2 5 5 5" |
| 111 | + textWrap="true" |
| 112 | +></HTMLLabel> |
| 113 | +``` |
| 114 | + |
| 115 | +```typescript |
| 116 | +<script lang="ts"> |
| 117 | +import Vue from 'vue'; |
| 118 | + |
| 119 | +export default Vue.extend({ |
| 120 | + data() { |
| 121 | + return { |
| 122 | + someBindedValue: "<p>This is really powerful. <b>Amazing to be quite honest</b></p>", |
| 123 | + }; |
| 124 | + }, |
| 125 | + methods: { |
| 126 | + // event binded to the linkTap on the HTMLLabel |
| 127 | + onLinkTap(args) { |
| 128 | + Utils.openUrl(args.link); |
| 129 | + }, |
| 130 | + }, |
| 131 | + beforeDestroy() {}, |
| 132 | +}); |
| 133 | +</script> |
| 134 | +``` |
| 135 | + |
32 | 136 | ### Properties
|
33 |
| -* **html** |
34 |
| -Html text that will be used to render text. HTML supported tags are a bit different on iOS and Android. To make sure it works as expected, for now only used Android [supported ones](https://stackoverflow.com/questions/9754076/which-html-tags-are-supported-by-android-textview) |
35 | 137 |
|
36 |
| -* **verticalTextAlignment** |
37 |
| -You can also set it through css with `vertical-text-alignment` |
| 138 | +- **html** |
| 139 | + Html text that will be used to render text. HTML supported tags are a bit different on iOS and Android. To make sure it works as expected, for now only used Android [supported ones](https://stackoverflow.com/questions/9754076/which-html-tags-are-supported-by-android-textview). |
| 140 | + |
| 141 | + If using a `url` with parameters, be sure to encode the query string so Android can open the link accordingly. |
| 142 | + |
| 143 | +- **verticalTextAlignment** |
| 144 | + You can also set it through css with `vertical-text-alignment` |
| 145 | + |
| 146 | +- **textShadow** |
| 147 | + You can also set it through css with `text-shadow`. The format is `offsetx offsety blurradius color` |
| 148 | + |
| 149 | +- **linkColor** |
| 150 | + Color for any `<a>` tags inside the `HTMLLabel`. |
38 | 151 |
|
39 |
| -* **textShadow** |
40 |
| -You can also set it through css with `text-shadow`. The format is `offsetx offsety blurradius color` |
| 152 | +- **linkUnderline** |
| 153 | + Boolean to enable underline for any `<a>` tags inside the `HTMLLabel`. |
| 154 | +- **linkTap** |
| 155 | + Event to handle taps of any `<a>` tags inside the `HTMLLabel`. Access the `link` property of the event data from the `linkTap` event. See Samples above for example. |
41 | 156 |
|
42 | 157 | ## Improvements
|
43 | 158 |
|
44 |
| -* Override the {N} font loading system to make it much faster |
45 |
| -* faster creation of `FormattedString` |
46 |
| -* faster label creation and drawing, especially on android |
| 159 | +- Override the {N} font loading system to make it much faster |
| 160 | +- faster creation of `FormattedString` |
| 161 | +- faster label creation and drawing, especially on android |
0 commit comments