ios - 带有左+右大写字母和中间图案的 UIButton

标签 ios cocoa-touch uibutton

如何创建一个 UIButton ,其背景图像由以下内容组成:

  1. 固定的上限
  2. 固定的上限
  3. 许多中间图像依次放置以填充所有可用空间

像下面的例子一样吗?

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/

相关文章:

iphone - 如何在仍然按住手指的同时从使用一个 UIButton 切换到另一个?

ios - 为单个区域设置与其他区域不同的 IAP 定价

ios - 真实设备上的月份和星期几语言

iphone - 如何在 iPad 上运行 iPod 应用程序

ios - xcode 将不同的最大字符长度设置为不同的 TextField

ios - 嵌入导航 Controller 后 View 不会旋转

ios - 在 cellForRowAtIndexPath 方法中设置自定义单元格的正确方法?

ios - 防止粘性 UICollectionView 部分标题被部分页脚推高

ios - 如何在不提供意图或使用快捷方式的情况下利用 Siri 创建自定义对象?

ios - 如何在 UITableView 中实现 Expandable - Collapsable UITableViewCell - IOS