Im trying to create a vertical Text with a vertical line above it like so:
where it says ADVERTORIAL
The closest i can get to at the moment is below, but the advertorial view isnt being pushed all the way to the right of the view and isnt pushed towards the top, i assume this is because the views are rotated after being drawn.
Here is the view debugger:
And here is the view code:
struct ArticleHeaderView<ViewModel: ArticleHeaderViewModeling & ObservableObject>: View {
let viewModel: ViewModel
var body: some View {
HStack {
VStack(alignment: .leading) {
titleText()
dateText()
if viewModel.isShowingAuthorText {
authorText()
}
}
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.trailing, 8)
verticalTextView()
}
.frame(maxWidth: .infinity, alignment: .leading)
}
// MARK: Private
private func titleText() -> some View {
Text(viewModel.titleText)
.font(Font.custom("Suisse Int'l Mono", size: 26))
.foregroundColor(.black)
.padding(.top, 40)
}
private func dateText() -> some View {
Text(viewModel.dateText)
.font(Font.custom("Suisse Int'l Mono", size: 12))
.foregroundColor(.black)
.lineSpacing(16)
.padding(.top, .Spacer.md)
}
private func authorText() -> some View {
Text(viewModel.authorText)
.font(Font.custom("Suisse Int'l Mono", size: 12))
.foregroundColor(.black)
.lineSpacing(16)
.padding(.top, .Spacer.md)
}
private func verticalTextView() -> some View {
HStack() {
Rectangle()
.fill(.gray)
.rotationEffect(Angle(degrees: 90))
.frame(maxWidth: 1, maxHeight: 16)
.padding(.trailing, 8)
Text(viewModel.verticalText)
.font(Font.custom("Suisse Int'l Mono", size: 12))
.fixedSize()
.foregroundColor(.black)
}
.fixedSize()
.rotationEffect(Angle(degrees: 90))
}
}
1
Understanding what’s happening
When you’re trying to work out what is going on with a rotated view, it can help to add a colored border around it.
This is what you see when you add .border(.blue)
to the parent HStack
and .border(.red)
at the end of verticalTextView()
:
So the position of verticalTextView
is vertically centered on the HStack
, then it is being rotated around its own center. Notice too how the title is wrapping, even though there would actually be space available to show the title without wrapping. This is because, the footprint for the vertical text (shown by the red border) is reducing the space available in the HStack
.
Addressing the issues
I assume that the vertical text (including line) should have the same height as the VStack
. To find this height, the vertical text can be shown as an overlay over the VStack
, wrapped with a GeometryReader
. This height is then set as the maximum width on the vertical text, before rotation.
By showing the vertical text as an overlay over the VStack
, it resolves the issue of horizontal space being taken away from the title. It also means, the HStack
is no longer needed. However, you might want to increase the trailing padding on the VStack
, to make sure there is always enough space for showing the vertical text (for the case of when a long title would otherwise extend across the full width of the VStack
).
Also, I assume that the line above the text should fill the space to the top of the VStack
. So instead of rotating the line separately, just let it use all the space available and rotate it with the rest of the view. The fixedSize
modifier needs to be removed, so that the line extends to the full width of the frame that is applied to it.
Changes to verticalTextView
Here is the updated function verticalTextView
. This includes some other small changes:
- Use the
spacing
of theHStack
to determine how much space there should be between the line and the text. - Apply
.lineLimit(1)
to the text, to prevent it from wrapping. - Apply
.layoutPriority(1)
to the text, so that the text takes priority over theRectangle
. - The rotation effect has been removed, because it needs to be applied after the width has been set.
- The modifier
.foregroundColor
is deprecated, use.foregroundStyle
instead.
private func verticalTextView() -> some View {
HStack(spacing: 10) {
Rectangle()
.fill(.gray)
.frame(height: 1)
Text(viewModel.verticalText)
.font(.custom("Suisse Int'l Mono", size: 12))
.foregroundStyle(.black)
.lineLimit(1)
.layoutPriority(1)
}
.border(.red)
}
Rotating and positioning the vertical text
So the plan is to show the vertical text as an overlay over the VStack
, wrapped with a GeometryReader
(to measure the size of the underlying VStack
).
-
A
GeometryReader
lays out its content with alignment.topLeading
, so the vertical text (which is still laid out horizontally at this point) will start off in the top-left corner. -
Use
anchor: .topLeading
for the rotation effect. This means, the view swivels around its top-left corner, instead of around its center. -
Due to the
.topLeading
anchor, the rotation will now cause the view to disappear off-screen on the left side. -
To bring it back into view, and indeed to move it to the trailing side of the overlay, an x-offset equal to the width of the
VStack
needs to be applied.
Putting it all together
Here is the updated main body:
var body: some View {
VStack(alignment: .leading) {
titleText()
dateText()
if viewModel.isShowingAuthorText {
authorText()
}
}
.padding(.trailing, 20)
.frame(maxWidth: .infinity, alignment: .leading)
.overlay {
GeometryReader { proxy in
verticalTextView()
.frame(maxWidth: proxy.size.height, alignment: .leading)
.rotationEffect(Angle(degrees: 90), anchor: .topLeading)
.offset(x: proxy.size.width)
}
}
.border(.blue)
}
Here’s how it looks:
If you want the vertical line to align with the top of the title, instead of with the top of the VStack
, then just remove the top padding from the titleText
and apply it to the VStack
instead, after the overlay:
If in fact the vertical line should not fill the height of the VStack
but should instead have a fixed maximum size, this can be applied to the Rectangle
in verticalTextView
:
Rectangle()
.fill(.gray)
.frame(height: 1)
.frame(maxWidth: 20) // 👈 here