Aligning Vertical text with Rectangle above it

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 the HStack 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 the Rectangle.
  • 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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật