title | description | position | slug | previous_url | environment |
---|---|---|---|---|---|
Using formatted string |
Learn how to use the FormattedString class in an Angular app |
2 |
formatted-string |
/formatted-string |
angular |
NativeScript has a special class called FormattedString to support various text transformations and decorations. The FormattedString
class can be used with all text-related components like Label, TextView, TextField and even Button. Using FormattedString
within an NativeScript-Angular app requires using a special syntax because of how Angular views get added to the native visual tree. Here’s what the correct syntax looks like:
<Label>
<FormattedString>
<Span text="some content" fontWeight="Bold"></Span>
</FormattedString>
</Label>
Binding (Two-way)
<Label>
<FormattedString>
<Span [text]="title" fontWeight="Bold" textDecoration="underline"></Span>
</FormattedString>
</Label>
import { Component } from '@angular/core';
@Component({
selector: 'ns-main',
moduleId: module.id,
templateUrl: './main.component.html',
})
export class MainComponent {
public title: string;
constructor() {
this.title = "Hello Word";
}
}
This syntax differs from the full syntax of FormattedString used in NativeScript Core, shown below, which does not work in Angular apps:
<Label>
<Label.formattedText>
<FormattedString>
<FormattedString.spans>
<Span text="some " fontWeight="Bold"></Span>
<Span text="content"></Span>
</FormattedString.spans>
</FormattedString>
</Label.formattedText>
</Label>