Skip to content

Commit 0374392

Browse files
committed
Document paragraph.getHeight method
1 parent 7dd903c commit 0374392

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

docs/docs/text/paragraph.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,32 @@ const textStyle = {
7878
};
7979
```
8080

81+
## Paragraph Height
82+
83+
To get the paragraph height, you can calculate the layout using `layout()` and once done, you can invoke `getHeight()`.
84+
85+
```tsx twoslash
86+
import { useMemo } from "react";
87+
import { Paragraph, Skia, useFonts } from "@shopify/react-native-skia";
88+
89+
const MyParagraph = () => {
90+
const paragraph = useMemo(() => {
91+
const para = Skia.ParagraphBuilder.Make()
92+
.addText("Say Hello to ")
93+
.addText("Skia 🎨")
94+
.pop()
95+
.build();
96+
// Calculate the layout
97+
para.layout(300);
98+
return para;
99+
}, []);
100+
// Now the paragraph height is available
101+
const height = paragraph.getHeight();
102+
// Render the paragraph
103+
return <Paragraph paragraph={paragraph} x={0} y={0} width={300} />;
104+
};
105+
```
106+
81107
## Fonts
82108

83109
By default, the paragraph API will use the system fonts.
23.1 KB
Loading

package/src/renderer/__tests__/e2e/Paragraphs.spec.tsx

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,67 @@ describe("Paragraphs", () => {
453453
);
454454
});
455455

456+
itRunsE2eOnly("should draw the bounding box", async () => {
457+
const img = await surface.drawOffscreen(
458+
(Skia, canvas, { bold, boldItalic, italic }) => {
459+
const para = Skia.ParagraphBuilder.Make()
460+
.pushStyle({
461+
fontStyle: italic,
462+
color: Skia.Color("black"),
463+
})
464+
.addText("Hello Skia in italic")
465+
.pop()
466+
.pushStyle({ fontStyle: bold, color: Skia.Color("black") })
467+
.addText("Hello Skia in bold")
468+
.pop()
469+
.pushStyle({
470+
fontStyle: boldItalic,
471+
color: Skia.Color("black"),
472+
})
473+
.addText("Hello Skia in bold-italic")
474+
.pop()
475+
.build();
476+
para.layout(150);
477+
const height = para.getHeight();
478+
const paint = Skia.Paint();
479+
paint.setColor(Skia.Color("cyan"));
480+
canvas.drawRect(Skia.XYWHRect(0, 0, 150, height), paint);
481+
para.paint(canvas, 0, 0);
482+
},
483+
{
484+
bold: FontStyle.Bold,
485+
italic: FontStyle.Italic,
486+
boldItalic: FontStyle.BoldItalic,
487+
}
488+
);
489+
checkImage(
490+
img,
491+
`snapshots/paragraph/paragraph-text-style-font-style-${surface.OS}-box.png`
492+
);
493+
});
494+
495+
itRunsE2eOnly("should return the paragraph height", async () => {
496+
const { width, height } = await surface.eval((Skia) => {
497+
const para = Skia.ParagraphBuilder.Make()
498+
.pushStyle({
499+
color: Skia.Color("black"),
500+
fontSize: 25,
501+
shadows: [
502+
{
503+
color: Skia.Color("#ff000044"),
504+
blurRadius: 4,
505+
offset: { x: 4, y: 4 },
506+
},
507+
],
508+
})
509+
.addText("Hello Skia with red shadow")
510+
.build();
511+
para.layout(150);
512+
return { height: para.getHeight(), width: para.getMaxWidth() };
513+
});
514+
expect(width).toBe(150);
515+
expect(height).toBeGreaterThan(25);
516+
});
456517
itRunsE2eOnly("should support font shadows", async () => {
457518
const img = await surface.drawOffscreen((Skia, canvas) => {
458519
const para = Skia.ParagraphBuilder.Make()

0 commit comments

Comments
 (0)