2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Textに対してlineLimit(_:reservesSpace: true)とlineSpacingを同時に使ったときのバグ

Posted at

検証環境: iOS 17.2 + Xcode 15.2

Textに対してlineLimit(_:reservesSpace: true)lineSpacingを同時に使ったときのバグがある。
reserveされた空白行にはlineSpacingが適用されず、高さが揃わないのである。
これに対するワークアラウンドとして、非常に単純であるが以下のようなものが考えられる。

import SwiftUI

public struct SpaceReservingMultilineText: View {
    var text: String
    var lineLimit: Int
    var lineSpacing: CGFloat
    public var body: some View {
        ZStack(alignment: .top) {
            Text((0..<lineLimit - 1).map { _ in "\n" }.joined())
                .opacity(0)
            Text(text)
                .lineLimit(lineLimit, reservesSpace: true)
        }
        .lineSpacing(lineSpacing)
    }
}

使うときは以下のように属性を外からまとめて指定することができる。

SpaceReservingMultilineText(text: ..., lineLimit: 3)
    .font(...)
    .foregroundColor(...)

もちろん将来的に修正されたら以下のように書けるはずなので、気長に待とう。

Text(text)
    .font(...)
    .foregroundColor(...)
    .lineLimit(3, reservesSpace: true)
    .lineSpacing(5)
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?