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
- Contentedgeinsets
Used sizeThatFits(_:)
when calculating.
- Titleedgeinsets
It sizeThatFits(_:)
is not used in calculations.
- 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