Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): center iOS message text within its line height (#4198)

Chris Zhang7ff1b4acc26b
diff --git a/apps/mobile/src/session/MessageRenderer.tsx b/apps/mobile/src/session/MessageRenderer.tsx
index 87cb598329..306cd73918 100644
--- a/apps/mobile/src/session/MessageRenderer.tsx
+++ b/apps/mobile/src/session/MessageRenderer.tsx
@@ -96,6 +96,7 @@ import {
useCancelShareSelectionRowTap,
} from '@/session/ShareMessageCheckbox';
import { SentInlineAtomBody } from '@/session/SentInlineAtomBody';
+import { selectableTextVerticalOffset } from '@/session/selectableTextAlignment';
import {
composerDocumentFromSerializedMessage,
type ComposerDocument,
@@ -496,10 +497,16 @@ type MarkdownSelectableTextProps = Omit<ComponentProps<typeof Text>, 'selectionC
* UITextView 在折叠→展开时骤增为超高复用视图会偶发只留下巨高空白容器。
*/
allowIosUITextView?: boolean;
+ /** Decorated table cells must stay on the row grid, including their borders. */
+ adjustVerticalAlignment?: boolean;
+ /** Include larger inline headings when aligning a combined text run. */
+ alignmentTextStyles?: readonly StyleProp<TextStyle>[];
};
function MarkdownSelectableText({
allowIosUITextView = true,
+ adjustVerticalAlignment = true,
+ alignmentTextStyles,
selectable,
...rest
}: MarkdownSelectableTextProps) {
@@ -509,7 +516,14 @@ function MarkdownSelectableText({
// 文本提交)。context 为 null(宿主未启用 / 非会话场景)时零开销走原路径。
const quoteCtx = useContext(SelectionQuoteContext);
const renderedLinesRef = useRef<readonly string[]>([]);
+ const { fontScale } = useWindowDimensions();
if (selectable && allowIosUITextView && Platform.OS === 'ios') {
+ const typography = [rest.style, ...(alignmentTextStyles ?? [])]
+ .map((style) => StyleSheet.flatten(style) ?? {});
+ const top = selectableTextVerticalOffset(typography, rest.allowFontScaling === false ? 1 : fontScale);
+ // Position only the selectable root. RN Text already centers its leading;
+ // shifting the bubble itself would also shift pending/Android text and chips.
+ const alignedStyle: StyleProp<TextStyle> = adjustVerticalAlignment ? [rest.style, { top }] : rest.style;
if (!quoteCtx) {
return (
<UITextView
@@ -517,6 +531,7 @@ function MarkdownSelectableText({
selectable
uiTextView
{...rest}
+ style={alignedStyle}
/>
);
}
@@ -526,6 +541,7 @@ function MarkdownSelectableText({
selectable
uiTextView
{...rest}
+ style={alignedStyle}
onTextLayout={(e) => {
// uitextview 自定义 spec 的 lines 是 string[](RN 核心 Text 是对象数组),
// 双形态防御:确保缓存的是纯字符串行。
@@ -5087,6 +5103,9 @@ function MarkdownBody({
return (
<MarkdownSelectableText
allowIosUITextView={allowIosUITextView}
+ alignmentTextStyles={group.blocks.map((block) => block.type === 'heading'
+ ? headingSizeStyle(styles, block.level)
+ : styles.messageText)}
key={`${group.key}:${pinSettledWidth ? contentWidth : 'hug'}`}
selectable={runSelectable}
style={settledTextStyle}
@@ -5236,6 +5255,7 @@ function MarkdownBody({
const cell = block.header[index] ?? [];
return (
<MarkdownSelectableText
+ adjustVerticalAlignment={false}
allowIosUITextView={allowIosUITextView}
key={`${block.key}:th:${index}`}
selectable={inlinesSelectable(cell)}
@@ -5256,6 +5276,7 @@ function MarkdownBody({
const cell = row.cells[index] ?? [];
return (
<MarkdownSelectableText
+ adjustVerticalAlignment={false}
allowIosUITextView={allowIosUITextView}
key={`${row.key}:td:${index}`}
selectable={inlinesSelectable(cell)}
diff --git a/apps/mobile/src/session/selectableTextAlignment.ts b/apps/mobile/src/session/selectableTextAlignment.ts
new file mode 100644
index 0000000000..8ccb9dce67
--- /dev/null
+++ b/apps/mobile/src/session/selectableTextAlignment.ts
@@ -0,0 +1,30 @@
+export interface SelectableTextTypography {
+ fontSize?: number;
+ lineHeight?: number;
+}
+
+/**
+ * Optical correction for the existing iOS UITextView renderer. Its fixed line
+ * height leaves the extra leading above the glyphs. Move the view, not the
+ * line boxes, so wrapping, list measurements and native selection stay intact.
+ * iOS system fonts have a natural line height of approximately 1.2 × font size;
+ * this is a JS layout correction, not an exact replacement for UIFont metrics.
+ */
+export function selectableTextVerticalOffset(
+ typography: readonly SelectableTextTypography[],
+ fontScale: number,
+): number {
+ let maximumFontSize = 0;
+ let maximumLineHeight = 0;
+ for (const style of typography) {
+ if (Number.isFinite(style.fontSize) && (style.fontSize ?? 0) > 0) {
+ maximumFontSize = Math.max(maximumFontSize, style.fontSize!);
+ }
+ if (Number.isFinite(style.lineHeight) && (style.lineHeight ?? 0) > 0) {
+ maximumLineHeight = Math.max(maximumLineHeight, style.lineHeight!);
+ }
+ }
+ if (!maximumFontSize || !maximumLineHeight) return 0;
+ const scale = Number.isFinite(fontScale) && fontScale > 0 ? fontScale : 1;
+ return -Math.max(0, maximumLineHeight - maximumFontSize * 1.2) * scale / 2;
+}