Can Text fit perfectly into a Shape? - swiftui

I'm trying to work our if it's possible to display text in a Shape so that the text it not rectangular but the same as the shape it's self.
I have tried
Text(loremIpsum)
.font(Font.system(size: 14))
.clipShape(Circle())
which the result was that I'm not seeing new lines when text reaches the edge of the shape.
I then tried to use the circle first and add text to that but that resulted in text appearing outside the circle.
Circle()
.stroke(Color.black, lineWidth: 2)
.padding(6)
.overlay(
Text(loremIpsum)
.font(Font.system(size: 14))
)
I'm wanting to achieve Text that fits perfectly into whatever Shape I use. Is this possible?
The question suggested here does not answer my question sadly. hat just fits text into a Shape but still retains a rectangular frame. I'm trying to make text exactly match the shape I am placing it in.

Related

Unicode char has odd gap when rendered in view

I am using the following code to show a playing card. Not sure why there is a large gap at the top.
This is on iOS 15.4
var body: some View {
Text(String(Character(unicodeScalarLiteral: "\u{1F0A1}")))
.font(.system(size: size))
.foregroundColor(.red)
}
Compare to how this smiley is displayed:
This is how this symbol layout within glyph box (pay attention on its position relative to base line):
and here is for comparison of Latin A:
and really if both in one Text
Probably you just need different symbol/image, or try to use some manual workaround like
Text("\u{1F0A1}")
.baselineOffset(20)

SwiftUI transparent png becomes white (jpg)

I have a png which is partically transparent, namely it has alpha channel. But when I used the following code, the transparent becomes white. I guess the alpha channel is missing. Do you know how to solve this problem? Thank you.
This is the reproducible code.
Image("power")
.resizable()
.aspectRatio(contentMode: .fit)
This is the image
This is the effect on simulator
I used this code to manage to show the transparent effect, but foreground colors are lost.
Color.black
.frame(width: home.screenWidth-2*home.defaultPadding,
height: home.screenWidth)
.mask(Image("power")
.resizable()
.aspectRatio(contentMode: .fit))
Reason
Thank you for your helps so much!!!! Without your support, I cann't find the reason.
The compression for the png should be automatic, my project is inherited(basic) somehow, that's why it rendered white.
The code and image has no problem.
That's what should happen. You've got an image with an alpha channel over a white background, so you can't see any of the white elements in your image. Change your Color Scheme to dark with .preferredColorScheme(.dark) and you'll see this:

Images imported from ImagePicker being stretched

I've encountered what I think is a bug, and I'm wondering if anyone else has encountered this, and/or has a work around for it.
I have images imported from the device's camera via the UIImagePickerController representable. Having imported this image, I then save it to the documents directory, and then display it using the following code:
Image(uiImage: image)
.resizable()
.frame(height: 300)
.scaledToFill()
This shouldn't cause the image to distort, as scaled to fill should simply enlarge the image until it fits the frame without distorting it. However, i'm getting a fair amount of horizontal stretching in the final image:
Has anyone encountered this problem? I don't think i'm missing anything obvious, as when I use it for images not taken with the camera then the code performs fine.
Try changing the order of the modifiers as below,
Image(uiImage: UIImage(named: "08-512")!)
.resizable()
.scaledToFill()
.frame(width: 50, height: 20)
//.clipped()
}

SwiftUI scaleEffect and layout bounds

I want to create a Text view with slighter larger than normal font. I tried,
Text("Foo").scaleEffect(1.3)
The text is larger as desired, but the layout uses the original borders, so the "Foo" is going off the bottom of the screen. Is there a way make it use the new larger bounds for layout?
I know there are named fonts, so I could do:
Text("Foo").font(.title)
but I wanted to say "1.3 times bigger than .body".
Here is possible approach
Text("Hello")
.font(Font.system(size: UIFont.systemFontSize * 1.3))

Circle not centered - expected something else

i have this code. The circle is not centered. I assume because of the edgesIgnoring...and yes, if i comment it out, it is centered. But if i comment out the "scaledToFill" it is centered too, although the edgesIgnoring is in...is this a bug or am i misunderstanding something? I tested on an iPhoneX Preview...
struct ContentView: View {
var body: some View {
Circle()
.scaledToFill()
.edgesIgnoringSafeArea(.all)
}
}
I try to propose solution (one might say it is workaround - does not matter), for some cases it might be acceptable, because both centred properly, so worth posting:
Note: order of modifiers important!
1) approach 1
Ellipse()
.edgesIgnoringSafeArea(.all)
.scaledToFill()
2) approach 2
Circle()
.edgesIgnoringSafeArea(.all)
.scaledToFill()
Short answer, yes, it's a bug. The circle's size is being calculated including the safe area, but the circle's horizontal offset is being calculated to center it assuming the size it would have not including the safe area. Both circles have their left edge at approximately -204 pixels on an iPhone 11, which leaves the larger one uncentered.