Skip to content

Conversation

@nguillot
Copy link
Contributor

Summary

Implements intelligent multi-line text wrapping with proper width constraint enforcement and international language support. The implementation uses Core Text's Unicode Line Breaking Algorithm with a conservative validation layer to protect numbers from being split across lines.

Problem

Previously, when setting preferredMaxLayoutWidth on MTMathUILabel, long text would either:

  1. Overflow beyond the width constraint - Text like "Rappelons la conversion : 1 km équivaut à 1000 m." would render at 262pt even with a 235pt
    constraint, causing truncation
  2. Break incorrectly - Simple space-based breaking didn't handle:
    - CJK languages (Chinese, Japanese) where words aren't space-separated
    - Number formats across locales (French 3,14, English 1,000,000, French 1 000 000)
    - Complex grapheme clusters (emoji, combining characters)

Example of the bug:

Constraint: 235pt
Actual width: 262.786pt
Rendered: "Rappelons la conversion : 1 km équi[TRUNCATED]"
expected: "Rappelons la conversion : 1 km équivaut à 1000 m."

Implementation Details

Number Protection Patterns

The implementation conservatively protects these number formats:

Format Example Protected? Use Case
French decimal 3,14 European decimals
English decimal 3.14 US/UK decimals
Thousands (comma) 1,000,000 English numbers
French thousands 1 000 000 French numbers
Swiss thousands 1'000'000 Swiss numbers
Scientific notation 6.022e23 Scientific text

Wrapping Behavior

CJK Languages:

Chinese: Can break between most characters
Japanese: Follows kinsoku rules (禁則処理)
Korean: Breaks at spaces (like English)

Latin Languages:

English/French/Spanish/German: Break at word boundaries (spaces)
Respects accented characters: é, ñ, ü, etc.

Mixed Content:

"The equation is 方程式: x² + y² = r²"
→ Can break between scripts intelligently

@mgriebling mgriebling merged commit 51b3560 into mgriebling:main Nov 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants