Memo:
. set contentMode to 'redraw' will call the drawRect in custom view
. call setNeedsDisplay
and drawRect will be called
. call setNeedLayout
if you want to layout the subviews
. label.frame.size = CGSize.zero
before calling label.sizeToFit()
if you want the system to adjust the size for you. If there is width for the label, calling sizeToFit
only adjusts the height.
. CGAffineTransform
has three methods: scale, translate and rotate
. traitCollectionDidChange
is called when dynamic type is changed
. add @IBDesignable
and storyboard will show the design
. add @IBInspectable
and you can change the property in the storyboard
. UIGestureRecognizer
has a variable state
override func draw(_ rect: CGRect) {
// create rounded corners
let roundedRect = UIBezierPath(roundedRect: bounds, cornerRadius: 16.0)
roundedRect.addClip()
UIColor.white.setFill()
roundedRect.fill()
}
private func centeredAttributedString(_ string: String, fontSize: CGFloat) -> NSAttributedString {
// create attributedString
var font = UIFont.preferredFont(forTextStyle: .body).withSize(fontSize)
font = UIFontMetrics(forTextStyle: .body).scaledFont(for: font)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
return NSAttributedString(string: string,
attributes: [.paragraphStyle: paragraphStyle,
.font: font])
}
// how to deal with const in Swift
extension PlayingCardView {
private struct SizeRatio {
static let cornerFontSizeToBoundsHeight: CGFloat = 0.005
static let cornerRadiusToBoundsHeight: CGFloat = 0.06
static let cornerOffsetToCornerRadius: CGFloat = 0.33
static let faceCardImageSizeToBoundsSize: CGFloat = 0.75
}
}
// adding a gesture recognizer to a UIView
@IBOutlet weak var pannableView: UIView {
didSet {
let panGestureRecognizer = UIPanGestureRecognizer {
target: self, action: #selector(someFunction)
}
pannableView.addGestureRecognizer(panGestureRecognizer)
}
}