IOS image stretching Solution

Source: Internet
Author: User

UIButton achieves background stretching, that is, there are two ways to do this without stretching between the two ends of the image:

The first method is simple and widely used. The method is to directly stretch the image for setBackgroundImage. The Code is as follows:

  1. UIImage * image = [UIImage imageNamed: @ "image.png"];
  2. Image = [image stretchableImageWithLeftCapWidth: flograding (image. size. width/2) topCapHeight: flograding (image. size. height/2)];


After the left-side cap is set, rightCapWidth = image. size. width-(image. leftCapWidth + 1); that is, one pixel in the middle of the image is used for stretching. The vertical direction is the same as above. After the settings, the image can be automatically stretched no matter what controls the image is placed in.

 

  1. UIImage * buttonImage = [UIImage imageNamed: @ "contact.png"];
  2. ButtonImage = [buttonImage stretchableImageWithLeftCapWidth: flolf (buttonImage. size. width/2) topCapHeight: flolf (buttonImage. size. height/2)];
  3. UIImage * buttonImageselected = [UIImage imageNamed: @ "contactselected.png"];
  4. ButtonImage = [buttonImage stretchableImageWithLeftCapWidth: flolf (buttonImage. size. width/2) topCapHeight: flolf (buttonImage. size. height/2)];
  5. UIButton * button = [UIButton buttonWithType: UIButtonTypeCustom];
  6. Button. frame = CGRectMake (0, 0,240, 44 );
  7. [Button setBackgroundImage: buttonImage forState: UIControlStateNormal];
  8. [Button setBackgroundImage: buttonImageselected forState: UIControlStateHighlighted];
  9. Button. center = CGPointMake (160,240 );
  10. [Button setTitle: @ "button" forState: UIControlStateNormal];
  11. [Button setTitle: @ "buttonClick" forState: UIControlStateHighlighted];
  12. [Self. view addSubview: button];


The second method is to add a UIImageView to UIButton, stretch the imageView, and set the background of the button to clearColor. Put the imageView in the button and sendToBack to get the effect. The Code is as follows:
// Code of the Image view extension just now

  1. UIImageView * strechTest = [[UIImageyiView alloc] initWithImage: [UIImage imageNamed: @ "contact.png"];
  2. [StrechTest setContentStretch: CGRectMake (0.5f, 0.5f, 0.f, 0.f)];
  3. CGRect frame = strechTest. frame;
  4. Frame. size. width + = 100;
  5. StrechTest. frame = frame;

  1. // Put the imageView in the button and set it to back
  2. UIButton * button = [UIButton buttonWithType: UIButtonTypeCustom];
  3. Button1.frame = frame;
  4. Button1.center = CGPointMake (160,140 );
  5. [Button1 addSubview: strechTest];
  6. [Button1 sendSubviewToBack: strechTest];
  7. [Button1 setBackgroundColor: [UIColor clearColor];
  8. [Button1 setTitle: @ "button" forState: UIControlStateNormal];
  9. [Button1 setTitle: @ "buttonClick" forState: UIControlStateHighlighted];
  10. [Self. view addSubview: button];


Effect:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.