Skip to content

Instantly share code, notes, and snippets.

@aqubi
aqubi / pickersamples.swift
Last active April 16, 2024 08:24
SwiftUI Colored Icon Picker Samples
import SwiftUI
struct ContentView: View {
@State private var labelStyle: PickerLabelStyle = .label
@State private var selection: PickerItem = PickerItem.item1
@State private var imageName: String = "circle.fill"
@State private var secondaryColor: Color = .primary
var body: some View {
extension UIColor {
convenience init?(hexString: String) {
let hexString = hexString.trimmingCharacters(in: .whitespacesAndNewlines)
let scanner = Scanner(string: hexString)
scanner.charactersToBeSkipped = CharacterSet(charactersIn: "#")
var color: UInt64 = 0
if scanner.scanHexInt64(&color) {
self.init(hex: color, useOpacity: hexString.count > 7)
} else {
return nil
func toHexString4(_ color:UIColor) -> String? {
var cgColor:CGColor = color.cgColor
if let name = cgColor.colorSpace?.name, name != CGColorSpace.sRGB {
cgColor = color.cgColor.converted(to: CGColorSpace(name: CGColorSpace.sRGB)!, intent: .defaultIntent, options: nil)!
}
let ciColor = CIColor(cgColor: cgColor)
return toHexString([ciColor.red, ciColor.green, ciColor.blue, ciColor.alpha])
}
func toHexString1(_ color:UIColor) -> String? {
guard let components = color.cgColor.components else { return nil }
return toHexString(components)
}
func toHexString2(_ color:UIColor) -> String? {
var r: CGFloat = 0
var g: CGFloat = 0
var b: CGFloat = 0
var a: CGFloat = 0
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let split = splitViewController, split.isCollapsed {
if let indexPath = tableView.indexPathForSelectedRow {
tableView.deselectRow(at: indexPath, animated: true)
}
}
}
override func viewWillAppear(_ animated: Bool) {
if let split = splitViewController {
self.clearsSelectionOnViewWillAppear = split.isCollapsed
}
super.viewWillAppear(animated)
}
class SURMasterTableViewCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
updateAccessoryType()
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
updateAccessoryType()
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
if sourceIndexPath.section != destinationIndexPath.section { return }
SURDataManager.shared.move(from: sourceIndexPath, to: destinationIndexPath)
}
func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath {
func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
let itemProvider = NSItemProvider(object: "sample name" as NSString)
return [UIDragItem(itemProvider: itemProvider)]
}
func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) {
if coordinator.items.count == 0 { return }
let destinationIndexPath = coordinator.destinationIndexPath ?? IndexPath(row: editImages.images.count, section: 0)
let cellImageWidth = tableView.frame.size.width - tableView.layoutMargins.left - tableView.layoutMargins.right
for item in coordinator.items {
let itemProvider = item.dragItem.itemProvider
if !itemProvider.canLoadObject(ofClass: UIImage.self) { continue }
itemProvider.loadObject(ofClass: UIImage.self) {(object, error) in
guard let image = object as? UIImage else { return }