UNPKG

4.75 kBJavaScriptView Raw
1import { Color } from '../../color';
2import { Font } from '../styling/font';
3import { colorProperty, fontInternalProperty } from '../styling/style-properties';
4import { HtmlViewBase, htmlProperty, selectableProperty, linkColorProperty } from './html-view-common';
5import { View } from '../core/view';
6import { iOSNativeHelper, layout } from '../../utils';
7export * from './html-view-common';
8const majorVersion = iOSNativeHelper.MajorVersion;
9export class HtmlView extends HtmlViewBase {
10 createNativeView() {
11 const view = UITextView.new();
12 view.scrollEnabled = false;
13 view.editable = false;
14 view.selectable = true;
15 view.userInteractionEnabled = true;
16 view.dataDetectorTypes = -1 /* UIDataDetectorTypes.All */;
17 return view;
18 }
19 initNativeView() {
20 super.initNativeView();
21 // Remove extra padding
22 this.nativeViewProtected.textContainer.lineFragmentPadding = 0;
23 this.nativeViewProtected.textContainerInset = UIEdgeInsets.zero;
24 }
25 // @ts-ignore
26 get ios() {
27 return this.nativeViewProtected;
28 }
29 onMeasure(widthMeasureSpec, heightMeasureSpec) {
30 const nativeView = this.nativeViewProtected;
31 if (nativeView) {
32 const width = layout.getMeasureSpecSize(widthMeasureSpec);
33 const widthMode = layout.getMeasureSpecMode(widthMeasureSpec);
34 const height = layout.getMeasureSpecSize(heightMeasureSpec);
35 const heightMode = layout.getMeasureSpecMode(heightMeasureSpec);
36 const desiredSize = layout.measureNativeView(nativeView, width, widthMode, height, heightMode);
37 const labelWidth = widthMode === layout.AT_MOST ? Math.min(desiredSize.width, width) : desiredSize.width;
38 const measureWidth = Math.max(labelWidth, this.effectiveMinWidth);
39 const measureHeight = Math.max(desiredSize.height, this.effectiveMinHeight);
40 const widthAndState = View.resolveSizeAndState(measureWidth, width, widthMode, 0);
41 const heightAndState = View.resolveSizeAndState(measureHeight, height, heightMode, 0);
42 this.setMeasuredDimension(widthAndState, heightAndState);
43 }
44 }
45 renderWithStyles() {
46 let html = this.currentHtml;
47 const styles = [];
48 if (this.nativeViewProtected.font) {
49 styles.push(`font-family: '${this.nativeViewProtected.font.fontName}';`);
50 styles.push(`font-size: ${this.nativeViewProtected.font.pointSize}px;`);
51 }
52 if (this.nativeViewProtected.textColor) {
53 const textColor = Color.fromIosColor(this.nativeViewProtected.textColor);
54 styles.push(`color: ${textColor.hex};`);
55 }
56 if (styles.length > 0) {
57 html += `<style>body {${styles.join('')}}</style>`;
58 }
59 const htmlString = NSString.stringWithString(html + '');
60 const nsData = htmlString.dataUsingEncoding(NSUnicodeStringEncoding);
61 this.nativeViewProtected.attributedText = NSAttributedString.alloc().initWithDataOptionsDocumentAttributesError(nsData, { [NSDocumentTypeDocumentAttribute]: NSHTMLTextDocumentType }, null);
62 if (majorVersion >= 13 && UIColor.labelColor) {
63 this.nativeViewProtected.textColor = UIColor.labelColor;
64 }
65 }
66 [htmlProperty.getDefault]() {
67 return '';
68 }
69 [htmlProperty.setNative](value) {
70 this.currentHtml = value;
71 this.renderWithStyles();
72 }
73 [selectableProperty.getDefault]() {
74 return true;
75 }
76 [selectableProperty.setNative](value) {
77 this.nativeViewProtected.selectable = value;
78 }
79 [colorProperty.getDefault]() {
80 return this.nativeViewProtected.textColor;
81 }
82 [colorProperty.setNative](value) {
83 const color = value instanceof Color ? value.ios : value;
84 this.nativeViewProtected.textColor = color;
85 this.renderWithStyles();
86 }
87 [linkColorProperty.getDefault]() {
88 return this.nativeViewProtected.linkTextAttributes[NSForegroundColorAttributeName];
89 }
90 [linkColorProperty.setNative](value) {
91 const color = value instanceof Color ? value.ios : value;
92 const linkTextAttributes = NSDictionary.dictionaryWithObjectForKey(color, NSForegroundColorAttributeName);
93 this.nativeViewProtected.linkTextAttributes = linkTextAttributes;
94 }
95 [fontInternalProperty.getDefault]() {
96 return this.nativeViewProtected.font;
97 }
98 [fontInternalProperty.setNative](value) {
99 const font = value instanceof Font ? value.getUIFont(this.nativeViewProtected.font) : value;
100 this.nativeViewProtected.font = font;
101 this.renderWithStyles();
102 }
103}
104//# sourceMappingURL=index.ios.js.map
\No newline at end of file