Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exposes icon fontweight from SF Symbol #43

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 43 additions & 5 deletions ios/BurntModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,19 @@ enum AlertPreset: String, Enumerable {
case .spinner:
return .spinner
case .custom:
guard let image = UIImage.init( systemName: options?.icon?.name ?? "swift") else {
throw BurntError.invalidSystemName
let weight: UIImage.SymbolWeight
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about:

let weight = try? toFontWeight(from: options?.icon?.fontWeight) ?? .regular

Copy link
Author

@peterpme peterpme Jun 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think i tried something like this earlier, but tried it again and got something like this:

let configuration = UIImage.SymbolConfiguration(weight: weight)

... value of optional type 'UIImage.SymbolWeight?' must be unwrapped to a value of type 'UIImage.SymbolWeight'

that being said, this worked. wdyt?

guard let weight = try? toFontWeight(from: options?.icon?.fontWeight) else {
    throw BurntError.invalidIconWeight
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you try the exact code i gave? mine has a fallback to regular

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that being said, this worked. wdyt?

that code doesn't not work because it will throw if weight isn't given, but it's optional

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, i ran the exact code, here's what the whole custom block looks like:

      case .custom:
        let weight = try? toFontWeight(from: options?.icon?.fontWeight) ?? .regular

        let configuration = UIImage.SymbolConfiguration(weight: weight)
         guard let image = UIImage(systemName: options?.icon?.name ?? "swift", withConfiguration: configuration) else {
            throw BurntError.invalidSystemName
        }

        return .custom(image.withTintColor(options?.icon?.color ?? .systemBlue, renderingMode: .alwaysOriginal))

with the same error (i just tried again)

do {
weight = try toFontWeight(from: options?.icon?.fontWeight)
} catch {
throw BurntError.invalidIconWeight
}
return .custom((image.withTintColor(options?.icon?.color ?? .systemBlue, renderingMode: .alwaysOriginal)))

let configuration = UIImage.SymbolConfiguration(weight: weight)
guard let image = UIImage(systemName: options?.icon?.name ?? "swift", withConfiguration: configuration) else {
throw BurntError.invalidSystemName
}

return .custom(image.withTintColor(options?.icon?.color ?? .systemBlue, renderingMode: .alwaysOriginal))
}
}
}
Expand Down Expand Up @@ -86,6 +95,9 @@ struct Icon: Record {

@Field
var color: UIColor = .systemGray

@Field
var fontWeight: String = "regular"
}

struct IconSize: Record {
Expand Down Expand Up @@ -168,6 +180,7 @@ enum ToastHaptic: String, Enumerable {
}
enum BurntError: Error {
case invalidSystemName
case invalidIconWeight
}
enum ToastPreset: String, Enumerable {
case done
Expand All @@ -184,14 +197,24 @@ enum ToastPreset: String, Enumerable {
case .none:
return .none
case .custom:
guard let image = UIImage.init( systemName: options?.icon?.name ?? "swift") else {
throw BurntError.invalidSystemName
let weight: UIImage.SymbolWeight
do {
weight = try toFontWeight(from: options?.icon?.fontWeight)
} catch {
throw BurntError.invalidIconWeight
}

let configuration = UIImage.SymbolConfiguration(weight: weight)
guard let image = UIImage(systemName: options?.icon?.name ?? "swift", withConfiguration: configuration) else {
throw BurntError.invalidSystemName
}

return .custom(image.withTintColor(options?.icon?.color ?? .systemBlue, renderingMode: .alwaysOriginal))
}
}
}


enum ToastPresentSide: String, Enumerable {
case top
case bottom
Expand Down Expand Up @@ -266,3 +289,18 @@ public class BurntModule: Module {
}.runOnQueue(.main)
}
}

func toFontWeight(from string: String?) -> UIImage.SymbolWeight {
switch string?.lowercased() {
case "ultralight": return .ultraLight
case "thin": return .thin
case "light": return .light
case "regular": return .regular
case "medium": return .medium
case "semibold": return .semibold
case "bold": return .bold
case "heavy": return .heavy
case "black": return .black
default: return .regular
}
}
14 changes: 14 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ export type IconParams = {
* @platform ios
*/
color: string;
/**
* Change the custom icon font weight, default is system regular.
* @platform ios
*/
fontWeight:
| "ultralight"
| "thin"
| "light"
| "regular"
| "medium"
| "semibold"
| "bold"
| "heavy"
| "black";
};
web?: JSX.Element;
};
Expand Down