iPhone的stretchableImageWithLeftCapWidth只产生“D”

发布于 2024-09-03 09:40:13 字数 341 浏览 12 评论 0原文

UIImage *aImage = [[UIImage imageNamed:@"Gray_Button.png"] stretchableImageWithLeftCapWidth:25 topCapHeight:0];

尝试制作“玻璃药丸纽扣”。

如果图像更大……而我尝试使用它的按钮……更小,“拉伸”会做什么?

图像会“拉伸”和“收缩”吗?

我问的原因...是因为我所有的图像最终看起来都像“D”形状。

左侧是方形的...右侧是圆形的。

D 形会告诉你什么我做错了? 太多...或太少...“leftCap 设置”? 图片太大?

UIImage *aImage = [[UIImage imageNamed:@"Gray_Button.png"] stretchableImageWithLeftCapWidth:25 topCapHeight:0];

Trying to make a "glass pill button".

What does "stretch" do if the image is bigger... and the button I'm trying to use it on... is smaller?

Does the image 'stretch' and 'shrink'?

The reason I ask... is because all my images end up look like a "D" shape.

The left side is squared-off... and the right side is rounded.

What would a D-shape tell you that I'm doing wrong?
Too much.. or too little... "leftCap setting"?
Too large an image?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橪书 2024-09-10 09:40:13

如果 leftCapWidthtopCapHeight 均非零,则可拉伸图像将分为 9 个部分。

     leftCapWidth
      <----->
      +--------------+ ^
      |     | |      | |
      |  A  | |  B   | | topCapHeight
      |-----+·+------| v
      |-----+·+------|
      |  C  | |  D   |
      |     | |      |
      +--------------+

中心部分的大小始终为 1 px,这是被拉伸的部分,例如:

     leftCapWidth (constant!)
      <----->
      +------------------+ ^
      |     |     |      | |
      |  A  |     |  B   | | topCapHeight (constant!)
    v |-----+ - - +------| v
    | |     .     .      |
    | |     .     .      |
    ^ |-----+ - - +------|
      |  C  |     |  D   |
      |     |     |      |
      +------------------+
            >-----<
        stretched region

要创建“玻璃药丸按钮”,您应该将圆形边框填充到上面的区域 A、B、C 和 D 中,然后将该药丸的半径提供给 leftCapWidthtopCapHeight


此外,可拉伸图像不可收缩。如果您尝试使用比您想要应用的按钮(或其他任何东西)更大的可拉伸图像,它们可能会被错误地渲染(尤其是在较旧的 iOS 上)。

A stretchable image is divided into 9 parts, if both leftCapWidth and topCapHeight are nonzero.

     leftCapWidth
      <----->
      +--------------+ ^
      |     | |      | |
      |  A  | |  B   | | topCapHeight
      |-----+·+------| v
      |-----+·+------|
      |  C  | |  D   |
      |     | |      |
      +--------------+

The central parts are always 1 px in size, and this is the part that is stretched, for example:

     leftCapWidth (constant!)
      <----->
      +------------------+ ^
      |     |     |      | |
      |  A  |     |  B   | | topCapHeight (constant!)
    v |-----+ - - +------| v
    | |     .     .      |
    | |     .     .      |
    ^ |-----+ - - +------|
      |  C  |     |  D   |
      |     |     |      |
      +------------------+
            >-----<
        stretched region

To create a "glass pill button", you should fill the rounded border into the regions A, B, C and D above, and supply the radius of that pill into both the leftCapWidth and topCapHeight.


Also, a stretchable image is not shrinkable. If you try to use stretchable images bigger than the buttons (or whatever else) you want to apply it to, they may be rendered incorrectly (especially on older iOSes).

谎言 2024-09-10 09:40:13

stretchableImageWithLeftCapWidth 的行为与您预期的不同。我建议使用 UIView 的 contentStretch 属性来避免一些挫败感。

仅供参考,stretchableImage 的行为方式是:

  • 如果缩小图像,它只会剪切图像的顶部和左侧部分,而不是拉伸任何内容。
  • 如果扩展图像,它将仅扩展顶盖下方的像素列和左盖右侧的像素行。这适用于非常简单的图形,但对于任何具有阴影或光泽等效果的图形来说会显得很尴尬。

另一方面,contentStretch 的行为正如您所期望的那样。该图形基本上分为 9 个部分,按照肯尼解释的方式扩展和收缩。

另外,不要忘记,无论何时拉伸视图,其 contentMode 都应设置为 UIViewContentModeScaleToFill。

stretchableImageWithLeftCapWidth does not behave as you would expect. I suggest using UIView's contentStretch property instead to save yourself some frustration.

FYI, the way stretchableImage behaves is:

  • if you shrink the image, it just clips the top and left parts of the image rather than stretching anything.
  • if you expand the image, it will expand only the column of pixels just beneath the top cap and the row of pixels just right of the left cap. This works for very simple graphics but will look awkward for any graphics that have effects like shading or gloss.

contentStretch, on the other hand, behaves just as you would expect. The graphic basically gets split into 9 parts that expand AND CONTRACT the way Kenny explained.

Also, don't forget that anytime you stretch a view, its contentMode should be set to UIViewContentModeScaleToFill.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文