I would like to know how to enter characters in the position specified for one image in Swift.
The image you want to create is a stylish resume (put your name, age, image, etc. in a cute image).
The current situation is that in the first place,
● Prepare one image and place the characters on it using ZStack etc.
●Start by programming the image itself by specifying the background color, size, etc.
I'm a beginner who doesn't know which is better.
Currently, we are making trial and error using the first method of preparing a single image and putting characters on it using ZStack etc.
The liver of the code I'm writing now is
Zstack{
Image ("Image File")
.resizable()
.aspectRation(contentMode:.fit)
Text ("Character Input 1")
.offset (x:-160, y:-80)
Text ("Character Input 2" )
.offset (x:-160, y:-35)
}
In this case, characters are entered in the position you want to fill in, but if you turn the screen sideways or change the size of the iPhone you use, the position will be shifted due to the screen coordinates.
Therefore, I wonder if I need to specify the coordinates of the image itself, but I can't get to the information well even if I search for it.
Also, I don't want to change the size of the image itself (I want to keep the original image size after I finish making it), but what kind of framework or code should I try or search for?
I would appreciate it if you could let me know.
Thank you for your cooperation.
swift
It is difficult to specify coordinates, so I made it possible to change the position of the text in DragGesture and made the View itself into an image.
struct ContentView:View {
@State private vardragAmount—CGPoint?
varimageView:someView {
let image : UIImage=editor.snapshot()
return Image (uiImage: image)
}
vendor:someView {
Image(systemName: "heart")
.resizable()
.frame (width: 100, height: 100, alignment: .center)
.overlay{
Text ("hello")
.position(self.dragAmount??.zero)
.gesture(DragGesture().onChanged {self.dragAmount=$0.location})
}
}
varbody:someView {
VStack {
editor
Text ("Below is the editor image and then View")
imageView
}
}
}
extension View {
func snapshot() - > UIImage {
let controller = UIHostingController (rootView:self)
let view = controller.view
let targetSize = controller.view.intrinsicContentSize
view?.bound=CGRect(origin:.zero, size:targetSize)
view?.backgroundColor=.clear
let render = UIGraphicsImageRender (size: targetSize)
return render.image{_in
view?.drawHierarchy (in:controller.view.bounds, afterScreenUpdates:true)
}
}
}
© 2024 OneMinuteCode. All rights reserved.