0

I'm working on custom keyboard for iOS 8. And i have image for key buttons background. For example lets take this one -> enter image description here

In future this image will have rounded corners. The problem is with space button if a'm adding this image for background image image will be resized and rounded corners woun't be as i whant (because space button is too long). So i'v decided to add capinsets. In this case picture is 64x64 and capinsets is 2 pixel from top, 2 pixel from left, 2 from bottom and right side. Because in Xcode capinsets is calculating from center i'v writen such code(in this case borderSize == 0):

private func imageWithCapinsetsForImage(initalImage: UIImage) -> UIImage {
    let halfOfImageWidth = currentImage.size.width / 2
    let halfOfImageHeight = currentImage.size.height / 2
    let capinsetsCalculatedFromImageCenter = UIEdgeInsets(top: halfOfImageHeight - capinsets.top - borderSize, left: halfOfImageWidth - capinsets.left - borderSize, bottom: halfOfImageHeight - capinsets.bottom - borderSize, right: halfOfImageWidth - capinsets.right - borderSize)
    let imageWithCapinsets = initalImage.resizableImageWithCapInsets(capinsetsCalculatedFromImageCenter)
    return imageWithCapinsets
}

It's code in Swift language, in Objective-C the same problem. When i add such capinsets in space button background (the longest button) that center line betwin gray and black color isn't at center any more(the longer button - the lower that line is). Who knows why? And how can i fix it? And screen:enter image description here If i'm working with such code:

private func imageWithCapinsetsForImage(initalImage: UIImage) -> UIImage {
        let capinsetsCalculatedFromImageCenter = UIEdgeInsets(top: capinsets.top + borderSize, left: capinsets.left + borderSize, bottom: capinsets.bottom + borderSize, right: capinsets.right + borderSize)
        let imageWithCapinsets = initalImage.resizableImageWithCapInsets(capinsetsCalculatedFromImageCenter)
        return imageWithCapinsets
    }

i'm getting such such result enter image description here

Vasyl Khmil
  • 2,548
  • 1
  • 20
  • 36
  • What does the image look like? Post a screenshot of the result you get. – duci9y Jul 26 '14 at 11:46
  • i'm not on my mac computer but i'v made by my self what it is like. Alos i saw that capinsets is not 1 pixel, but 2 pixels, can problem be in this thing? – Vasyl Khmil Jul 26 '14 at 12:17
  • Post the original image, the context you use the image in, and a screenshot of the actual outcome you get. – duci9y Jul 26 '14 at 12:21
  • i'v added changes you want, but i can't post the context because there are too much code to understand where and how i call that functions. – Vasyl Khmil Jul 28 '14 at 06:29

1 Answers1

0

Cap insets are not calculated from the center. They are calculated from the sides. The cap insets define the size from each side that you don't want the image to be stretched. In your case, I think that you simply want:

UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1)
drewag
  • 93,393
  • 28
  • 139
  • 128