Swift Ui: Center Text into Circle

Swift UI: Center Text into Circle

.overlay already has size of container with Text an Circle centred in it, so it is just needed to operate with insets of circle not shifting it, like below

demo

Text(day.name!)
.frame(width: 35, height: 35, alignment: .center)
.padding()
.overlay(
Circle()
.stroke(Color.orange, lineWidth: 4)
.padding(6)
)

How to center text inside a round button using swiftUI?

Just use center alignment in your .frame, ie

}
.padding(.all)
.accentColor(.white)
.frame(width: 100, height: 100, alignment: .center) // << here!

SwiftUI: Make Circle fit the Text element

I am going to post this as it is a different take on the other two answers, which are simpler and do work. This is a bit more flexible in handling different sizes, but still making them look consistent. This uses a PreferenceKey to read the size of the initials and restrict the circle to a certain size as a result.

struct EntryHeaderIconView: View {
@State var textViewSize = CGFloat.zero
// This gives a consistent additional size of the circle around the intitials
let circleSizeMultiplier = 1.5
// To give it a minimum size, just have the size introduced here
let minimumSize: CGfloat

var backgroundSize: CGFloat {
min(textViewSize * circleSizeMultiplier, minimumSize)
}

private let color: Color
private let initials: String

init(color: Color,
initials: String = "") {
self.color = color
self.initials = initials
}

var body: some View {
ZStack(alignment: .center) {
Circle()
// The frame is sized at the circleSizeMultiplier times the
// largest dimension of the initials.
.frame(width: backgroundSize
height: backgroundSize)
.foregroundColor(color)
icon
// This reads the size of the initials
.background(GeometryReader { geometry in
Color.clear.preference(
key: SizePreferenceKey.self,
value: geometry.size
)
})
}
// this sets backgroundSize to be the max value of the width or height
.onPreferenceChange(SizePreferenceKey.self) {
textViewSize = max($0.width, $0.height)
}
}

@ViewBuilder
private var icon: some View {
Text(verbatim: initials)
.font(.system(size: 48, weight: .bold, design: .rounded))
.foregroundColor(.white)
.accessibilityIdentifier("entry_header_initials")
}
}
// This is the actual preferenceKey that makes it work.
fileprivate struct SizePreferenceKey: PreferenceKey {
static var defaultValue: CGSize = .zero
static func reduce(value: inout CGSize, nextValue: () -> CGSize) {}
}

Edit:

Unless the icon is a SF Symbol, it will have to be handled differently. However, I have updated the code to add a minimumSize constant and changed backgroundSize to be a computed variable. This view is not set up to handle an image, but you would simply need to determine how you wanted to constrain the image, or do something like this.

Draw a circle around Text in SwiftUI

You can look at this way:

struct ContentView: View {

var body: some View {

CircledText(text: "Hello, world!")
.border(Color.orange)

}

}


struct CircledText: View {

let text: String
@State private var radius: CGFloat = .zero

var body: some View {

return ZStack {

Text(text)
.padding()
.background(GeometryReader { proxy in Color.clear.onAppear() { radius = max(proxy.size.width, proxy.size.height) } }.hidden())

if (!radius.isZero) {

Circle().strokeBorder().frame(width: radius, height: radius)

}

}

}
}

Sample Image

How to position plus sign right in the middle of circle

use Image(systemName: "plus").foregroundColor(.white) instead of Text("+")

In text "+" symbol doesn't have to be in the middle of the view because of text layout.

SFSymbols are more convenient in this regard. Also you can specify size with .font(.system(size: 10))

Center alignment text inside of Text in SwiftUI

You should use the .multilineTextAlignment(_:) method on the Text element.

This works fine for me:

Text("[...]")
.lineLimit(nil)
.multilineTextAlignment(.center)
.padding(.horizontal, 40)

How to align text at the bottom in SwiftUI

You can use VStack with Spacer() and set frame for HStack()

struct TestView: View {
var body: some View {
HStack() {
Text("09")
DotView()
.frame(width: 7, height: 30, alignment: .center)
Text("22")
VStack { // Added VStack with Spacer()
Spacer()
Text("PM")
.font(.system(size: 20))
}
}
.frame(width: 300, height: 55) // Added Line, adjust frame of HStack
.font(.system(size: 66, weight: .medium))
}
}

Sample Image

How to align text sub-views in VStack?

Here is the simplest way that is possible. You must to have a size for Image.

PS: You should not use Image as parameter for your view, just use the string of image. I corrected for you.

struct ContentView: View {
var body: some View {
VStack {
BulletPointView(string: "square.and.pencil")
BulletPointView(string: "hare.fill")
BulletPointView(string: "circle.fill")
BulletPointView(string: "car.2.fill")
BulletPointView(string: "switch.2")
BulletPointView(string: "swiftPunk")
}
.padding()
}
}

struct BulletPointView: View {
let title: String
let string: String
let text : String

init(title: String = "New feature",
string: String,
text: String = "This is a new feature for this app. And this text should wrap.") {
self.title = title
self.string = string
self.text = text
}

var body: some View {
HStack (alignment: .center){

imageFunction(string: string)
.scaledToFit()
.frame(width: 50, height: 50)

VStack (alignment: .leading, spacing: 4){
Text(title)
.fontWeight(.semibold)
Text(text)
.foregroundColor(.secondary)
}
.multilineTextAlignment(.leading)
.font(.subheadline)
.padding(.bottom, 6)

}
}

@ViewBuilder func imageFunction(string: String) -> some View {

if (UIImage(systemName: string) != nil) {
Image(systemName: string)
.font(.title)
}
else {
Image(string)
.resizable()
}

}

}

Sample Image



Related Topics



Leave a reply



Submit