Skip to content

Make TextStyle react to Dynamic Type changes - #70

Open
janpollak wants to merge 2 commits into
mainfrom
fix/text-style-dynamic-type
Open

janpollak wants to merge 2 commits into
mainfrom
fix/text-style-dynamic-type

Conversation

@janpollak

@janpollak janpollak commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

TextStyle bakes the scaled point size into the Font it returns, so text styled with .textStyle(_:) does not resize when the user changes their text size. It catches up only the next time the view's body is re-evaluated for some other reason — on a static screen, possibly never.

.system(size: scaling.fontMetrics?.scaledValue(for: size) ?? size, weight: weight, design: design)

UIFontMetrics.scaledValue(for:) reads UITraitCollection.current at call time and returns a plain number, so the Font is fixed-size and SwiftUI records no dependency on \.dynamicTypeSize. Same for lineSpacing and kerning. .custom font types are unaffected — Font.custom is resolved by SwiftUI at render time.

Change

  • font, uiFont, lineSpacing and kerning gain …(for: DynamicTypeSize?) counterparts. nil resolves against the current trait collection, which is what the existing computed properties now call, so their behaviour is unchanged.
  • textStyle(_:) becomes a ViewModifier that reads \.dynamicTypeSize and passes it in.
  • Added textStyleText(_:dynamicTypeSize:), since Text cannot read the environment and had no way to react at all.

For adopters

.dynamicTypeSize(…) clamps now actually apply to TextStyle, so screens that clamp will change appearance.

TextStyle baked the scaled point size into the returned Font via
UIFontMetrics.scaledValue(for:), which reads UITraitCollection.current
at call time. SwiftUI therefore saw a fixed-size font with no dependency
on \.dynamicTypeSize and never invalidated the view when the user changed
their text size - the new size only appeared once the body happened to be
re-evaluated for some other reason.

Resolve font, uiFont, lineSpacing and kerning against an explicit
DynamicTypeSize, and turn textStyle(_:) into a ViewModifier that reads it
from the environment. The existing computed properties keep their
signature and behaviour by resolving against the current trait collection.
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.

1 participant