A protocol that provides common functionality to attributed strings and attributed substrings.
Don't declare new conformances to ``AttributedStringProtocol``. Only the ``AttributedString`` and ``AttributedSubstring`` types in the standard library are valid conforming types.
Members (15)
get · iOS 15.0+
description
Swift declaration
var description: String { get }
A textual representation of this instance.
Calling this property directly is discouraged. Instead, convert an
instance of any type to a string by using the `String(describing:)`
initializer. This initializer works with any type, and uses the custom
`description` property for types that conform to
`CustomStringConvertible`:
struct Point: CustomStringConvertible {
let x: Int, y: Int
var description: String {
return "(\(x), \(y))"
}
}
let p = Point(x: 21, y: 30)
let s = String(describing: p)
print(s)
// Prints "(21, 30)"
The conversion of `p` to a string in the assignment to `s` uses the
`Point` type's `description` property.
Returns an attributed string by merging the attributed string's attributes with those in a specified attribute container.
- Parameters:
- attributes: The attribute container with the attributes to merge.
- mergePolicy: A policy to use when resolving conflicts between this string's attributes and those in `attributes`.
Returns an attributed string by setting the attributed string's attributes to those in a specified attribute container.
- Parameter attributes: The attribute container with the attributes to apply.
- Returns: An attributed string from setting the attributed string's attributes to those in a specified attribute container.
method · iOS 15.0+
replacingAttributes
Swift declaration
func replacingAttributes(_ attributes: AttributeContainer, with others: AttributeContainer) -> AttributedString
Returns an attributed string by replacing occurrences of attributes in one attribute container with those in another attribute container.
- Parameters:
- attributes: The existing attributes to replace.
- others: The new attributes to apply.
Returns the position of the character immediately after another charcter indicated by an index.
- Parameter i: The index of a character in the attributed string.
- Returns: The position of the character immediately after the character at index `i`.
Returns the position of the character immediately before another charcter indicated by an index.
- Parameter i: The index of a character in the attributed string.
- Returns: The position of the character immediately before the character at index `i`.
Returns the position of the Unicode scalar immediately after a Unicode scalar indicated by an index.
- Parameter i: The index of a Unicode scalar in the attributed string.
- Returns: The position of the Unicode scalar immediately after the Unicode scalar at index `i`.
Returns the position of the Unicode scalar immediately before a Unicode scalar indicated by an index.
- Parameter i: The index of a Unicode scalar in the attributed string.
- Returns: The position of the Unicode scalar immediately before the Unicode scalar at index `i`.
Returns the position of the run immediately after a run indicated by an index.
- Parameter i: The index of a run in the attributed string.
- Returns: The position of first run immediately after the end of `i`-th run.
Returns the position of the run immediately before a run indicated by an index.
- Parameter i: The index of a run in the attributed string.
- Returns: The position of the first run immediately before the beginning of the `i`-th run.
Returns the position of the run offset a given number of runs from a given string index.
- Parameters:
- i: The index of a position in the string.
- distance: The number of runs to advance by.
Returns the position of the character offset a given distance, measured in characters, from a given string index.
- Parameters:
- i: The index of a position in the string.
- distance: The number of charcters to advance by.
Returns the position of the Unicode scalar offset a given distance, measured in Unicode scalars, from a given string index.
- Parameters:
- i: The index of a position in the string.
- distance: The number of Unicode scalars to advance by.
method · iOS 15.0+
range
Swift declaration
func range<T>(of stringToFind: T, options: String.CompareOptions = [], locale: Locale? = nil) -> Range<AttributedString.Index>? where T : StringProtocol
Returns the range of a substring in the attributed string, if it exists.
- Parameters:
- stringToFind: The string to find.
- options: Options that affect the search behavior, such as case-insensivity, search direction, and regular expression matching.
- locale: The locale to use when searching, or `nil` to use the current locale. The default is `nil`.
subscript · iOS 26.0+
subscript
Swift declaration
subscript(indices: RangeSet<AttributedString.Index>) -> DiscontiguousAttributedSubstring { get }
Returns a discontiguous substring of this attributed string using a set of ranges to indicate the discontiguous substring bounds.
- Parameter indices: A set of ranges that indicate the bounds of the discontiguous substring to return.