Block Utilities
This document outlines the utility functions and extensions provided by the NativeblocksFoundation package to simplify working with SwiftUI blocks.
Extensions on Color
Initializers
init(blockHex:)
public init?(blockHex: String)
mapBlockAlignmentHorizontal(_:)
Maps a horizontal alignment string to the corresponding HorizontalAlignment.
func mapBlockAlignmentHorizontal(_ alignment: String) -> HorizontalAlignment
Parameters
- alignment: The alignment string (e.g., "leading", "trailing", "center").
Returns
The mapped HorizontalAlignment value.
mapBlockScrollable(_:)
Maps a scrollable axis string to the corresponding Axis.Set.
func mapBlockScrollable(_ scroll: String) -> Axis.Set
Parameters
- scroll: The scroll string (e.g., "horizontal", "vertical", "both").
Returns
The mapped Axis.Set value.
roundedRectangleShape(_:)
Creates a rounded rectangle shape with the specified corner radius.
public func roundedRectangleShape(
_ radius: CGFloat
) -> RoundedRectangle
Parameters
- radius: The corner radius of the rounded rectangle.
Returns
A RoundedRectangle with the specified corner radius.
Extensions on String
Methods
isValidImageUrl()
Checks if the string is a valid URL for an image.
public func isValidImageUrl() -> Bool
Returns
true if the URL is valid, otherwise false.
Extensions on View
Methods
blockWidthAndHeightModifier(::alignment:)
Applies a width and height modifier to a view.
public func blockWidthAndHeightModifier(_ width: String, _ height: String, alignment: Alignment = .center) -> some View
Parameters
- width: The width string (e.g., "infinity" or a numeric value).
- height: The height string (e.g., "infinity" or a numeric value).
- alignment: The alignment for the view's frame.
Returns
A view with the specified frame settings.
blockMultilineTextAlignment(_:)
Aligns the text of a view based on the provided alignment string.
public func blockMultilineTextAlignment(_ alignment: String) -> some View
Parameters
- alignment: The alignment string (e.g., "leading", "center", "trailing").
Returns
A view with the specified text alignment.
blockFont(family:size:weight:design:)
Applies a font style to the view.
public func blockFont(family: String, size: CGFloat, weight: String, design: String) -> some View
Parameters
- family: The font family (e.g., "system", "custom font name").
- size: The font size.
- weight: The font weight string (e.g., "regular", "bold").
- design: The font design string (e.g., "default", "serif").
Returns
A view with the specified font style.
mapFontWeight(_:)
private func mapFontWeight(_ fontWeight: String) -> Font.Weight
mapFontDesign(_:)
private func mapFontDesign(_ fontWeight: String) -> Font.Design
blockDirection(_:)
Sets the layout direction of the view's environment based on the specified direction.
public func blockDirection(_ direction: String) -> some View
This method updates the environment's layout direction, which is useful for supporting languages or regions that require right-to-left (RTL) layouts, such as Arabic or Hebrew.
Parameters
- direction: The layout direction as a string ("RTL" for right-to-left, otherwise defaults to left-to-right).
Returns
A view with the updated layout direction applied in its environment.
blockKeyboardType(_:)
Sets the keyboard type for text input fields within the view's environment.
public func blockKeyboardType(_ type: String) -> some View
Supported keyboard types:
-
"asciicapable"
-
"numbersandpunctuation"
-
"url"
-
"numberpad"
-
"phonepad"
-
"namephonepad"
-
"emailaddress"
-
"decimalpad"
-
"twitter"
-
"websearch"
-
"asciicapablenumberpad"
-
"alphabet"
Defaults to .default if an unsupported type is provided.
Parameters
- type: A string representing the keyboard type (e.g., "asciicapable", "numberpad").
Returns
A view with the specified keyboard type applied.
blockAutocapitalization(_:)
Configures autocapitalization behavior for text input fields within the view's environment.
public func blockAutocapitalization(_ type: String) -> some View
Supported autocapitalization types:
-
"allcharacters"
-
"sentences"
-
"words"
Defaults to .none if an unsupported type is provided.
Parameters
- type: A string representing the autocapitalization type (e.g., "allcharacters", "sentences", "words").
Returns
A view with the specified autocapitalization type applied.
blockScrollIndicators(_:)
Configures the visibility of scroll indicators within the view's environment.
public func blockScrollIndicators(_ type: String) -> some View
Supported visibility options:
-
"automatic"
-
"hidden"
-
"never"
-
"visible"
Defaults to .visible if an unsupported type is provided. This feature is available on iOS 16.0 and later.
Parameters
- type: A string representing the scroll indicator visibility (e.g., "automatic", "hidden", "never", "visible").
Returns
A view with the specified scroll indicator visibility applied.
blockScaled(_:)
Scales the view based on the specified scaling mode.
public func blockScaled(_ scale: String) -> some View
Parameters
- scale: The scaling mode as a string (e.g., "fill", "fit").
Returns
A view scaled according to the specified mode.
blockAspectRatio(ratio:mode:)
Sets the aspect ratio of the view based on a given ratio and mode.
public func blockAspectRatio(ratio: CGFloat, mode: String) -> some View
Parameters
- ratio: The aspect ratio as a CGFloat.
- mode: The content mode as a string (e.g., "fill").
Returns
A view with the specified aspect ratio and mode applied.
Extensions on KFImage
Methods
blockResizable(_:)
Makes the image resizable based on the specified resizing mode.
public func blockResizable(_ mode: String) -> KFImage
Parameters
- mode: The resizing mode as a string (e.g., "stretch", "tile").
Returns
A KFImage object configured with the specified resizing mode.
blockInterpolation(_:)
Configures the image interpolation quality.
public func blockInterpolation(_ value: String) -> KFImage
Parameters
- value: The interpolation quality as a string (e.g., "high", "medium", "low").
Returns
A KFImage object with the specified interpolation setting.