Browse Source

add local custom font for demo

iou90 4 years ago
parent
commit
e20917b542

+ 54
- 39
demo/App.js View File

@@ -1,6 +1,13 @@
1
-import React, { useState } from 'react';
1
+import React, {useState} from 'react';
2 2
 
3
-import { ScrollView, StyleSheet, Text, TouchableOpacity, Platform, Linking } from 'react-native';
3
+import {
4
+  ScrollView,
5
+  StyleSheet,
6
+  Text,
7
+  TouchableOpacity,
8
+  Platform,
9
+  Linking,
10
+} from 'react-native';
4 11
 
5 12
 import AutoHeightWebView from 'react-native-autoheight-webview';
6 13
 
@@ -13,7 +20,7 @@ import {
13 20
   autoWidthScript,
14 21
   autoDetectLinkScript,
15 22
   style0,
16
-  inlineBodyStyle
23
+  inlineBodyStyle,
17 24
 } from './config';
18 25
 
19 26
 const onShouldStartLoadWithRequest = result => {
@@ -21,10 +28,11 @@ const onShouldStartLoadWithRequest = result => {
21 28
   return true;
22 29
 };
23 30
 
24
-const onError = ({ nativeEvent }) => console.error('WebView error: ', nativeEvent);
31
+const onError = ({nativeEvent}) =>
32
+  console.error('WebView error: ', nativeEvent);
25 33
 
26 34
 const onMessage = event => {
27
-  const { data } = event.nativeEvent;
35
+  const {data} = event.nativeEvent;
28 36
   let messageData;
29 37
   // maybe parse stringified JSON
30 38
   try {
@@ -33,10 +41,12 @@ const onMessage = event => {
33 41
     console.log(e.message);
34 42
   }
35 43
   if (typeof messageData === 'object') {
36
-    const { url } = messageData;
44
+    const {url} = messageData;
37 45
     // check if this message concerns us
38 46
     if (url && url.startsWith('http')) {
39
-      Linking.openURL(url).catch(error => console.error('An error occurred', error));
47
+      Linking.openURL(url).catch(error =>
48
+        console.error('An error occurred', error),
49
+      );
40 50
     }
41 51
   }
42 52
 };
@@ -54,51 +64,56 @@ const onWidthLoad = () => console.log('width on load');
54 64
 const onWidthLoadEnd = () => console.log('width on load end');
55 65
 
56 66
 const Explorer = () => {
57
-  const [{ widthHtml, heightHtml }, setHtml] = useState({
67
+  const [{widthHtml, heightHtml}, setHtml] = useState({
58 68
     widthHtml: autoWidthHtml0,
59
-    heightHtml: autoHeightHtml0
69
+    heightHtml: autoHeightHtml0,
60 70
   });
61 71
   const changeSource = () =>
62 72
     setHtml({
63 73
       widthHtml: widthHtml === autoWidthHtml0 ? autoWidthHtml1 : autoWidthHtml0,
64
-      heightHtml: heightHtml === autoHeightHtml0 ? autoHeightHtml1 : autoHeightHtml0
74
+      heightHtml:
75
+        heightHtml === autoHeightHtml0 ? autoHeightHtml1 : autoHeightHtml0,
65 76
     });
66 77
 
67
-  const [{ widthStyle, heightStyle }, setStyle] = useState({
78
+  const [{widthStyle, heightStyle}, setStyle] = useState({
68 79
     heightStyle: null,
69
-    widthStyle: inlineBodyStyle
80
+    widthStyle: inlineBodyStyle,
70 81
   });
71 82
   const changeStyle = () =>
72 83
     setStyle({
73
-      widthStyle: widthStyle === inlineBodyStyle ? style0 + inlineBodyStyle : inlineBodyStyle,
74
-      heightStyle: heightStyle === null ? style0 : null
84
+      widthStyle:
85
+        widthStyle === inlineBodyStyle
86
+          ? style0 + inlineBodyStyle
87
+          : inlineBodyStyle,
88
+      heightStyle: heightStyle === null ? style0 : null,
75 89
     });
76 90
 
77
-  const [{ widthScript, heightScript }, setScript] = useState({
91
+  const [{widthScript, heightScript}, setScript] = useState({
78 92
     heightScript: autoDetectLinkScript,
79
-    widthScript: null
93
+    widthScript: null,
80 94
   });
81 95
   const changeScript = () =>
82 96
     setScript({
83 97
       widthScript: widthScript == autoWidthScript ? autoWidthScript : null,
84 98
       heightScript:
85
-        heightScript !== autoDetectLinkScript ? autoDetectLinkScript : autoHeightScript + autoDetectLinkScript
99
+        heightScript !== autoDetectLinkScript
100
+          ? autoDetectLinkScript
101
+          : autoHeightScript + autoDetectLinkScript,
86 102
     });
87 103
 
88
-  const [heightSize, setHeightSize] = useState({ height: 0, width: 0 });
89
-  const [widthSize, setWidthSize] = useState({ height: 0, width: 0 });
104
+  const [heightSize, setHeightSize] = useState({height: 0, width: 0});
105
+  const [widthSize, setWidthSize] = useState({height: 0, width: 0});
90 106
 
91 107
   return (
92 108
     <ScrollView
93 109
       style={{
94 110
         paddingTop: 45,
95
-        backgroundColor: 'lightyellow'
111
+        backgroundColor: 'lightyellow',
96 112
       }}
97 113
       contentContainerStyle={{
98 114
         justifyContent: 'center',
99
-        alignItems: 'center'
100
-      }}
101
-    >
115
+        alignItems: 'center',
116
+      }}>
102 117
       <AutoHeightWebView
103 118
         customStyle={heightStyle}
104 119
         onError={onError}
@@ -107,26 +122,18 @@ const Explorer = () => {
107 122
         onLoadEnd={onHeightLoadEnd}
108 123
         onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
109 124
         onSizeUpdated={setHeightSize}
110
-        source={{ html: heightHtml }}
125
+        source={{html: heightHtml}}
111 126
         customScript={heightScript}
112 127
         onMessage={onMessage}
113 128
       />
114
-      <Text style={{ padding: 5 }}>
129
+      <Text style={{padding: 5}}>
115 130
         height: {heightSize.height}, width: {heightSize.width}
116 131
       </Text>
117 132
       <AutoHeightWebView
118
-        baseUrl={Platform.OS === 'android' ? 'file:///android_asset/webAssets/' : 'webAssets/'}
119 133
         style={{
120
-          marginTop: 15
134
+          marginTop: 15,
121 135
         }}
122 136
         enableBaseUrl
123
-        files={[
124
-          {
125
-            href: 'demo.css',
126
-            type: 'text/css',
127
-            rel: 'stylesheet'
128
-          }
129
-        ]}
130 137
         customStyle={widthStyle}
131 138
         onError={onError}
132 139
         onLoad={onWidthLoad}
@@ -134,10 +141,16 @@ const Explorer = () => {
134 141
         onLoadEnd={onWidthLoadEnd}
135 142
         onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
136 143
         onSizeUpdated={setWidthSize}
137
-        source={{ html: widthHtml }}
144
+        allowFileAccessFromFileURLs={true}
145
+        allowUniversalAccessFromFileURLs={true}
146
+        source={{
147
+          html: widthHtml,
148
+          baseUrl:
149
+            Platform.OS === 'android' ? 'file:///android_asset/' : 'web/',
150
+        }}
138 151
         customScript={widthScript}
139 152
       />
140
-      <Text style={{ padding: 5 }}>
153
+      <Text style={{padding: 5}}>
141 154
         height: {widthSize.height}, width: {widthSize.width}
142 155
       </Text>
143 156
       <TouchableOpacity onPress={changeSource} style={styles.button}>
@@ -146,7 +159,9 @@ const Explorer = () => {
146 159
       <TouchableOpacity onPress={changeStyle} style={styles.button}>
147 160
         <Text>change style</Text>
148 161
       </TouchableOpacity>
149
-      <TouchableOpacity onPress={changeScript} style={[styles.button, { marginBottom: 100 }]}>
162
+      <TouchableOpacity
163
+        onPress={changeScript}
164
+        style={[styles.button, {marginBottom: 100}]}>
150 165
         <Text>change script</Text>
151 166
       </TouchableOpacity>
152 167
     </ScrollView>
@@ -158,8 +173,8 @@ const styles = StyleSheet.create({
158 173
     marginTop: 15,
159 174
     backgroundColor: 'aliceblue',
160 175
     borderRadius: 5,
161
-    padding: 5
162
-  }
176
+    padding: 5,
177
+  },
163 178
 });
164 179
 
165 180
 export default Explorer;

BIN
demo/android/app/src/main/assets/ae_AlArabiya.ttf View File


+ 7
- 1
demo/config.js View File

@@ -20,7 +20,13 @@ const style1 = `
20 20
 `;
21 21
 
22 22
 const inlineBodyStyle = `
23
+    @font-face {
24
+      font-family: ae_AlArabiya;
25
+      src:url("ae_AlArabiya.ttf");
26
+    }
27
+
23 28
     body {
29
+        font-family: ae_AlArabiya;
24 30
         display: inline-block;
25 31
     }
26 32
 `;
@@ -50,7 +56,7 @@ document.body.style.background = 'cornflowerblue';
50 56
 const autoWidthHtml0 = `
51 57
 <html>
52 58
 <head>
53
-  <meta name="viewport" content="target-densitydpi=device-dpi, initial-scale=1.0, user-scalable=no" />
59
+  <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
54 60
 </head>
55 61
 <p class="localStyle" style="display: inline;background-color: transparent !important;background-image: linear-gradient(to bottom, rgba(146, 249, 190, 1), rgba(146, 249, 190, 1));font-style: normal;font-size: 21px;line-height: 1.58;letter-spacing: -.003em;padding-top:0;padding-bottom:0;">hey</p>
56 62
 </html>

+ 14
- 2
demo/ios/demo.xcodeproj/project.pbxproj View File

@@ -19,6 +19,7 @@
19 19
 		428E7C00A1DF1C1A2C1140EE /* libPods-demoTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 34E943675B55A190041D9BC6 /* libPods-demoTests.a */; };
20 20
 		5D9570ED1A0B682B5FF5E421 /* libPods-demo-tvOSTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 821EC82BD30472D6D387B788 /* libPods-demo-tvOSTests.a */; };
21 21
 		A50903E452D634CE572F43CD /* libPods-demo.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 8486CCD792395AAD80956F45 /* libPods-demo.a */; };
22
+		B7C1B3872394B353001240AA /* ae_AlArabiya.ttf in Resources */ = {isa = PBXBuildFile; fileRef = B7C1B3862394B353001240AA /* ae_AlArabiya.ttf */; };
22 23
 		EFB606294F69E6FA304BD500 /* libPods-demo-tvOS.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 9D8370FC4C13D1AE1CC8852B /* libPods-demo-tvOS.a */; };
23 24
 /* End PBXBuildFile section */
24 25
 
@@ -62,6 +63,7 @@
62 63
 		914896C0CEFDAA592DADF952 /* Pods-demo.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-demo.release.xcconfig"; path = "Target Support Files/Pods-demo/Pods-demo.release.xcconfig"; sourceTree = "<group>"; };
63 64
 		9D8370FC4C13D1AE1CC8852B /* libPods-demo-tvOS.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-demo-tvOS.a"; sourceTree = BUILT_PRODUCTS_DIR; };
64 65
 		A74FCF62A73172586BCD1E8C /* Pods-demo-tvOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-demo-tvOS.release.xcconfig"; path = "Target Support Files/Pods-demo-tvOS/Pods-demo-tvOS.release.xcconfig"; sourceTree = "<group>"; };
66
+		B7C1B3862394B353001240AA /* ae_AlArabiya.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = ae_AlArabiya.ttf; sourceTree = "<group>"; };
65 67
 		B932E4CF7C7ADA47E0C72933 /* Pods-demo-tvOSTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-demo-tvOSTests.debug.xcconfig"; path = "Target Support Files/Pods-demo-tvOSTests/Pods-demo-tvOSTests.debug.xcconfig"; sourceTree = "<group>"; };
66 68
 		BB1DF6C3AB03F9FA00E48CAE /* Pods-demo-tvOS.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-demo-tvOS.debug.xcconfig"; path = "Target Support Files/Pods-demo-tvOS/Pods-demo-tvOS.debug.xcconfig"; sourceTree = "<group>"; };
67 69
 		ED297162215061F000B7C4FE /* JavaScriptCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = JavaScriptCore.framework; path = System/Library/Frameworks/JavaScriptCore.framework; sourceTree = SDKROOT; };
@@ -148,7 +150,6 @@
148 150
 				43307E7576B385875B79FCDF /* Pods-demoTests.debug.xcconfig */,
149 151
 				150E6CAFA5EF11AE47ED825C /* Pods-demoTests.release.xcconfig */,
150 152
 			);
151
-			name = Pods;
152 153
 			path = Pods;
153 154
 			sourceTree = "<group>";
154 155
 		};
@@ -175,6 +176,7 @@
175 176
 		83CBB9F61A601CBA00E9B192 = {
176 177
 			isa = PBXGroup;
177 178
 			children = (
179
+				B7C1B3852394B30A001240AA /* web */,
178 180
 				13B07FAE1A68108700A75B9A /* demo */,
179 181
 				832341AE1AAA6A7D00B99B32 /* Libraries */,
180 182
 				00E356EF1AD99517003FC87E /* demoTests */,
@@ -198,6 +200,14 @@
198 200
 			name = Products;
199 201
 			sourceTree = "<group>";
200 202
 		};
203
+		B7C1B3852394B30A001240AA /* web */ = {
204
+			isa = PBXGroup;
205
+			children = (
206
+				B7C1B3862394B353001240AA /* ae_AlArabiya.ttf */,
207
+			);
208
+			path = web;
209
+			sourceTree = "<group>";
210
+		};
201 211
 /* End PBXGroup section */
202 212
 
203 213
 /* Begin PBXNativeTarget section */
@@ -308,6 +318,7 @@
308 318
 			developmentRegion = English;
309 319
 			hasScannedForEncodings = 0;
310 320
 			knownRegions = (
321
+				English,
311 322
 				en,
312 323
 				Base,
313 324
 			);
@@ -338,6 +349,7 @@
338 349
 			files = (
339 350
 				13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */,
340 351
 				13B07FBD1A68108700A75B9A /* LaunchScreen.xib in Resources */,
352
+				B7C1B3872394B353001240AA /* ae_AlArabiya.ttf in Resources */,
341 353
 			);
342 354
 			runOnlyForDeploymentPostprocessing = 0;
343 355
 		};
@@ -920,4 +932,4 @@
920 932
 /* End XCConfigurationList section */
921 933
 	};
922 934
 	rootObject = 83CBB9F71A601CBA00E9B192 /* Project object */;
923
-}
935
+}

BIN
demo/ios/web/ae_AlArabiya.ttf View File