String

struct String

Extends String to support the Action Data controls and adds convenience methods for working with UIImage and UIColor properties in a Codable, Encodable or Decodable class.

Examples:

// Get the hex representation of a color in iOS, tvOS and watchOS.
let hex: String ~= UIColor.white
  • Sets the String from the given UIColor where the color is converted to a hex string in the format #rrggbbaa where:

    • rr - Specifies the red component as a hex value in the range 00 to FF.
    • gg - Specifies the green component as a hex value in the range 00 to FF.
    • bb - Specifies the blue component as a hex value in the range 00 to FF.
    • aa - Specifies the alpha component as a hex value in the range 00 to FF.

    Examples:

    // Get the hex representation of a color in iOS, tvOS and watchOS.
    let hex: String ~= UIColor.white
    

    Declaration

    Swift

    public static func ~= ( left: inout String, right: UIColor)
  • Sets the String from the given UIImage where the image is converted to a PNG representation and Base 64 encoded.

    Examples:

    // Get the Base 64 representation of an image in iOS, tvOS and watchOS.
    let hex: String ~= UIImage(named: "Background.png")
    

    Declaration

    Swift

    public static func ~= ( left: inout String, right: UIImage)
  • Attempts to convert the String into a UIColor if it is in the format rrggbb or rrggbbaa where:

    • rr - Specifies the red component as a hex value in the range 00 to FF.
    • gg - Specifies the green component as a hex value in the range 00 to FF.
    • bb - Specifies the blue component as a hex value in the range 00 to FF.
    • aa - Specifies the alpha component as a hex value in the range 00 to FF.

    Examples:

    // Get the hex representation of a color in iOS, tvOS and watchOS.
    let hex: String ~= UIColor.white
    let color = hex.uiColor
    

    The hex string can optionally start with the prefix of #. If the String cannot be converted to a UIImage, nil is returned.

    Declaration

    Swift

    public var uiColor: UIColor?
  • If the String contains a Base 64 encoded representation of an image it is returns as a UIImage, else nil is returned.

    Examples:

    // Get the Base 64 representation of an image in iOS, tvOS and watchOS.
    let hex: String ~= UIImage(named: "Background.png")
    let image = hex.uiImage
    

    Declaration

    Swift

    public var uiImage: UIImage?
  • Initializes a String instance from a given UIColor stored in the format #rrggbbaa where:

    • rr - Specifies the red component as a hex value in the range 00 to FF.
    • gg - Specifies the green component as a hex value in the range 00 to FF.
    • bb - Specifies the blue component as a hex value in the range 00 to FF.
    • aa - Specifies the alpha component as a hex value in the range 00 to FF.

    Examples:

    // Get the hex representation of a color in iOS, tvOS and watchOS.
    let hex = String(fromColor: UIColor.white)
    

    Declaration

    Swift

    public init(fromColor color: UIColor)

    Parameters

    color

    The given UIColor to convert to a hex string.

  • Initializes a String instance from a given UIImage where the image is converted to a PNG representation and Base 64 encoded.

    Examples:

    // Get the Base 64 representation of an image in iOS, tvOS and watchOS.
    let hex = String(fromImage: UIImage(named: "Background.png"))
    

    Declaration

    Swift

    public init(fromImage image: UIImage)
  • Returns a pretty-printed type name (minus the module name) for the given value.

    Example:

    // Assings a pretty type name
    let name = String.typeName(of: "<module>")
    

    Declaration

    Swift

    public static func typeName(of value: Any) -> String

    Parameters

    value

    The value to get the type name of.

    Return Value

    The type name minus the module name.

  • Returns the draw height of the string in the given font constrained to the given width.

    Example:

    // Get a string height in iOS, tvOS and watchOS
    let font = UIFont.systemFont(ofSize: 34, weight: UIFontWeightThin)
    let text = "Hello World"
    let height = text.height(withConstrainedWidth: 250, font: font)
    

    Declaration

    Swift

    public func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat

    Parameters

    width

    The pixel width to constrain the string to.

    font

    The font that the string will be drawn in.

    Return Value

    The height of the string drawn in the given font constrained to the given width.

  • Returns the draw width of the string in the given font constrained to the given height.

    Example:

    // Get a string height in iOS, tvOS and watchOS
    let font = UIFont.systemFont(ofSize: 34, weight: UIFontWeightThin)
    let text = "Hello World"
    let height = text.width(withConstrainedHeight: 50, font: font)
    

    Declaration

    Swift

    public func width(withConstrainedHeight height: CGFloat, font: UIFont) -> CGFloat

    Parameters

    height

    The pixel height to constrain the string to.

    font

    The font that the string will be drawn in.

    Return Value

    The width of the string drawn in the given font constrained to the given height.

  • Returns the drawing bounds of the string in the given font constrained to the given maximum bounds.

    Example:

    // Get a string height in iOS, tvOS and watchOS
    let font = UIFont.systemFont(ofSize: 34, weight: UIFontWeightThin)
    let text = "Hello World"
    let bounds = text.bounds(withConstrainedSize: GGSize(width: 250, height: 50), font: font)
    
    • size: The maximum pixel width and height on the string.
    • font: The font that the string will be drawn in.

    Declaration

    Swift

    public func bounds(withConstrainedSize size: CGSize, font: UIFont) -> CGSize

    Return Value

    The bounds on the string drawn in the given font constrained to the given maximum bounds.