custom symbols picker

This commit is contained in:
neon443
2025-06-14 10:59:48 +01:00
parent 6e5b7adbc4
commit 13ef94ea3e
5 changed files with 118 additions and 4 deletions

View File

@@ -0,0 +1,36 @@
//
// SymbolsLoader.swift
// NearFuture
//
// Created by neon443 on 14/06/2025.
//
import Foundation
class SymbolsLoader: ObservableObject {
@Published var allSymbols: [String] = []
init() {
self.allSymbols = getAllSymbols()
}
func getSymbols(_ searched: String) -> [String] {
if searched.isEmpty {
return allSymbols
} else {
return allSymbols.filter() { $0.localizedCaseInsensitiveContains(searched) }
}
}
func getAllSymbols() -> [String] {
var allSymbols = [String]()
if let bundle = Bundle(identifier: "com.apple.CoreGlyphs"),
let resPath = bundle.path(forResource: "name_availability", ofType: "plist"),
let plist = try? NSDictionary(contentsOf: URL(fileURLWithPath: resPath), error: ()),
let plistSymbols = plist["symbols"] as? [String: String]
{
allSymbols = Array(plistSymbols.keys)
}
return allSymbols
}
}

View File

@@ -0,0 +1,48 @@
//
// SymbolsPicker.swift
// NearFuture
//
// Created by neon443 on 14/06/2025.
//
import SwiftUI
struct SymbolsPicker: View {
@StateObject private var symbolsLoader = SymbolsLoader()
@Binding var selection: String
@State var searchInput: String = ""
private func gridLayout(forWidth geoSizeWidth: CGFloat) -> [GridItem] {
let gridItem = GridItem(.fixed(40), spacing: 20, alignment: .center)
let columns = Int(geoSizeWidth/60.rounded(.up))
return Array(repeating: gridItem, count: columns)
}
var body: some View {
GeometryReader { geo in
ScrollView {
LazyVGrid(columns: gridLayout(forWidth: geo.size.width)) {
ForEach(symbolsLoader.getSymbols(searchInput), id: \.self) { symbol in
Button() {
selection = symbol
} label: {
Image(systemName: symbol)
.resizable()
.scaledToFit()
.frame(maxWidth: 40, maxHeight: 40)
.symbolRenderingMode(.palette)
.foregroundStyle(.blue, .gray, .black)
}
.buttonStyle(.plain)
}
}
}
.searchable(text: $searchInput)
}
}
}
#Preview {
SymbolsPicker(selection: .constant(""))
}