Tuesday, June 11, 2024
HomeiOS Developmentios - The best way to centrally align one character inside a...

ios – The best way to centrally align one character inside a string in a body/view?


I might wish to centrally align the numerical digit inside a body, in order that the signal is ignored.
E.g. In a round form, I might wish to align the string “+9” in order that the “9” is dead-centre and never the place between the “+” and the “9”.

I used a ZSTACK to align the digit and the signal individually, however that is messy and fails when rendering on completely different units with completely different display screen estates, in addition to Dynamic Sort.

Right here is instance code – the purple circle is with default alignment, the inexperienced with an approximate algorithm.

                let diam = 75.0
        
        ZStack {
            Rectangle()
                            .body(width: 1, top: .infinity)
                            .foregroundColor(.yellow)
            
            // Aproximately right!
            VStack {
                
                ForEach(9..<11) { i in
                    ZStack {
                        Circle()
                            .body(width: diam, top: diam)
                            .foregroundColor(.inexperienced)
                        
                        Textual content("(i)")
                        Textual content("+").font(.title)
                            .body(width: diam * 0.75, top: diam * 0.75, alignment: .main)
                    }
                    .foregroundColor(  .black).font(.title)
                }
                // Unsuitable!
                ZStack {
                    Circle()
                        .body(width: diam, top: diam)
                        .foregroundColor(.purple)
                    
                    Textual content("+23")
                }
                .foregroundColor(  .black).font(.title)
            }

        }

enter image description here

There needs to be a greater resolution.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments