I'm working on custom keyboard for iOS 8. And i have image for key buttons background. For example lets take this one ->
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:
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