question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Android HtmlView Does not accept size or font face styling

See original GitHub issue

Html view in android does not accept styling or font-family. IOS this works.

Here is a sample code that does not style correctly <HtmlView html="<span><b><font size='10' color='blue' face='lato'>{{item.text}} </font></b></span>" </HtmlView>

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
rahadurcommented, Mar 23, 2019

This is how i create a HtmlView custom component.

Angular Component 💥💥💥


import { Component, OnInit, AfterViewInit, Input, ViewChild, ElementRef } from "@angular/core";
import { Color } from 'tns-core-modules/color';

@Component({
	selector: "HtmlViewer",
	moduleId: module.id,
	template: `
		<HtmlView #htmlView [html]="htmlString" (loaded)="onLoaded()"></HtmlView> 
	`,
	styleUrls: ['./html-viewer.component.css']
})
export class HtmlViewerComponent implements OnInit, AfterViewInit {

	@Input('html') html: string;

	@Input('textSize') textSize: number

	@Input('textColor') textColor: string;

	@Input('textLinkColor') textLinkColor: string;

	@ViewChild('htmlView') htmlView: ElementRef;

	htmlString: string;


	constructor() {
		this.htmlString = '';
	}

	ngOnInit(): void {
		this.htmlString = this.html ? this.html : '';
	}


	ngAfterViewInit() {
	}


	onLoaded() {
		let htmlView = this.htmlView.nativeElement.android;

		htmlView.setTextSize(this.textSize);
		htmlView.setTextColor(this.textColor ? new Color(this.textColor).android : new Color('#999999').android);
		htmlView.setLinkTextColor(this.textLinkColor ? new Color(this.textLinkColor).android : new Color('#0000FF').android);
	}
}
4reactions
corne-de-bruincommented, Apr 26, 2018

Hi @captainhaddock29

HTML:

<!-- Styling the content through external css is not possible -->
<HTMLView
        [html]="htmlString"
        #htmlView>
</HTMLView>

This is our Angular component:

export class HtmlViewComponent implements OnChanges {
    @Input()
    public html: string = '';
    @ViewChild('htmlView')
    public htmlView: ElementRef;
    public htmlString: string = '';

    private textColor: string = HEX.textColor;
    private linkColor: string = HEX.linkColor;

    private fontSize: string = '[insert font size]';
    private fontFamily: string = '[insert font family]';
    private fontLocation: string = '[insert font directory example: app/fonts]';
    private font: string = '[insert font file name]';

    public ngOnChanges(): void {
        try {
            this.setHTMLView();
        } catch (exception) {
            console.error(exception);
        }
    }

    protected setHTMLView(): void {
        if (isIOS) {
            this.htmlString = this.template;
            wait(() => this.htmlView.nativeElement.ios, () => {
                const htmlView = this.htmlView.nativeElement.ios;
                const linkTextAttributes: any = NSDictionary.dictionaryWithObjectForKey(
                    new Color(this.linkColor).ios, NSForegroundColorAttributeName
                );
                htmlView.linkTextAttributes = linkTextAttributes;
                htmlView.dataDetectorTypes = UIDataDetectorTypes.None;
            });
        } else if (isAndroid) {
            wait(() => this.htmlView.nativeElement.android, () => {
                const htmlView = this.htmlView.nativeElement.android;
                const LINK_MASK_NONE = 0;
                const parsedHtmlString = android.text.Html.fromHtml(this.html);
                const textColor = android.graphics.Color.parseColor(this.textColor);
                const linkColor = android.graphics.Color.parseColor(this.linkColor);
                const assets = androidApp.context.getAssets();
                const typeface = android.graphics.Typeface.createFromAsset(assets, this.fontLocation + fs.path.separator + this.font);
                htmlView.setAutoLinkMask(LINK_MASK_NONE);
                htmlView.setText(parsedHtmlString);
                htmlView.setTextColor(textColor);
                htmlView.setLinkTextColor(linkColor);
                htmlView.setTypeface(typeface);
            });
        }
    }

    private get template(): string {
        let html = '';

        if (!isNullOrUndefined(this.html)) {
            html = this.html;
        }

        return `
            <style type="text/css">
                body {font-family: "${this.fontFamily}"; font-size: ${this.fontSize};}
                a {text-decoration:none;}
             </style>
             <span>
                 <font color="${this.textColor}">
                     ${html}
                 </font>
            </span>`;
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Android HtmlView Does not accept size or font face styling
Html view in android does not accept styling or font-family. IOS this works. Here is a sample code that does not style correctly...
Read more >
How to change font face of Webview in Android?
There's a working example of this in this project. It boils down to: In your assets/fonts folder, place the desired OTF or TTF...
Read more >
How to change font face of Webview in Android? - Tutorialspoint
This example demonstrates how do I change font face of WebView in android. Step 1 − Create a new project in Android Studio,...
Read more >
Add a font as an XML resource - Android Developers
For more information on using the Support Library, refer to the Use the ... A font family is a set of font files...
Read more >
font-stretch - CSS: Cascading Style Sheets - MDN Web Docs
If the font you are using does not offer condensed or expanded faces, this property has no effect. Font face selection. The face...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found