forked from react-native-oh-library/react-native-text-size
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e8483f5
commit 9ed44be
Showing
30 changed files
with
1,016 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
/** | ||
* Sample React Native App | ||
* https://github.com/facebook/react-native | ||
* | ||
* @format | ||
*/ | ||
|
||
|
||
// useEffect:模拟声明周期 | ||
import React, { useState, useEffect } from 'react'; | ||
import type { PropsWithChildren } from 'react'; | ||
import { | ||
ActivityIndicator, | ||
Dimensions, | ||
Platform, | ||
SafeAreaView, | ||
ScrollView, | ||
StatusBar, | ||
StyleSheet, | ||
Switch, | ||
Text, | ||
TextInput, | ||
useColorScheme, | ||
View, | ||
LayoutChangeEvent, | ||
} from 'react-native'; | ||
|
||
import { | ||
Colors, | ||
DebugInstructions, | ||
Header, | ||
LearnMoreLinks, | ||
ReloadInstructions, | ||
} from 'react-native/Libraries/NewAppScreen'; | ||
|
||
|
||
import rnTextSize from 'react-native-text-size'; | ||
import flatHeights from 'react-native-text-size'; | ||
|
||
import TextSize, { | ||
// typings | ||
TSFontInfo, | ||
TSFontForStyle, | ||
TSFontSpecs, | ||
TSFontStyle, | ||
TSFontVariant, | ||
TSFontWeight, | ||
TSMeasureResult, | ||
TSTextBreakStrategy, | ||
} from 'react-native-text-size'; | ||
|
||
function App(): React.JSX.Element { | ||
|
||
type Props = { texts: string[] } | ||
type State = { heights: number[] } | ||
|
||
type Props1 = {} | ||
type State2 = { width2: number, height2: number } | ||
|
||
const [heights, setHeights] = useState<number[]>([]); | ||
|
||
const [width2, setWidth2] = useState<number>(); | ||
const [height2, setHeight2] = useState<number>(); | ||
|
||
const [texts, setTexts] = useState(['I ❤️ rnTextSize', 'I ❤️ rnTextSize using flatHeights', 'Thx for flatHeights', 'test123',]) | ||
const [texts2, setTexts2] = useState('I ❤️ rnTextSize') | ||
const [res, setRes] = useState<string[]>([]) | ||
const [key, setKey] = useState({}) | ||
const [info, setInfo] = useState<TSFontInfo>() | ||
|
||
|
||
const fontSpecs: TSFontSpecs = { | ||
fontFamily: undefined, | ||
fontSize: 20, | ||
fontStyle: 'italic', //正斜 | ||
fontWeight: 'bold', | ||
} | ||
const fontSpecs2: TSFontSpecs = { | ||
fontFamily: 'Android', | ||
textBreakStrategy: 'balanced', | ||
} | ||
|
||
const fontSpecs3: TSFontSpecs = { | ||
fontFamily: undefined, | ||
fontSize: 20, | ||
fontStyle: 'normal', //正斜 | ||
fontWeight: '700', | ||
} | ||
|
||
useEffect(() => { | ||
const myFun = async () => { | ||
const resp = await rnTextSize.fontFamilyNames(); | ||
setRes(resp) | ||
const keyp = await rnTextSize.specsForTextStyles(); | ||
setKey(keyp) | ||
const infos = await rnTextSize.fontFromSpecs(fontSpecs2); | ||
setInfo(infos) | ||
|
||
setTexts(texts) | ||
const width = Dimensions.get('window').width * 0.8 | ||
const newHeights = await rnTextSize.flatHeights({ | ||
text: texts, // array of texts to measure, can include symbols | ||
width, // max-width of the "virtual" container | ||
...fontSpecs, // RN font specification | ||
}) | ||
setHeights(newHeights); | ||
|
||
setTexts2(texts2) | ||
const width2 = Dimensions.get('window').width * 0.8 | ||
const newHeight2 = await rnTextSize.measure({ | ||
text: texts2, // array of texts to measure, can include symbols | ||
width: width2, // max-width of the "virtual" container | ||
...fontSpecs3, // RN font specification | ||
}) | ||
setHeight2(newHeight2.height); | ||
setWidth2(newHeight2.width) | ||
} | ||
myFun() | ||
}, []) | ||
|
||
return ( | ||
<ScrollView style={{ flexGrow: 1 }}> | ||
<View style={{ paddingLeft: 12, paddingRight: 12 }}> | ||
<Text> | ||
------------------------------ | ||
--------------- | ||
measure接口:[fontFamily: undefined,fontSize: 20,fontStyle: 'normal', 正斜fontWeight: '700'] | ||
</Text> | ||
<View> | ||
<Text style={{ | ||
width: width2, | ||
height: height2, | ||
...fontSpecs3 | ||
}}> | ||
{texts2} | ||
</Text> | ||
</View> | ||
<Text> | ||
---------------- | ||
flatHeights接口:[fontFamily: undefined,fontSize: 20,fontStyle: 'italic', fontWeight: 'bold'] | ||
</Text> | ||
{texts.map( | ||
(text, index) => ( | ||
<Text style={{ height: heights[index], ...fontSpecs }}> | ||
{text} | ||
</Text> | ||
) | ||
)} | ||
<Text> | ||
----------------fontFamilyNames接口:获取系统默认配置的字体 | ||
</Text> | ||
<Text> | ||
{res} | ||
</Text> | ||
|
||
<Text> | ||
----------------specsForTextStyles接口:获取系统默认配置的字体的具体信息 | ||
</Text> | ||
<Text> | ||
{JSON.stringify(key)} | ||
</Text> | ||
|
||
|
||
<Text> | ||
----------------fontFromSpecs:返回从给定规范中获得的字体特征 | ||
</Text> | ||
<Text> | ||
{JSON.stringify(info)} | ||
</Text> | ||
</View> | ||
</ScrollView> | ||
) | ||
|
||
} | ||
|
||
export default App; |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default class BuildProfile { | ||
static readonly BUNDLE_NAME = 'com.rnoh.tester'; | ||
static readonly VERSION_CODE = 1000000; | ||
static readonly VERSION_NAME = '1.0.0'; | ||
static readonly HAR_VERSION = '1.0.0'; | ||
static readonly BUILD_MODE_NAME = 'Debug'; | ||
static readonly DEBUG = true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './ts' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"apiType": "stageMode", | ||
"targets": [ | ||
{ | ||
"name": "default", | ||
"runtimeOS": "HarmonyOS" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Script for compiling build behavior. It is built in the build plug-in and cannot be modified currently. | ||
export { harTasks } from '@ohos/hvigor-ohos-plugin'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Define project specific obfuscation rules here. | ||
# You can include the obfuscation configuration files in the current module's build-profile.json5. | ||
# | ||
# For more details, see | ||
# https://gitee.com/openharmony/arkcompiler_ets_frontend/blob/master/arkguard/README.md | ||
|
||
# Obfuscation options: | ||
# -disable-obfuscation: disable all obfuscations | ||
# -enable-property-obfuscation: obfuscate the property names | ||
# -enable-toplevel-obfuscation: obfuscate the names in the global scope | ||
# -compact: remove unnecessary blank spaces and all line feeds | ||
# -remove-log: remove all console.* statements | ||
# -print-namecache: print the name cache that contains the mapping from the old names to new names | ||
# -apply-namecache: reuse the given cache file | ||
|
||
# Keep options: | ||
# -keep-property-name: specifies property names that you want to keep | ||
# -keep-global-name: specifies names that you want to keep in the global scope |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"license": "MIT", | ||
"devDependencies": { | ||
}, | ||
"author": "", | ||
"name": "rnoh-text-size", | ||
"description": "Please describe the basic information.", | ||
"main": "index.ets", | ||
"version": "1.0.0", | ||
"dependencies": { | ||
"rnoh": "file:../rnoh" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
cmake_minimum_required(VERSION 3.13) | ||
set(CMAKE_VERBOSE_MAKEFILE on) | ||
|
||
file(GLOB rnoh_text_size_SRC CONFIGURE_DEPENDS *.cpp) | ||
add_library(rnoh_text_size SHARED ${rnoh_text_size_SRC}) | ||
target_include_directories(rnoh_text_size PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) | ||
target_link_libraries(rnoh_text_size PUBLIC rnoh) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* MIT License | ||
* | ||
* Copyright (C) 2024 Huawei Device Co., Ltd. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
# pragma once | ||
# include "RNOH/Package.h" | ||
# include "RNTextSizeTurboModule.h" | ||
|
||
using namespace rnoh; | ||
using namespace facebook; | ||
|
||
class RNTextSizeTurboModuleFactoryDelegate : public TurboModuleFactoryDelegate { | ||
public: | ||
SharedTurboModule createTurboModule(Context ctx, const std::string &name) const override | ||
{ | ||
if (name == "RTNTextSize") { | ||
return std::make_shared<RNTextSizeTurboModule>(ctx, name); | ||
} | ||
return nullptr; | ||
}; | ||
}; | ||
namespace rnoh { | ||
class RNTextSizePackage : public Package { | ||
public: | ||
RNTextSizePackage(Package::Context ctx) : Package(ctx) {} | ||
std::unique_ptr<TurboModuleFactoryDelegate> createTurboModuleFactoryDelegate() override | ||
{ | ||
return std::make_unique<RNTextSizeTurboModuleFactoryDelegate>(); | ||
} | ||
}; | ||
} // namespace rnoh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* MIT License | ||
* | ||
* Copyright (C) 2023 Huawei Device Co., Ltd. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
#include "RNTextSizeTurbomodule.h" | ||
#include "RNOH/ArkTSTurboModule.h" | ||
|
||
using namespace rnoh; | ||
using namespace facebook; | ||
|
||
static jsi::Value __hostFunction_RNTextSizeTurboModule_measure(jsi::Runtime &rt, | ||
react::TurboModule &turboModule, | ||
const jsi::Value *args, size_t count) | ||
{ | ||
return static_cast<ArkTSTurboModule &>(turboModule).callAsync(rt, "measure", args, count); | ||
} | ||
static jsi::Value __hostFunction_RNTextSizeTurboModule_flatHeights(jsi::Runtime &rt, | ||
react::TurboModule &turboModule, | ||
const jsi::Value *args, size_t count) | ||
{ | ||
return static_cast<ArkTSTurboModule &>(turboModule).callAsync(rt, "flatHeights", args, count); | ||
} | ||
static jsi::Value __hostFunction_RNTextSizeTurboModule_specsForTextStyles(jsi::Runtime &rt, | ||
react::TurboModule &turboModule, | ||
const jsi::Value *args, size_t count) | ||
{ | ||
return static_cast<ArkTSTurboModule &>(turboModule).callAsync(rt, "specsForTextStyles", args, count); | ||
} | ||
static jsi::Value __hostFunction_RNTextSizeTurboModule_fontFromSpecs(jsi::Runtime &rt, | ||
react::TurboModule &turboModule, | ||
const jsi::Value *args, size_t count) | ||
{ | ||
return static_cast<ArkTSTurboModule &>(turboModule).callAsync(rt, "fontFromSpecs", args, count); | ||
} | ||
static jsi::Value __hostFunction_RNTextSizeTurboModule_fontFamilyNames(jsi::Runtime &rt, | ||
react::TurboModule &turboModule, | ||
const jsi::Value *args, size_t count) | ||
{ | ||
return static_cast<ArkTSTurboModule &>(turboModule).callAsync(rt, "fontFamilyNames", args, count); | ||
} | ||
static jsi::Value __hostFunction_RNTextSizeTurboModule_fontNamesForFamilyName(jsi::Runtime &rt, | ||
react::TurboModule &turboModule, | ||
const jsi::Value *args, size_t count) | ||
{ | ||
return static_cast<ArkTSTurboModule &>(turboModule).callAsync(rt, "fontNamesForFamilyName", args, count); | ||
} | ||
|
||
|
||
RNTextSizeTurboModule::RNTextSizeTurboModule(const ArkTSTurboModule::Context ctx, const std::string name) | ||
: ArkTSTurboModule(ctx, name) | ||
{ | ||
methodMap_["measure"] = MethodMetadata{1, __hostFunction_RNTextSizeTurboModule_measure}; | ||
methodMap_["flatHeights"] = MethodMetadata{1, __hostFunction_RNTextSizeTurboModule_flatHeights}; | ||
methodMap_["specsForTextStyles"] = MethodMetadata{0, __hostFunction_RNTextSizeTurboModule_specsForTextStyles}; | ||
methodMap_["fontFromSpecs"] = MethodMetadata{1, __hostFunction_RNTextSizeTurboModule_fontFromSpecs}; | ||
methodMap_["fontFamilyNames"] = MethodMetadata{0, __hostFunction_RNTextSizeTurboModule_fontFamilyNames}; | ||
methodMap_["fontNamesForFamilyName"] = MethodMetadata{1, __hostFunction_RNTextSizeTurboModule_fontNamesForFamilyName}; | ||
} |
Oops, something went wrong.