Reputation: 1784
I am trying to find a way to detect the right edge of a png and add a label to it (like text "AB" in the screenshot). The current way I am doing this is by adding a label on top of the image and then setting its left, right and top constraints.
Now this method works fine for 1 particular shoe, but if I add more shoes of different shapes and sizes then it obviously doesn't work and the label position isn't where I want it to be.
So is there anyway I can detect the right edge of the png image? So I can set the constraints relative to the image itself and not the image view? Not sure if that is possible.
Thank you
Upvotes: 0
Views: 150
Reputation: 1784
Unfortunately the shoes were quite different in shape so any code-only solution was not working well.
So I asked the designer to add a place holder text and then detected the frame using ML Kit. Then added my own label on top of the image.
Upvotes: 0
Reputation: 162
You can get image size itself from here. Then you can get aspect ratio of image itself, aspect ratio of UIImageView
(using .bounds
property) and perform some geometry calculations.
UPDATE: Code that will help you:
let imageSize = yourImage.size
let imageViewSize = yourImageView.bounds.size
let imageAspectRatio = imageSize.height / imageSize.width
let imageViewAspectRatio = imageViewSize.height / imageViewSize.width
var rightOffset: CGFloat = 0
if imageAspectRatio > imageViewAspectRatio {
let scaleCoef = imageViewSize.height / imageSize.height
let scaledWidth = imageSize.width * scaleCoef
rightOffset = (imageViewSize.width - scaledWidth) / 2
}
Upvotes: 1