Is there a nine-patch loader for iPhone?

后端 未结 4 1911
小蘑菇
小蘑菇 2020-12-22 22:19

Android has a nice way of defining stretchable images called a nine-patch. See these docs for a description of the concept. The idea is to surround a png image with a 1-pi

相关标签:
4条回答
  • 2020-12-22 22:32

    Also look at UIView's contentStretch property. It is more robust and well-behaved than stretchableImageWithLeftCapWidth. Basically, it works by just defining the stretchable rectangle within your image and automatically creating a scaled nine-patch. This internal rectangle can be anything - it doesn't even have to be in the center of the image. Plus unlike stretchableImage this method will properly shrink graphics and behave as expected for graphics with lighting or gloss. I can't think of any real-world application where you would want more than this.

    0 讨论(0)
  • 2020-12-22 22:43

    All UIImage images support this natively. By default the entire images is stretchable, but you can set caps with the leftCapWidth and topCapHeight properties or you can generate one from an existing UIImage with the - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight method.

    Do note that in apple's implementation, when you set one or both of these values, the stretchable area is forced to be a single pixel high/wide.

    0 讨论(0)
  • 2020-12-22 22:49

    Yes UIImage does support something like it. See

    - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight and the documentation for leftCapWidth and topCapHeight

    basically the image is not stretched in the area leftCapWidth pixels from the left and right edge and topCapHeight pixels from the top and the bottom. When the image is scaled the area inside of these limits is subject to stretching.

    0 讨论(0)
  • 2020-12-22 22:50

    I received an e-mail from Tortuga22 software who informed me that they have created such a library and released it under the Apache license:

    Announcement: http://blog.tortuga22.com/2010/05/31/announcing-tortuga-22-ninepatch/

    Source code: http://github.com/tortuga22/Tortuga22-NinePatch

    Example usage:

    // loads-and-caches ninepatch and rendered image of requested size
    UIImage buttonImg = [TUNinePatchCache imageOfSize:buttonSize 
                                    forNinePatchNamed:@"buttonNormalBackground"];
    [self.buttonNeedingBackground setImage:buttonImg
                           forControlState:UIControlStateNormal];
    
    0 讨论(0)
提交回复
热议问题