[docs] UIKit.md: Views & Controls section
This commit is contained in:
@@ -229,7 +229,210 @@ stack.addArrangedSubview(row3)
|
|||||||
|
|
||||||
## Views & Controls
|
## Views & Controls
|
||||||
|
|
||||||
<!-- batch 10 -->
|
<details>
|
||||||
|
<summary><b>UIButton</b> — per-state accessors, all-state setters</summary>
|
||||||
|
|
||||||
|
### Per-state properties
|
||||||
|
`imageForNormal` / `imageForHighlighted` / `imageForSelected` / `imageForDisabled`,
|
||||||
|
`titleForNormal` / `…Highlighted` / `…Selected` / `…Disabled`,
|
||||||
|
`titleColorForNormal` / `…Highlighted` / `…Selected` / `…Disabled` — get/set
|
||||||
|
shortcuts for the matching `UIControl.State` (also `@IBInspectable`).
|
||||||
|
|
||||||
|
```swift
|
||||||
|
button.titleForNormal = "Save"
|
||||||
|
button.titleColorForDisabled = .tertiaryLabel
|
||||||
|
```
|
||||||
|
|
||||||
|
### `func setTitleForAllStates(_:)` / `func setTitleColorForAllStates(_:)` / `func setImageForAllStates(_:)`
|
||||||
|
Apply one value to `.normal`, `.selected`, `.highlighted`, `.disabled` at once.
|
||||||
|
|
||||||
|
```swift
|
||||||
|
button.setTitleColorForAllStates(.white)
|
||||||
|
```
|
||||||
|
|
||||||
|
### `func centerTextAndImage(spacing: CGFloat)`
|
||||||
|
Balance title/image edge insets so text + icon sit centred with `spacing` between them.
|
||||||
|
|
||||||
|
```swift
|
||||||
|
button.centerTextAndImage(spacing: 8)
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary><b>UILabel</b></summary>
|
||||||
|
|
||||||
|
### `func lineNumbers() -> Int`
|
||||||
|
Rendered line count at the current width.
|
||||||
|
|
||||||
|
```swift
|
||||||
|
if bodyLabel.lineNumbers() > 3 { showMoreButton() }
|
||||||
|
```
|
||||||
|
|
||||||
|
### `var getEstimatedHeight: CGFloat`
|
||||||
|
Height the label would need to show its full text/attributed text unclipped.
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary><b>UITextField</b></summary>
|
||||||
|
|
||||||
|
### `var placeholderColor: UIColor`
|
||||||
|
Get/set the placeholder text colour (rebuilds `attributedPlaceholder`; setter is a
|
||||||
|
no-op if no placeholder text is set).
|
||||||
|
|
||||||
|
```swift
|
||||||
|
field.placeholder = "Email"
|
||||||
|
field.placeholderColor = .secondaryLabel
|
||||||
|
```
|
||||||
|
|
||||||
|
### `func addPaddingLeft(_ padding: CGFloat)`
|
||||||
|
Inset the text from the left with an empty spacer view.
|
||||||
|
|
||||||
|
### `func addPaddingLeftIcon(_ image: UIImage, padding: CGFloat)`
|
||||||
|
Left view = an icon plus trailing padding.
|
||||||
|
|
||||||
|
```swift
|
||||||
|
field.addPaddingLeftIcon(UIImage(systemName: "magnifyingglass")!, padding: 8)
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary><b>UIImageView</b> (<code>@MainActor</code>)</summary>
|
||||||
|
|
||||||
|
### `func changeColorOfImage(_ color: UIColor, image: UIImage?) -> UIImageView`
|
||||||
|
Set a template-rendered image tinted to `color`; returns `self`.
|
||||||
|
|
||||||
|
```swift
|
||||||
|
iconView.changeColorOfImage(.systemBlue, image: UIImage(named: "star"))
|
||||||
|
```
|
||||||
|
|
||||||
|
### `var encodeToBase64: String?`
|
||||||
|
JPEG (quality 0.6) of the current image as a Base64 string.
|
||||||
|
|
||||||
|
### `func addAspectRatioConstraint()` / `func removeAspectRatioConstraint()`
|
||||||
|
Add / remove a width-to-height constraint matching the current image's ratio.
|
||||||
|
|
||||||
|
```swift
|
||||||
|
photoView.image = photo
|
||||||
|
photoView.addAspectRatioConstraint()
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary><b>UIImage</b> — recolour, resize, thumbnails, masks, init</summary>
|
||||||
|
|
||||||
|
### `func imageWithColor(color: UIColor) -> UIImage` / `func tintImage(color: UIColor) -> UIImage`
|
||||||
|
Return a copy filled / tinted with `color` (keeps the alpha shape).
|
||||||
|
|
||||||
|
```swift
|
||||||
|
let redIcon = icon.tintImage(color: .systemRed)
|
||||||
|
```
|
||||||
|
|
||||||
|
### `func backgroundColorTransparent(initialColor: UIColor, finalColor: UIColor) -> UIImage?`
|
||||||
|
Make pixels in a colour range transparent.
|
||||||
|
|
||||||
|
### `class func outlinedEllipse(size: CGSize, color: UIColor, lineWidth: CGFloat = 1) -> UIImage?`
|
||||||
|
Generate a stroked-ellipse image.
|
||||||
|
|
||||||
|
```swift
|
||||||
|
UIImage.outlinedEllipse(size: CGSize(width: 24, height: 24), color: .label)
|
||||||
|
```
|
||||||
|
|
||||||
|
### `func resizeImage(newWidth: CGFloat) -> UIImage`
|
||||||
|
Scale to `newWidth`, keeping the aspect ratio.
|
||||||
|
|
||||||
|
### `func createThumbnail(_ maxPixelSize: UInt) -> UIImage`
|
||||||
|
Fast down-sampled thumbnail via `CGImageSource`.
|
||||||
|
|
||||||
|
```swift
|
||||||
|
let thumb = fullImage.createThumbnail(200)
|
||||||
|
```
|
||||||
|
|
||||||
|
### `func maskWithAlphaImage(maskImage: UIImage) -> UIImage`
|
||||||
|
Use another image's alpha as a mask.
|
||||||
|
|
||||||
|
### `func isAnimated() -> Bool`
|
||||||
|
Whether the image has more than one frame.
|
||||||
|
|
||||||
|
### `init?(base64String: String, scale: CGFloat = 1)`
|
||||||
|
Decode a Base64 string to an image.
|
||||||
|
|
||||||
|
### `init(view: UIView)` — *`@MainActor`*
|
||||||
|
Rasterise a view into an image.
|
||||||
|
|
||||||
|
```swift
|
||||||
|
let snapshot = UIImage(view: cardView)
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary><b>UIColor</b> — hex, components</summary>
|
||||||
|
|
||||||
|
### `convenience init(hex: String)`
|
||||||
|
Parse `#RGB`, `#RGBA`, `#RRGGBB`, or `#RRGGBBAA` (with or without `#`).
|
||||||
|
|
||||||
|
```swift
|
||||||
|
view.backgroundColor = UIColor(hex: "#1E88E5")
|
||||||
|
UIColor(hex: "FF0000CC") // red at 80% alpha
|
||||||
|
```
|
||||||
|
|
||||||
|
### `var hexString: String?`
|
||||||
|
`#RRGGBB` (or `#RRGGBBAA` when alpha < 1).
|
||||||
|
|
||||||
|
### `var redValue` / `var greenValue` / `var blueValue` / `var alphaValue`
|
||||||
|
Individual channel values (`CGFloat`, via `CIColor`).
|
||||||
|
|
||||||
|
```swift
|
||||||
|
UIColor.systemBlue.redValue // 0.0…1.0
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary><b>UIScrollView</b> — snapshot, visible rect, paged scrolling</summary>
|
||||||
|
|
||||||
|
### `var snapshot: UIImage?`
|
||||||
|
Image of the **entire** content size (not just the visible part) — works on
|
||||||
|
`UITableView` / `UICollectionView` too.
|
||||||
|
|
||||||
|
### `var visibleRect: CGRect`
|
||||||
|
The currently visible content region.
|
||||||
|
|
||||||
|
### `var offsetInPage: CGFloat`
|
||||||
|
Fractional position within the current page height (`0.0`…`1.0`).
|
||||||
|
|
||||||
|
### `func scrollUp(animated:)` / `scrollDown(animated:)` / `scrollLeft(animated:)` / `scrollRight(animated:)`
|
||||||
|
Move one page (respects `isPagingEnabled`). `animated` defaults to `true`.
|
||||||
|
|
||||||
|
```swift
|
||||||
|
nextButton.onTap = { scrollView.scrollRight() }
|
||||||
|
```
|
||||||
|
|
||||||
|
### `enum orientation`
|
||||||
|
`horizontal` / `vertical` — helper enum used by scroll utilities.
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary><b>UITapGestureRecognizer</b></summary>
|
||||||
|
|
||||||
|
### `func didTapAttributedTextInLabel(label: UILabel, textToTouch: String) -> Bool`
|
||||||
|
Whether the tap landed on a given substring of a label's attributed text — for
|
||||||
|
making part of a label tappable.
|
||||||
|
|
||||||
|
```swift
|
||||||
|
@objc func handleTap(_ g: UITapGestureRecognizer) {
|
||||||
|
if g.didTapAttributedTextInLabel(label: termsLabel, textToTouch: "Terms of Use") {
|
||||||
|
openTerms()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
## Navigation & Controllers
|
## Navigation & Controllers
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user