如何创建一个 UIButton
,其背景图像由以下内容组成:
- 固定的左上限
- 固定的右上限
- 许多中间图像依次放置以填充所有可用空间
像下面的例子一样吗?
编辑:我没有意识到中心没有N个重复的图像,而只有一个拉伸(stretch)的图像。查看已接受的答案。
最佳答案
据我所知这是不可能的。您可以做的是拉伸(stretch)图像,但不能添加 n 个中间图像。
在其间添加可拉伸(stretch)图像的代码是
//Create an image - Where UIEdgeInsets is in top left bottom right
UIImage* buttonImage = [[UIImage imageNamed:@"button.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 16, 0, 16)];
// Create a custom buttom
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(0, 0, 100, buttonImage.size.height);
[myButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[myButton setTitle:@"Button" forState:UIControlStateNormal];
//Add it to view - if it is a view controller self.view
[self addView:myButton];
来自苹果的UIImage Class Reference :
"resizableImageWithCapInsets:
You use this method to add cap insets to an image or to change the existing cap insets of an image. [...] During scaling or resizing of the image, areas covered by a cap are not scaled or resized. Instead, the pixel area not covered by the cap in each direction is tiled, left-to-right and top-to-bottom, to resize the image." [emphasis added]
您可以在http://mobiledevelopertips.com/user-interface/ios-5-uiimage-and-resizableimagewithcapinsets.html阅读更多相关信息。
关于ios - 带有左+右大写字母和中间图案的 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11824603/