Skip to content
TaDev Logo TaDev
Go back

iOS - UIKit Japanese LineHeight Issue

JapaneseLineHeightLabel on iOS

1. Problem Summary

When rendering Japanese text on iOS, text can look vertically shifted (usually lower than visual center) when the configured line height is much larger than the font’s default line height.

Example:

This issue can happen for both single-line and multi-line labels because it is mostly a baseline/glyph-metrics problem, not only a wrapping problem.

2. Why It Happens

UIKit line layout depends on font metrics (ascender, descender, lineHeight) and paragraph style.

If only minimumLineHeight/maximumLineHeight is applied, the text baseline may not be visually centered for Japanese fonts.

In short:

3. Class-Based Solution

The class JapaneseLineHeightLabel solves this by:

  1. Applying fixed line height through NSMutableParagraphStyle.

  2. Computing baseline offset to center text in the target line box:

    offset = (lineHeight - font.lineHeight) / 2 + customBaselineOffset

  3. Re-applying style when key properties change:

  1. Preventing recursive loops with an internal guard (isUpdatingText).

4. How To Use

4.1 Interface Builder (Storyboard/XIB)

  1. Set label class to JapaneseLineHeightLabel.
  2. Set lineHeight (IBInspectable), for example 32.
  3. Optional: set customBaselineOffset for fine tuning.
    • Positive value: move text upward.
    • Negative value: move text downward.

4.2 Programmatic

let label = JapaneseLineHeightLabel()
label.font = .systemFont(ofSize: 16)
label.lineHeight = 32
label.customBaselineOffset = 0
label.text = "Hello World"
  1. Start with:
  1. If text still looks low:
  1. If text looks too high:

This is font-dependent, so tune per design token/font family.

6. Behavior Notes

7. Troubleshooting Checklist

If line height appears not applied:

  1. Check class mapping in XIB/Storyboard is exactly JapaneseLineHeightLabel.
  2. Ensure lineHeight > 0.
  3. Ensure label has non-empty text.
  4. Verify no later code overwrites paragraph style unexpectedly.
  5. Verify font is set before/around text assignment in complicated bindings.
  6. Use customBaselineOffset for final visual adjustment.

8. File Location


Share this post on:

Next Post
Multi-Agent tại Google I/O 2026: Giải mã Antigravity 2.0 và góc nhìn về 'Multi-Agent Thật'