### Install SwiftyAttributes with Swift Package Manager Source: https://github.com/eddiekaiger/swiftyattributes/blob/master/README.md Provides the dependency declaration for integrating SwiftyAttributes into your project using Swift Package Manager. ```swift dependencies: [ .package(url: "https://github.com/eddiekaiger/SwiftyAttributes.git", from: "5.3.0") ] ``` -------------------------------- ### Enumerate All Attributes in a Range with `swiftyAttributes` Source: https://context7.com/eddiekaiger/swiftyattributes/llms.txt Get all attribute runs within a specified range as an array of `([Attribute], Range)` tuples using `swiftyAttributes(in:options:)`. This provides a Swift-native way to iterate over attribute segments without `NSRange` bridging. ```swift let richText = "Hello" .withTextColor(.systemRed) .withFont(.boldSystemFont(ofSize: 14)) + " World" .withTextColor(.systemBlue) .withFont(.systemFont(ofSize: 14)) .withUnderlineStyle(.single) let runs = richText.swiftyAttributes(in: 0..` for attribute manipulation. ```swift extension NSMutableAttributedString { func addAttributes(_ attributes: [Attribute], range: Range) func addAttributes(_ attributes: [Attribute], range: NSRange) func setAttributes(_ attributes: [Attribute], range: Range) func setAttributes(_ attributes: [Attribute], range: NSRange) func replaceCharacters(in range: Range, with str: String) func replaceCharacters(in range: Range, with attrString: NSAttributedString) func deleteCharacters(in range: Range) func removeAttribute(_ name: NSAttributedStringKey, range: Range) } ``` -------------------------------- ### Read Single Attribute with `swiftyAttribute` Source: https://context7.com/eddiekaiger/swiftyattributes/llms.txt Retrieve a specific attribute's value at a given index using `swiftyAttribute(_:at:)`. This method returns the attribute as a `Attribute` enum case, simplifying type checking and unwrapping. Use the `effectiveRange` parameter to get the range over which the attribute applies. ```swift let str = "Hello World" .withFont(.boldSystemFont(ofSize: 16)) .withTextColor(.systemBlue) .withKern(1.5) if let fontAttr = str.swiftyAttribute(.font, at: 3) { if case .font(let font) = fontAttr { print(font.pointSize) // 16.0 } } var effectiveRange = NSRange() if let colorAttr = str.swiftyAttribute(.foregroundColor, at: 0, effectiveRange: &effectiveRange) { if case .textColor(let color) = colorAttr { print(color) // UIColor.systemBlue print(effectiveRange) // {0, 11} — full string range } } let kern = str.swiftyAttribute(.kern, at: 6) // kern == .kern(1.5) since it applies to the whole string ``` -------------------------------- ### Create Attributed String with Underline Color and Style using Extensions Source: https://github.com/eddiekaiger/swiftyattributes/blob/master/README.md Demonstrates creating an attributed string with specific underline color and style using the `with[Attribute]` extensions. ```swift "Hello World".withUnderlineColor(.red).withUnderlineStyle(.styleDouble) ``` -------------------------------- ### Initialize NSAttributedString with Swifty Attributes Source: https://github.com/eddiekaiger/swiftyattributes/blob/master/README.md Illustrates initializing an `NSAttributedString` with a string and a list of Swifty attributes using a convenience initializer. ```swift NSAttributedString(string: "Hello World", swiftyAttributes: [.kern(5), .backgroundColor(.gray)]) ``` -------------------------------- ### SwiftyAttributes: All Available Attribute Cases Source: https://context7.com/eddiekaiger/swiftyattributes/llms.txt Demonstrates the usage of the `Attribute` enum to define various text styles. Supports cross-platform attributes and custom attributes. ```swift // All available Attribute cases (cross-platform) let attrs: [Attribute] = [ .font(.systemFont(ofSize: 16, weight: .bold)), .textColor(.white), .backgroundColor(.systemBlue), .kern(1.5), .ligatures(.default), .baselineOffset(2.0), .obliqueness(0.2), .expansion(0.1), .strokeColor(.black), .strokeWidth(-2.0), .underlineStyle(.single), .underlineColor(.red), .strikethroughStyle(.double), .strikethroughColor(.gray), .paragraphStyle({ let style = NSMutableParagraphStyle() style.alignment = .center style.lineSpacing = 4 return style }()), .link(URL(string: "https://example.com")!), .textEffect(.letterPressStyle), .writingDirections([.leftToRight(.override)]), .custom("MyAppHighlight", true) ] // Inspect a single attribute let colorAttr = Attribute.textColor(.systemRed) print(colorAttr.keyName) // NSAttributedString.Key.foregroundColor print(colorAttr.value) // UIColor.systemRed print(colorAttr.foundationValue) // UIColor.systemRed (same here; differs for enums like .ligatures) // Equality check let a = Attribute.kern(2.0) let b = Attribute.kern(2.0) print(a == b) // true ``` -------------------------------- ### Create Attributed String with Text Color and Underline Source: https://github.com/eddiekaiger/swiftyattributes/blob/master/README.md Demonstrates creating an attributed string with specific text color and underline style using SwiftyAttributes extensions. ```swift let fancyString = "Hello World!".withTextColor(.blue).withUnderlineStyle(.styleSingle) ``` -------------------------------- ### Dictionary / Sequence conversion — swiftyAttributes and foundationAttributes Source: https://context7.com/eddiekaiger/swiftyattributes/llms.txt Bidirectional conversion between [NSAttributedString.Key: Any] (Foundation) and [Attribute] (SwiftyAttributes) via computed properties on Dictionary and Sequence. ```APIDOC ## Dictionary / Sequence conversion — `swiftyAttributes` and `foundationAttributes` ### Description Bidirectional conversion between `[NSAttributedString.Key: Any]` (Foundation) and `[Attribute]` (SwiftyAttributes) via computed properties on `Dictionary` and `Sequence`. ### Computed Properties - **`Dictionary.swiftyAttributes`**: Converts a Foundation attribute dictionary to an array of SwiftyAttributes. - **`Sequence.foundationAttributes`**: Converts an array of SwiftyAttributes to a Foundation attribute dictionary. ### Request Example ```swift // Foundation dictionary → [Attribute] let foundationDict: [NSAttributedString.Key: Any] = [ .font: UIFont.boldSystemFont(ofSize: 14), .foregroundColor: UIColor.systemBlue, .kern: NSNumber(value: 1.5) ] let swiftyAttrs: [Attribute] = foundationDict.swiftyAttributes // → [.font(...), .textColor(.systemBlue), .kern(1.5)] // [Attribute] → Foundation dictionary let attrs: [Attribute] = [ .textColor(.systemRed), .strikethroughStyle(.single), .strikethroughColor(.systemRed) ] let dict: [NSAttributedString.Key: Any] = attrs.foundationAttributes // → [.foregroundColor: UIColor.systemRed, .strikethroughStyle: 1, .strikethroughColor: UIColor.systemRed] // Useful when interoperating with UIKit APIs expecting Foundation dictionaries let label = UILabel() label.attributedText = NSAttributedString( string: label.text ?? "", attributes: attrs.foundationAttributes ) ``` ### Response These computed properties return the converted attribute collections. ``` -------------------------------- ### macOS-only Attributes with SwiftyAttributes Source: https://context7.com/eddiekaiger/swiftyattributes/llms.txt This code demonstrates macOS-specific attributes including tooltips, superscripts, custom cursors, and spelling error highlighting. It also shows how to retrieve font and ruler attributes within a specified range. ```swift #if os(macOS) // Tooltip on hover let tooltipStr = "Hover me" .withToolTip("This is a tooltip message") .withTextColor(.linkColor) // Superscript (e.g., footnotes) let footnote = "Reference" .withFont(.systemFont(ofSize: 10)) .withSuperscript(1) // Cursor let clickable = "Click here" .withCursor(.pointingHand) .withTextColor(.linkColor) // Spelling/grammar error highlight let misspelled = "Helo World" .withSpellingState(.spellingError) // Retrieve font attributes in range let richStr = NSAttributedString(string: "Bold", swiftyAttributes: [.font(.boldSystemFont(ofSize: 14))]) let fontAttrs: [Attribute] = richStr.fontAttributes(in: 0..<4) // Retrieve paragraph (ruler) attributes in range let parasStr = NSAttributedString(string: "Para", swiftyAttributes: [ .paragraphStyle(NSParagraphStyle.default) ]) let rulerAttrs: [Attribute] = parasStr.rulerAttributes(in: 0..<4) #endif ``` -------------------------------- ### Enumerate All Attributes in a Range Source: https://context7.com/eddiekaiger/swiftyattributes/llms.txt The `swiftyAttributes(in:options:)` method enumerates all attribute runs within a specified range, returning them as an array of tuples containing attributes and their corresponding ranges. ```APIDOC ## `NSAttributedString.swiftyAttributes(in:options:)` — Enumerate all attributes in a range Returns all attribute runs in a given `Range` as an array of `([Attribute], Range)` tuples. Uses Swift ranges natively, eliminating `NSRange` bridging. ### Example Usage: ```swift let richText = "Hello" .withTextColor(.systemRed) .withFont(.boldSystemFont(ofSize: 14)) + " World" .withTextColor(.systemBlue) .withFont(.systemFont(ofSize: 14)) .withUnderlineStyle(.single) // Enumerate all attribute runs over the full string let runs = richText.swiftyAttributes(in: 0.. NSMutableAttributedString func withAttribute(_ attribute: Attribute) -> NSMutableAttributedString } ``` -------------------------------- ### Measure Text Size and Bounding Rectangle Source: https://context7.com/eddiekaiger/swiftyattributes/llms.txt Cross-platform extensions on NSString for measuring text size and bounding rectangle using [Attribute] arrays instead of Foundation attribute dictionaries. Use swiftySize for natural size and swiftyBoundingRect for constrained sizes. ```swift let text: NSString = "Measure me" let attrs: [Attribute] = [ .font(.boldSystemFont(ofSize: 18)), .kern(1.0) ] // Get the natural size of the string let size = text.swiftySize(withSwiftyAttributes: attrs) print(size) // e.g., (98.5, 21.48...) // Get bounding rect within a constrained width let boundingRect = text.swiftyBoundingRect( with: CGSize(width: 200, height: CGFloat.greatestFiniteMagnitude), options: [.usesLineFragmentOrigin, .usesFontLeading], swiftyAttributes: attrs, context: nil ) print(boundingRect.size) // constrained size ``` -------------------------------- ### Dictionary and Sequence Conversion for Attributes Source: https://context7.com/eddiekaiger/swiftyattributes/llms.txt Bidirectional conversion between Foundation attribute dictionaries and SwiftyAttributes [Attribute] arrays via computed properties. Useful for interoperating with UIKit APIs expecting Foundation dictionaries. ```swift // Foundation dictionary → [Attribute] let foundationDict: [NSAttributedString.Key: Any] = [ .font: UIFont.boldSystemFont(ofSize: 14), .foregroundColor: UIColor.systemBlue, .kern: NSNumber(value: 1.5) ] let swiftyAttrs: [Attribute] = foundationDict.swiftyAttributes // → [.font(...), .textColor(.systemBlue), .kern(1.5)] // [Attribute] → Foundation dictionary let attrs: [Attribute] = [ .textColor(.systemRed), .strikethroughStyle(.single), .strikethroughColor(.systemRed) ] let dict: [NSAttributedString.Key: Any] = attrs.foundationAttributes // → [.foregroundColor: UIColor.systemRed, .strikethroughStyle: 1, .strikethroughColor: UIColor.systemRed] // Useful when interoperating with UIKit APIs expecting Foundation dictionaries let label = UILabel() label.attributedText = NSAttributedString( string: label.text ?? "", attributes: attrs.foundationAttributes ) ``` -------------------------------- ### Create Attributed String with Multiple Attributes using Enum Source: https://github.com/eddiekaiger/swiftyattributes/blob/master/README.md Shows how to create an attributed string with multiple attributes by using the Attribute enum and a dictionary. ```swift let fancyString = "Hello World!".withAttributes([ .backgroundColor(.magenta), .strokeColor(.orange), .strokeWidth(1), .baselineOffset(5.2) ]) ``` -------------------------------- ### SwiftyAttributes: String with Single Attribute Source: https://context7.com/eddiekaiger/swiftyattributes/llms.txt Applies a single text color attribute to a string literal using a convenience method. ```swift // Single attribute via convenience method let title = "Hello, World!" .withTextColor(.systemBlue) ``` -------------------------------- ### SwiftyAttributes: Chained String Styling Source: https://context7.com/eddiekaiger/swiftyattributes/llms.txt Applies multiple text styling attributes to a string literal using chained convenience methods. ```swift // All convenience with* methods on String let styled = "SwiftyAttributes" .withFont(.boldSystemFont(ofSize: 20)) .withTextColor(.label) .withUnderlineStyle(.single) .withUnderlineColor(.systemPurple) .withKern(2.0) ``` -------------------------------- ### Block-based Attribute Enumeration Source: https://context7.com/eddiekaiger/swiftyattributes/llms.txt The `enumerateSwiftyAttributes(in:options:using:)` method provides a block-based approach to enumerate attribute runs within a range, allowing for early exit and processing of attributes as `Attribute` types. ```APIDOC ## `NSAttributedString.enumerateSwiftyAttributes(in:options:using:)` — Block-based attribute enumeration Calls a closure for each attribute run in the specified range, passing `[Attribute]` and `Range` instead of Foundation types. Supports early exit via the `stop` pointer. ### Example Usage: ```swift let annotated = "Swift is great!" .withAttributes([.textColor(.label), .font(.systemFont(ofSize: 14))]) // Find the first run containing a text color var foundColor: Color? annotated.enumerateSwiftyAttributes(in: 0..`. ```swift extension NSAttributedString { convenience init(string str: String, swiftyAttributes: [Attribute]) func withAttributes(_ attributes: [Attribute]) -> NSMutableAttributedString func withAttribute(_ attribute: Attribute) -> NSMutableAttributedString func attributedSubstring(from range: Range) -> NSAttributedString func swiftyAttribute(_ attrName: NSAttributedStringKey, at location: Int, effectiveRange range: NSRangePointer? = nil) -> Attribute? func swiftyAttributes(in range: Range, options: NSAttributedString.EnumerationOptions = []) -> [([Attribute], Range)] func enumerateSwiftyAttributes(in enumerationRange: Range, options: NSAttributedString.EnumerationOptions = [], using block: (_ attrs: [Attribute], _ range: Range, _ stop: UnsafeMutablePointer) -> Void) func enumerateSwiftyAttribute(_ attrName: NSAttributedStringKey, in enumerationRange: Range, options: NSAttributedString.EnumerationOptions = [], using block: (_ value: Any?, _ range: Range, _ stop: UnsafeMutablePointer) -> Void } ``` -------------------------------- ### Chaining `with*` convenience methods on String Source: https://context7.com/eddiekaiger/swiftyattributes/llms.txt Chain multiple `with*` calls to apply various attributes like font, color, background, kern, and baseline offset to a string. Each call returns a new `NSMutableAttributedString` for infinite chaining. ```swift let badge = "NEW" .withFont(.systemFont(ofSize: 11, weight: .semibold)) .withTextColor(.white) .withBackgroundColor(.systemRed) .withKern(1.2) .withBaselineOffset(1.0) ``` ```swift let shadow = NSShadow() shadow.shadowColor = UIColor.black.withAlphaComponent(0.4) shadow.shadowOffset = CGSize(width: 0, height: 2) shadow.shadowBlurRadius = 4 let shadowed = "Drop Shadow" .withFont(.boldSystemFont(ofSize: 24)) .withTextColor(.white) .withShadow(shadow) ``` ```swift let attachment = NSTextAttachment() attachment.image = UIImage(systemName: "star.fill") attachment.bounds = CGRect(x: 0, y: -3, width: 16, height: 16) let withIcon = "Rating ".attributedString + NSAttributedString(attachment: attachment).withTextColor(.systemYellow) ``` ```swift let outlined = "Outlined" .withStrokeColor(.systemBlue) .withStrokeWidth(3.0) // positive = stroke only; negative = fill + stroke .withTextColor(.white) ``` ```swift let skewed = "Skewed Text" .withObliqueness(0.3) .withExpansion(0.15) ``` ```swift let linkText = "Visit Swift.org" .withTextColor(.systemBlue) .withUnderlineStyle(.single) .withLink(URL(string: "https://swift.org")!) ``` ```swift let letterpress = "Letterpress" .withFont(.systemFont(ofSize: 28, weight: .bold)) .withTextEffect(.letterPressStyle) ``` ```swift let rtl = "مرحبا" .withWritingDirections([.rightToLeft(.override)]) ``` ```swift let annotated = "Important" .withCustomAttribute(named: "priority", value: 1) ``` -------------------------------- ### Block-Based Attribute Enumeration with `enumerateSwiftyAttributes` Source: https://context7.com/eddiekaiger/swiftyattributes/llms.txt Iterate over attribute runs within a range using a closure with `enumerateSwiftyAttributes(in:options:using:)`. The closure receives `[Attribute]` and `Range`, and supports early exit via a `stop` pointer, making it efficient for finding specific attributes. ```swift let annotated = "Swift is great!" .withAttributes([.textColor(.label), .font(.systemFont(ofSize: 14))]) var foundColor: Color? annotated.enumerateSwiftyAttributes(in: 0.., yielding the raw value and a Swift range to the closure. ```APIDOC ## NSAttributedString.enumerateSwiftyAttribute(_:in:options:using:) ### Description Enumerates runs for a specific `NSAttributedString.Key` within a `Range`, yielding the raw value and a Swift range to the closure. ### Method `enumerateSwiftyAttribute(_:in:options:using:)` ### Parameters #### Path Parameters - **attributeKey** (`NSAttributedString.Key`) - Required - The attribute key to enumerate. - **range** (`Range`) - Required - The range within the attributed string to search. - **options** (`NSAttributedString.EnumerationOptions`) - Optional - Options for enumeration. - **closure** (`(Any?, Range, UnsafeMutablePointer) -> Void`) - Required - The closure to execute for each attribute run. ### Request Example ```swift let mutable = NSMutableAttributedString("Red Blue Green") // ... add attributes ... mutable.enumerateSwiftyAttribute(.foregroundColor, in: 0.. NSMutableAttributedString { return "Highlighted".withAttributes { Attribute.backgroundColor(color) Attribute.font(.boldSystemFont(ofSize: 14)) Attribute.textColor(.white) Attribute.kern(1.0) } } let redHighlight = highlightedStyle(color: .systemRed) let greenHighlight = highlightedStyle(color: .systemGreen) ``` -------------------------------- ### Range-based Attribute Mutation Source: https://context7.com/eddiekaiger/swiftyattributes/llms.txt Extensions on `NSMutableAttributedString` provide methods for adding, setting, and removing attributes within a specified Swift `Range`, simplifying attribute management. ```APIDOC ## `NSMutableAttributedString.addAttributes(_:range:)` / `setAttributes(_:range:)` — Range-based attribute mutation Extensions on `NSMutableAttributedString` accepting `[Attribute]` and Swift `Range` (instead of `NSRange`), removing the need for manual `NSRange` conversion. ### Example Usage: ```swift let mutable = NSMutableAttributedString(string: "Hello, Swift World!") // Highlight "Swift" (range 7..<12) using Swift Range mutable.addAttributes([ .textColor(.systemOrange), .font(.boldSystemFont(ofSize: 16)) ], range: 7..<12) // Replace attributes entirely for "World" (range 13..<18) mutable.setAttributes([ .textColor(.systemGreen), .underlineStyle(.single), .underlineColor(.systemGreen) ], range: 13..<18) // Remove an attribute from a range mutable.removeAttribute(.foregroundColor, range: 7..<12) // Replace characters in a range mutable.replaceCharacters(in: 0..<5, with: "Goodbye") // Delete characters in range mutable.deleteCharacters(in: 7..<8) // removes the comma ``` ``` -------------------------------- ### Integrate SwiftyAttributes with AttributedString (iOS 15+) Source: https://context7.com/eddiekaiger/swiftyattributes/llms.txt An extension on AttributeContainer (Swift's AttributedString API) that applies a SwiftyAttributes Attribute to a container. This bridges the old and new attributed string APIs for iOS 15 and later. ```swift if #available(iOS 15.0, *) { var container = AttributeContainer() // Apply individual Attribute cases to an AttributeContainer let swiftyAttr = Attribute.textColor(.systemPurple) container.set(attribute: swiftyAttr) container.set(attribute: .font(.boldSystemFont(ofSize: 16))) container.set(attribute: .kern(1.2)) container.set(attribute: .underlineStyle(.single)) // Use the container with Swift's AttributedString var attributedString = AttributedString("Modern API") attributedString.mergeAttributes(container) // Display in a SwiftUI Text view // Text(attributedString) } ``` -------------------------------- ### Retrieve Swifty Attribute at Specific Location Source: https://github.com/eddiekaiger/swiftyattributes/blob/master/README.md Demonstrates how to retrieve a specific Swifty attribute from an `NSAttributedString` at a given location. ```swift let attr: Attribute? = myAttributedString.swiftyAttribute(.shadow, at: 5) ``` -------------------------------- ### Enumerate Single Attribute with NSAttributedString Source: https://context7.com/eddiekaiger/swiftyattributes/llms.txt Enumerates runs for a specific NSAttributedString.Key within a Range, yielding the raw value and a Swift range to the closure. Ensure attributes are added to the NSMutableAttributedString before enumeration. ```swift let mixed = "Red Blue Green" .withTextColor(.systemRed) let mutable = mixed.mutableCopy() as! NSMutableAttributedString mutable.addAttributes([.textColor(.systemBlue)], range: 4..<8) mutable.addAttributes([.textColor(.systemGreen)], range: 9..<14) mutable.enumerateSwiftyAttribute(.foregroundColor, in: 0.. instead of NSRange. ```APIDOC ## NSAttributedString.attributedSubstring(from:) ### Description Returns a substring `NSAttributedString` using a Swift `Range` instead of `NSRange`. ### Method `attributedSubstring(from:)` ### Parameters #### Path Parameters - **range** (`Range`) - Required - The Swift range specifying the substring. ### Request Example ```swift let full = "Hello, Swift!" .withFont(.boldSystemFont(ofSize: 16)) .withTextColor(.label) let sub = full.attributedSubstring(from: 7..<12) print(sub.string) // "Swift" ``` ### Response #### Success Response (200) - **NSAttributedString** - The substring with its attributes preserved. ``` -------------------------------- ### AttributeContainer.set(attribute:) Source: https://context7.com/eddiekaiger/swiftyattributes/llms.txt An extension on the modern AttributeContainer type (iOS 15+) that applies a SwiftyAttributes Attribute to a container, bridging the old and new attributed string APIs. ```APIDOC ## AttributeContainer.set(attribute:) ### Description An extension on the modern `AttributeContainer` type (Swift's `AttributedString` API, iOS 15+/tvOS 15+/watchOS 8+) that applies a SwiftyAttributes `Attribute` to a container, bridging the old and new attributed string APIs. ### Method `set(attribute:)` ### Parameters #### Path Parameters - **attribute** (`Attribute`) - Required - The SwiftyAttributes `Attribute` to set on the container. ### Request Example ```swift if #available(iOS 15.0, *) { var container = AttributeContainer() // Apply individual Attribute cases to an AttributeContainer let swiftyAttr = Attribute.textColor(.systemPurple) container.set(attribute: swiftyAttr) container.set(attribute: .font(.boldSystemFont(ofSize: 16))) container.set(attribute: .kern(1.2)) container.set(attribute: .underlineStyle(.single)) // Use the container with Swift's AttributedString var attributedString = AttributedString("Modern API") attributedString.mergeAttributes(container) // Display in a SwiftUI Text view // Text(attributedString) } ``` ### Response This method modifies the `AttributeContainer` in place. It does not return a value. ``` -------------------------------- ### Attributed String Concatenation Source: https://context7.com/eddiekaiger/swiftyattributes/llms.txt Overloaded `+` and `+=` operators allow for easy composition of attributed strings, where each segment can have distinct formatting. ```APIDOC ## `+` and `+=` operators — Attributed string concatenation Overloaded `+` and `+=` operators on `NSAttributedString` and `NSMutableAttributedString` for composing multi-segment attributed strings where each segment can have completely different formatting. ### Example Usage: ```swift // Build a rich label from segments let price = "$" .withFont(.systemFont(ofSize: 14, weight: .regular)) .withTextColor(.secondaryLabel) + "29" .withFont(.boldSystemFont(ofSize: 32)) .withTextColor(.label) + ".99" .withFont(.systemFont(ofSize: 14, weight: .regular)) .withTextColor(.secondaryLabel) .withBaselineOffset(12) // Append with += var message = "Hello ".withTextColor(.label) message += "World".withTextColor(.systemBlue).withUnderlineStyle(.single) message += "!".withFont(.boldSystemFont(ofSize: 18)) // Multi-line rich text let body = "Note: " .withFont(.boldSystemFont(ofSize: 14)) .withTextColor(.label) + "This action cannot be undone." .withFont(.systemFont(ofSize: 14)) .withTextColor(.secondaryLabel) ``` ``` -------------------------------- ### SwiftyAttributes: Plain String Conversion Source: https://context7.com/eddiekaiger/swiftyattributes/llms.txt Converts a plain string to an `NSMutableAttributedString` without any attributes using the `.attributedString` computed property. ```swift // Raw .attributedString property (no attributes) let plain: NSMutableAttributedString = "Plain text".attributedString ``` -------------------------------- ### Extract Substring with Swift Range Source: https://context7.com/eddiekaiger/swiftyattributes/llms.txt Returns a substring NSAttributedString using a Swift Range instead of NSRange. The substring retains all attributes from the original attributed string. ```swift let full = "Hello, Swift!" .withFont(.boldSystemFont(ofSize: 16)) .withTextColor(.label) // Extract "Swift" (characters 7..<12) let sub = full.attributedSubstring(from: 7..<12) print(sub.string) // "Swift" // The substring retains all attributes from the original if let fontAttr = sub.swiftyAttribute(.font, at: 0), case .font(let font) = fontAttr { print(font.fontDescriptor.postscriptName) // retains bold font } ``` -------------------------------- ### Attribute Enum Source: https://context7.com/eddiekaiger/swiftyattributes/llms.txt The Attribute enum provides type-safe representations for various attributed string properties. It allows for easy inspection and comparison of attributes. ```APIDOC ## Attribute Enum ### Description The `Attribute` enum is the central abstraction of SwiftyAttributes. Each case corresponds to a standard `NSAttributedString.Key`, carrying its associated typed value. It exposes `keyName` (`NSAttributedString.Key`), `value` (the Swift-typed associated value), and `foundationValue` (the raw value passed to Foundation APIs). Custom attributes are supported via `.custom(String, Any)`. ### Usage Examples ```swift // All available Attribute cases (cross-platform) let attrs: [Attribute] = [ .font(.systemFont(ofSize: 16, weight: .bold)), .textColor(.white), .backgroundColor(.systemBlue), .kern(1.5), .ligatures(.default), .baselineOffset(2.0), .obliqueness(0.2), .expansion(0.1), .strokeColor(.black), .strokeWidth(-2.0), .underlineStyle(.single), .underlineColor(.red), .strikethroughStyle(.double), .strikethroughColor(.gray), .paragraphStyle({ let style = NSMutableParagraphStyle() style.alignment = .center style.lineSpacing = 4 return style }()), .link(URL(string: "https://example.com")!), .textEffect(.letterPressStyle), .writingDirections([.leftToRight(.override)]), .custom("MyAppHighlight", true) ] // Inspect a single attribute let colorAttr = Attribute.textColor(.systemRed) print(colorAttr.keyName) // NSAttributedString.Key.foregroundColor print(colorAttr.value) // UIColor.systemRed print(colorAttr.foundationValue) // UIColor.systemRed (same here; differs for enums like .ligatures) // Equality check let a = Attribute.kern(2.0) let b = Attribute.kern(2.0) print(a == b) // true ``` ``` -------------------------------- ### Mutate NSMutableAttributedString with Swift Ranges Source: https://context7.com/eddiekaiger/swiftyattributes/llms.txt Utilize `addAttributes`, `setAttributes`, `removeAttribute`, `replaceCharacters`, and `deleteCharacters` with Swift `Range` for direct manipulation of `NSMutableAttributedString`. This avoids manual `NSRange` conversions. ```swift let mutable = NSMutableAttributedString(string: "Hello, Swift World!") mutable.addAttributes([ .textColor(.systemOrange), .font(.boldSystemFont(ofSize: 16)) ], range: 7..<12) mutable.setAttributes([ .textColor(.systemGreen), .underlineStyle(.single), .underlineColor(.systemGreen) ], range: 13..<18) mutable.removeAttribute(.foregroundColor, range: 7..<12) mutable.replaceCharacters(in: 0..<5, with: "Goodbye") mutable.deleteCharacters(in: 7..<8) // removes the comma ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.