Skip to content

fix(ios): Apply text color to HTMLView content #10708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions packages/core/ui/html-view/html-view-common.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { CssProperty } from '../core/properties';
import { View, CSSType } from '../core/view';
import { View, CSSType } from '../core/view';
import { booleanConverter } from '../core/view-base';
import { Property } from '../core/properties';
import { Style } from '../styling/style';
import { Color } from '../../color';
import { HtmlView as HtmlViewDefinition } from '.';

Expand Down Expand Up @@ -30,10 +28,9 @@ export const selectableProperty = new Property<HtmlViewBase, boolean>({
});
selectableProperty.register(HtmlViewBase);

export const linkColorProperty = new CssProperty<Style, Color>({
export const linkColorProperty = new Property<HtmlViewBase, Color>({
name: 'linkColor',
cssName: 'link-color',
equalityComparer: Color.equals,
valueConverter: (value) => new Color(value),
});
linkColorProperty.register(Style);
linkColorProperty.register(HtmlViewBase);
3 changes: 3 additions & 0 deletions packages/core/ui/html-view/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { View } from '../core/view';
import { Property } from '../core/properties';
import { Color } from '../../color';

/**
* Represents a view with html content. Use this component instead WebView when you want to show just static HTML content.
Expand Down Expand Up @@ -39,3 +40,5 @@ export class HtmlView extends View {
}

export const htmlProperty: Property<HtmlView, string>;
export const selectableProperty: Property<HtmlView, boolean>;
export const linkColorProperty: Property<HtmlView, Color>;
65 changes: 36 additions & 29 deletions packages/core/ui/html-view/index.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@ const majorVersion = iOSNativeHelper.MajorVersion;

export class HtmlView extends HtmlViewBase {
nativeViewProtected: UITextView;
private currentHtml: string;

public createNativeView() {
const view = UITextView.new();
view.scrollEnabled = false;
view.editable = false;
view.selectable = true;
view.userInteractionEnabled = true;
view.dataDetectorTypes = UIDataDetectorTypes.All;

return view;
const nativeView = UITextView.new();
nativeView.scrollEnabled = false;
nativeView.editable = false;
nativeView.selectable = true;
nativeView.userInteractionEnabled = true;
nativeView.dataDetectorTypes = UIDataDetectorTypes.All;

return nativeView;
}

public initNativeView(): void {
Expand Down Expand Up @@ -60,24 +59,37 @@ export class HtmlView extends HtmlViewBase {
}

private renderWithStyles() {
let html = this.currentHtml;
const styles = [];
if (this.nativeViewProtected.font) {
styles.push(`font-family: '${this.nativeViewProtected.font.fontName}';`);
styles.push(`font-size: ${this.nativeViewProtected.font.pointSize}px;`);
const bodyStyles: string[] = [];

let htmlContent = this.html ?? '';

htmlContent += '<style>';

bodyStyles.push(`font-size: ${this.style.fontSize}px;`);

if (this.style.fontFamily) {
bodyStyles.push(`font-family: '${this.style.fontFamily}';`);
}
if (this.nativeViewProtected.textColor) {
const textColor = Color.fromIosColor(this.nativeViewProtected.textColor);
styles.push(`color: ${textColor.hex};`);

if (this.style.color) {
bodyStyles.push(`color: ${this.style.color.hex};`);
}
if (styles.length > 0) {
html += `<style>body {${styles.join('')}}</style>`;

htmlContent += `body {${bodyStyles.join('')}}`;

if (this.linkColor) {
htmlContent += `a, a:link, a:visited { color: ${this.linkColor.hex} !important; }`;
}
const htmlString = NSString.stringWithString(html + '');

htmlContent += '</style>';

const htmlString = NSString.stringWithString(htmlContent);
const nsData = htmlString.dataUsingEncoding(NSUnicodeStringEncoding);
this.nativeViewProtected.attributedText = NSAttributedString.alloc().initWithDataOptionsDocumentAttributesError(nsData, <any>{ [NSDocumentTypeDocumentAttribute]: NSHTMLTextDocumentType }, null);
const attributes = NSDictionary.dictionaryWithObjectForKey(NSHTMLTextDocumentType, NSDocumentTypeDocumentAttribute);

if (majorVersion >= 13 && UIColor.labelColor) {
this.nativeViewProtected.attributedText = NSAttributedString.alloc().initWithDataOptionsDocumentAttributesError(nsData, attributes, null);

if (!this.style.color && majorVersion >= 13 && UIColor.labelColor) {
this.nativeViewProtected.textColor = UIColor.labelColor;
}
}
Expand All @@ -86,7 +98,6 @@ export class HtmlView extends HtmlViewBase {
return '';
}
[htmlProperty.setNative](value: string) {
this.currentHtml = value;
this.renderWithStyles();
}

Expand All @@ -106,14 +117,10 @@ export class HtmlView extends HtmlViewBase {
this.renderWithStyles();
}

[linkColorProperty.getDefault](): UIColor {
return this.nativeViewProtected.linkTextAttributes[NSForegroundColorAttributeName];
}
[linkColorProperty.setNative](value: Color | UIColor) {
const color = value instanceof Color ? value.ios : value;
const linkTextAttributes = NSDictionary.dictionaryWithObjectForKey(color, NSForegroundColorAttributeName);
this.nativeViewProtected.linkTextAttributes = linkTextAttributes;
this.renderWithStyles();
}

[fontInternalProperty.getDefault](): UIFont {
return this.nativeViewProtected.font;
}
Expand Down
22 changes: 22 additions & 0 deletions packages/core/ui/text-base/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Property, CssProperty, InheritedCssProperty } from '../core/properties'
import { CoreTypes } from '../../core-types';
import { ShadowCSSValues } from '../styling/css-shadow';
import { StrokeCSSValues } from '../styling/css-stroke';
import { FontStyleType, FontWeightType } from '../styling/font-interfaces';

/**
* @nsView TextBase
Expand All @@ -31,13 +32,34 @@ export class TextBase extends View implements AddChildFromBuilder {
*/
formattedText: FormattedString;

/**
* Gets or sets font-family style property.
*
* @nsProperty
*/
fontFamily: string;

/**
* Gets or sets font-size style property.
*
* @nsProperty
*/
fontSize: number;

/**
* Gets or sets font-style style property.
*
* @nsProperty
*/
fontStyle: FontStyleType;

/**
* Gets or sets font-weight style property.
*
* @nsProperty
*/
fontWeight: FontWeightType;

/**
* Gets or sets letterSpace style property.
*
Expand Down