The Quest of UIButton

Source: Internet
Author: User

Original link

SizeToFit () and Sizethatfits (_:)

sizeToFit()The method is called sizeThatFits(_:) , and the current frame parameter is used. The view is then updated based on the results returned by the function.

SizeToFit would simply call through to sizethatfits:passing the view's current size as the argument. It would then update the view's frame based on the value of it gets back. The important logic goes in sizethatfits:, and this is the method you should override for your own custom controls.

Shadow effect and reversal of UIButton title
        myButton?.titleLabel?.shadowOffset = CGSizeMake(2.0, 2.0)        myButton?.setTitleShadowColor(UIColor.redColor(), forState: .Normal)//        myButton?.showsTouchWhenHighlighted = true        myButton?.reversesTitleShadowWhenHighlighted = true



Plus the effect of a selected bright blind

        myButton?.showsTouchWhenHighlighted = true



The layout and uiedgeinsets of UIButton

There are three of uiedgeinsets

    1. Contentedgeinsets
      Used sizeThatFits(_:) when calculating.
    2. Titleedgeinsets
      It sizeThatFits(_:) is not used in calculations.
    3. Imageedgeinsets
      It sizeThatFits(_:) is not used in calculations.

You can specify a different value for each of the four insets (top, left, bottom, right). A positive value shrinks, or insets, that edge-moving it closer to the center of the button. A negative value expands, or outsets, that edge.

Examples Show

    • Contentedgeinsets

      Both left and right are 30, which move 30pt to the middle, and 30pt to the middle. That is, both the left and right spacing are 30pt. Because Contentedgeinsets participates sizeThatFits(_:) in the calculation, the end result is that the entire button is pulled wide.
    • Titleedgeinsets

      Keep the contentedgeinsets unchanged, set the left of the titleedgeinsets to 15. Since the Titleedgeinsets sizeThatFits(_:) does not participate in, so the button is not pulled wide, just titlelabel left to the right (center) moved 15pt, the right side remains unchanged. Causes the string to not be displayed full.

      The right of Titleedgeinsets is set to-15, and the result is that the Titlelabel is shifted to the left of 15pt, thus the overall display range remains the same.
    • Imageedgeinsets
Image on right, title in left
button.transform = CGAffineTransformMakeScale(-1.0, 1.0);button.titleLabel.transform = CGAffineTransformMakeScale(-1.0, 1.0);button.imageView.transform = CGAffineTransformMakeScale(-1.0, 1.0);

Link

The Quest of UIButton

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.