Skip to content

fix: 解决measure接口返回数据未作单位转换(px -> vp) #22

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 1 commit into from
Dec 25, 2024
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
2 changes: 0 additions & 2 deletions .vscode/settings.json

This file was deleted.

Binary file modified harmony/text_size.har
Binary file not shown.
2 changes: 1 addition & 1 deletion harmony/text_size/oh-package.json5
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"name": "@react-native-oh-tpl/react-native-text-size",
"description": "Please describe the basic information.",
"main": "index.ets",
"version": "4.0.0-0.0.9",
"version": "4.0.0-0.0.10",
"dependencies": {
"@rnoh/react-native-openharmony": "file:../react_native_openharmony"
}
Expand Down
2 changes: 2 additions & 0 deletions harmony/text_size/src/main/ets/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import measure, { MeasureOptions } from '@ohos.measure';
import display from '@ohos.display';
import { text } from "@kit.ArkGraphics2D";

export const getDensity = () => display.getDefaultDisplaySync().densityPixels;

export function config(measureText: MeasureOptions) {
let textSize = measure.measureTextSize(measureText);
return textSize;
Expand Down
10 changes: 6 additions & 4 deletions harmony/text_size/src/main/ets/RNTextSizeTurboModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import { TurboModule } from "@rnoh/react-native-openharmony/ts";
import font from '@ohos.font'
import measure, { MeasureOptions } from '@ohos.measure'
import { config, measureTextParagraph } from './Config';
import { config, measureTextParagraph, getDensity } from './Config';
import display from '@ohos.display';

export class RNTextSizeTurboModule extends TurboModule {
Expand All @@ -45,9 +45,10 @@ export class RNTextSizeTurboModule extends TurboModule {
let height: number = textSize.height as number;
let measureResult = measureTextParagraph(measureText, options.width);

const density = getDensity();
let result: TSMeasureResult = {
width: width,
height: height,
width: width / density,
height: height / density,
lineCount: measureResult.lines,
}
if (options.usePreciseWidth) {
Expand All @@ -61,6 +62,7 @@ export class RNTextSizeTurboModule extends TurboModule {
};

flatHeights(options: TSHeightsParams): Promise<number[]> {
const density = getDensity();
return new Promise<number[]>((resolve, reject) => {
try {
const text = options.text;
Expand All @@ -75,7 +77,7 @@ export class RNTextSizeTurboModule extends TurboModule {
letterSpacing: options.letterSpacing,
}
let textSize = config(measureText);
let height: number = textSize.height as number;
let height: number = textSize.height as number / density;
prev.push(height);
return prev;
}, [])
Expand Down