Skip to content

[Text] Allow for line height *between* lines on text.  #47175

Closed
flutter-team-archive/engine
#15087

Description

@johnsonmh

Use case

This is the default behavior for TextViews on Android. The lineHeight param only applies between lines. This is useful, for example, when you want to increase the line height in your TextTheme. For multiple lines of text, this will increase the spacing between the lines of text. But, importantly, for single lines of text, they will remain unaffected. This means that if you have single line of text that is vertically aligned with a button or a switch, the text will not be affected (and appear off center).

Proposal

It would be nice to have API in TextStyle for specifying the lineHeight behavior. Maybe it could be an enum, something like:

enum LineHeightBehavior {
   betweenLinesOnly,
   aboveAndBelowLines,
   ...
}

That way on TextStyle I could do something like

TextTheme(
  body1: TextStyle(
    height: 1.2,
    lineHeightBehavior: LineHeightBehavior.betweenLinesOnly,
  ),
),

.

Activity

  1. GaryQian commented on Dec 16, 2019

    @GaryQian
    Contributor

    A simpler parameter may just be something like:

    Text(
      'blah',
      lineSpacingBetweenLinesOnly: true,
    )
    

    Of course, if there were additional enums other than these two behaviors, then an enum may make more sense. Something like this should not belong on the TextStyle though, as it is possible to have many TextStyles in a single paragraph/line/word. LibTxt and dart:ui has a concept of ParagraphStyle, which in the framework is exposed as just properties on the TextWidget itself.

  2. self-assigned this
    on Dec 16, 2019
  3. johnsonmh commented on Dec 16, 2019

    @johnsonmh
    ContributorAuthor

    Something like this should not belong on the TextStyle though, as it is possible to have many TextStyles in a single paragraph/line/word.

    The reason I think it makes sense on TextStyle is that the height parameter is set on TextStyle. So to me it makes sense to keep those parameters together. It also makes it much easier to use with the existing TextTheme and DefaultTextStyle APIs if this parameter lives on TextStyle.

    What happens when you set multiple different TextStyle.height params on a single line/word ?

  4. GaryQian commented on Dec 17, 2019

    @GaryQian
    Contributor

    What happens when you set multiple different TextStyle.height params on a single line/word ?

    The line will use the maximum ascent and descent.

    If it were on TextStyle, we would have to disambiguate what to do when there are multiples styles on the line, one with the first line having spacing, other with first line ignoring spacing.

  5. johnsonmh commented on Dec 17, 2019

    @johnsonmh
    ContributorAuthor

    Ah, I see. Would we be able to make some sort of convenient API for declaring this behavior once per app? Something at the top of our app so that users wouldn't have to say Text('foo', lineHeightBehavior: ...) every time they want to use a Text widget?

  6. added this to the April 2020 milestone on Dec 18, 2019
  7. GaryQian commented on Dec 19, 2019

    @GaryQian
    Contributor

    The property can also be added in DefaultTextStyle which will allow all the Text widgets to be set at once.

  8. GaryQian commented on Dec 19, 2019

    @GaryQian
    Contributor

    The property itself can be a maskable enum where the line spacing for different parts of the paragraph can be enabled/disabled by setting bits.

    For example something like this:

    111 => all spacing enabled
    010 => only spacing between lines
    101 => only spacing above first line and below last line
    011 => disable spacing above first line

  9. Hixie commented on Dec 20, 2019

    @Hixie
    Contributor

    I hesitate to expose the 101 case unless we have a reason to support that.

  10. GaryQian commented on Dec 30, 2019

    @GaryQian
    Contributor

    flutter-team-archive/engine#15087 Starts engine-side work

  11. GaryQian commented on Jan 10, 2020

    @GaryQian
    Contributor

    #48346 exposes this on the framework side.

  12. added
    engineflutter/engine related. See also e: labels.
    on Jan 16, 2020
  13. dnfield commented on Jan 28, 2020

    @dnfield
    Contributor

    Reopening until this lands in framework (and because I'm reverting engine change for now).

  14. GaryQian commented on Jan 28, 2020

    @GaryQian
    Contributor

    Oops, did not realize this was closed! Github autoclosed it :(

  15. kaikai4989 commented on Feb 6, 2020

    @kaikai4989

    Late to the game ... but I am wondering whether "height" is the right way to achieve line spacing in the first place because "height" is font-family-specific (each font defines its own ascent and descent to be multiplied by "height"), which means "height" becomes tricky when controlling line spacing if a line has two different fonts.

    In this kind of situation, usually people would prefer StrutStyle to control the line height, with its "leading" support. Actually, line-spacing was called "leading" in old times, see https://en.wikipedia.org/wiki/Leading for details.

    So I am wondering whether we should extend TextHeightBehavior to have similar opt-in for "leading", as it should be able to exclude the first line / last line as well.

  16. github-actions commented on Aug 23, 2021

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  17. locked as resolved and limited conversation to collaborators on Aug 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

a: typographyText rendering, possibly libtxtc: new featureNothing broken; request for a new capabilityengineflutter/engine related. See also e: labels.frameworkflutter/packages/flutter repository. See also f: labels.

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions