init()
init(AttributedSubstring)
init(String, attributes: AttributeContainer)
init(Substring, attributes: AttributeContainer)
init<S>(S, attributes: AttributeContainer)
struct AttributeContainer
Availability 有効性
Technology
@dynamicMemberLookup struct AttributedString
Attributed strings are character strings that have attributes for individual characters or ranges of characters. Attributes provide traits like visual styles for display, accessibility for guided access, and hyperlink data for linking between data sources. Attribute keys provide the name and value type of each attribute. System frameworks like Foundation and SwiftUI define common keys, and you can define your own in custom extensions. 属性キーは、各属性の名前と値の型を提供します。Foundation および SwiftUI のようなシステムフレームワークは、共通のキーを定義します、そしてあなたはあなた独自のものをあつらえの拡張の中で定義できます。
You can apply an attribute to an entire string, or to a range within the string. The string represents each range with consistent attributes as a run.
Attributed
uses subscripts and dynamic member lookup to simplify working with attributes from your call points. In its most verbose form, you set an attribute by creating an Attribute
and merging it into an existing attributed string, like this:
Using the attributed string’s subscript(_:)
method, you can omit the explicit use of an Attribute
and just set the attribute by its type:
Because an Attributed
supports dynamic member lookup — as described under Attributes in The Swift Programming Language — you can access its subscripts with dot syntax instead. When combined with properties like foreground
that return the attribute key type, this final form offers a natural way to set an attribute that applies to an entire string:
This example works because AppKit defines an Attribute
, Attribute
, in which the property foreground
returns the type Attribute
. Because AppKit’s attribute scope implements Attribute
, the dot syntax resolves to an equivalent subscript expression, allowing attributed
to replace attributed
.
You can also set an attribute to apply only to part of an attributed string, by applying the attribute to a range, as seen here:
You can access portions of the string with unique combinations of attributes by iterating over the string’s runs
property.
あなたは属性の固有の組み合わせをもつ文字列の一部分に、文字列のもつruns
プロパティに対して反復していくことによってアクセスできます。
You can define your own custom attributes by creating types that conform to Attributed
, and collecting them in an Attribute
. Custom keys should also extend Attribute
, so callers can use dot-syntax to access the attribute.
You can create an attributed string by passing a standard String
or Data
instance that contains Markdown to initializers like init(markdown:
. The attributed string creates attributes by parsing the markup in the string.
Localized strings that you load from strings files with initializers like init(localized:
can also contain Markdown to add styling. In addition, these localized attributed string initializers can apply the replacement
attribute, which allows you to determine the range of replacement strings, whose order may vary between languages.
By declaring new attributes that conform to Markdown
, you can add attributes that you invoke by using Apple’s Markdown extension syntax: ^[text](name:
. See the sample code project Building a Localized Food-Ordering App for an example of creating custom attributes and using them with Markdown.
Localized attributed strings can also use the extension syntax to indicate parts of the string where the system can apply automatic grammar agreement. See the initializers that take a localized:
parameter for examples of this extension syntax, as used with automatic grammar agreement.
The Attributed
API defines keys for common uses, such as text styling, semantically marking up formattable types like dates and numbers, and hyperlinking.
Attributed
API は、一般的な利用のためのキーを定義します、たとえばテキストスタイル、日付および数値のような意味論的なマークアップ書式設定可能型、、そしてハイパーリンクなど。
You can find these in the Attribute
enumeration, which contains attributes for AppKit, Foundation, SwiftUI, and UIKit.
You can define your own attributes by implementing Attributed
, and reference them by name by collecting them in an Attribute
.
あなたは、あなた独自の属性それらをAttributed
を実装することによって定義できます、そしてそれらを名前によって参照できます、それらをAttribute
の中に収集することによって。
init()
init(AttributedSubstring)
init(String, attributes: AttributeContainer)
init(Substring, attributes: AttributeContainer)
init<S>(S, attributes: AttributeContainer)
struct AttributeContainer
init(stringLiteral : String)
typealias AttributedString.StringLiteralType
init(unicodeScalarLiteral : String)
typealias AttributedString.UnicodeScalarLiteralType
init(extendedGraphemeClusterLiteral : String)
typealias AttributedString.ExtendedGraphemeClusterLiteralType
init(localized: String.LocalizationValue, options: AttributedString.FormattingOptions, table: String?, bundle: Bundle?, locale: Locale?, comment: StaticString?)
init<S>(localized: String.LocalizationValue, options: AttributedString.FormattingOptions, table: String?, bundle: Bundle?, locale: Locale?, comment: StaticString?, including: S.Type)
init<S>(localized: String.LocalizationValue, options: AttributedString.FormattingOptions, table: String?, bundle: Bundle?, locale: Locale?, comment: StaticString?, including: KeyPath<AttributeScopes, S.Type>)
struct String.LocalizationValue
struct AttributedString.FormattingOptions
init<S>(NSAttributedString, including: S.Type)
init<S>(NSAttributedString, including: KeyPath<AttributeScopes, S.Type>)
init(NSAttributedString)
init<S, T>(T, including: S.Type)
init<S, T>(T, including: KeyPath<AttributeScopes, S.Type>)
func setAttributes (AttributeContainer)
func settingAttributes (AttributeContainer) -> AttributedString
func mergeAttributes (AttributeContainer, mergePolicy : AttributedString.AttributeMergePolicy)
func mergingAttributes (AttributeContainer, mergePolicy : AttributeMergePolicy) -> AttributedString
enum AttributedString.AttributeMergePolicy
func replaceAttributes (AttributeContainer, with: AttributeContainer)
func replacingAttributes (AttributeContainer, with: AttributeContainer) -> AttributedString
protocol AttributedStringAttributeMutation
enum AttributeScopes
enum AttributeDynamicLookup
struct ScopedAttributeContainer
func range<T>(of: T, options: String.CompareOptions, locale: Locale?) -> Range<Index>?
subscript<R>(R) -> AttributedSubstring
var characters: AttributedString.CharacterView
struct AttributedString.CharacterView
var unicodeScalars : AttributedString.UnicodeScalarView
struct AttributedString.UnicodeScalarView
var runs: AttributedString.Runs
struct AttributedString.Runs
func insert<S>(S, at: AttributedString.Index)
struct AttributedString.Index
func removeSubrange <R>(R)
func replaceSubrange <R, S>(R, with: S)
func transformingAttributes <K>(K.Type, (inout AttributedString.SingleAttributeTransformer<K>) -> Void) -> AttributedString
func transformingAttributes <K>(KeyPath<AttributeDynamicLookup, K>, (inout AttributedString.SingleAttributeTransformer<K>) -> Void) -> AttributedString
func transformingAttributes <K1, K2>(K1.Type, K2.Type, (inout AttributedString.SingleAttributeTransformer<K1>, inout AttributedString.SingleAttributeTransformer<K2>) -> Void) -> AttributedString
func transformingAttributes <K1, K2>(KeyPath<AttributeDynamicLookup, K1>, KeyPath<AttributeDynamicLookup, K2>, (inout AttributedString.SingleAttributeTransformer<K1>, inout AttributedString.SingleAttributeTransformer<K2>) -> Void) -> AttributedString
func transformingAttributes <K1, K2, K3>(K1.Type, K2.Type, K3.Type, (inout AttributedString.SingleAttributeTransformer<K1>, inout AttributedString.SingleAttributeTransformer<K2>, inout AttributedString.SingleAttributeTransformer<K3>) -> Void) -> AttributedString
func transformingAttributes <K1, K2, K3>(KeyPath<AttributeDynamicLookup, K1>, KeyPath<AttributeDynamicLookup, K2>, KeyPath<AttributeDynamicLookup, K3>, (inout AttributedString.SingleAttributeTransformer<K1>, inout AttributedString.SingleAttributeTransformer<K2>, inout AttributedString.SingleAttributeTransformer<K3>) -> Void) -> AttributedString
func transformingAttributes <K1, K2, K3, K4>(K1.Type, K2.Type, K3.Type, K4.Type, (inout AttributedString.SingleAttributeTransformer<K1>, inout AttributedString.SingleAttributeTransformer<K2>, inout AttributedString.SingleAttributeTransformer<K3>, inout AttributedString.SingleAttributeTransformer<K4>) -> Void) -> AttributedString
func transformingAttributes <K1, K2, K3, K4>(KeyPath<AttributeDynamicLookup, K1>, KeyPath<AttributeDynamicLookup, K2>, KeyPath<AttributeDynamicLookup, K3>, KeyPath<AttributeDynamicLookup, K4>, (inout AttributedString.SingleAttributeTransformer<K1>, inout AttributedString.SingleAttributeTransformer<K2>, inout AttributedString.SingleAttributeTransformer<K3>, inout AttributedString.SingleAttributeTransformer<K4>) -> Void) -> AttributedString
func transformingAttributes <K1, K2, K3, K4, K5>(K1.Type, K2.Type, K3.Type, K4.Type, K5.Type, (inout AttributedString.SingleAttributeTransformer<K1>, inout AttributedString.SingleAttributeTransformer<K2>, inout AttributedString.SingleAttributeTransformer<K3>, inout AttributedString.SingleAttributeTransformer<K4>, inout AttributedString.SingleAttributeTransformer<K5>) -> Void) -> AttributedString
func transformingAttributes <K1, K2, K3, K4, K5>(KeyPath<AttributeDynamicLookup, K1>, KeyPath<AttributeDynamicLookup, K2>, KeyPath<AttributeDynamicLookup, K3>, KeyPath<AttributeDynamicLookup, K4>, KeyPath<AttributeDynamicLookup, K5>, (inout AttributedString.SingleAttributeTransformer<K1>, inout AttributedString.SingleAttributeTransformer<K2>, inout AttributedString.SingleAttributeTransformer<K3>, inout AttributedString.SingleAttributeTransformer<K4>, inout AttributedString.SingleAttributeTransformer<K5>) -> Void) -> AttributedString
struct AttributedString.SingleAttributeTransformer
subscript<K>(K.Type) -> K.Value?
subscript<K>(dynamicMember : KeyPath<AttributeDynamicLookup, K>) -> K.Value?
enum AttributeDynamicLookup
subscript<S>(dynamicMember : KeyPath<AttributeScopes, S.Type>) -> ScopedAttributeContainer<S>
struct ScopedAttributeContainer
func append<S>(S)
static func + (AttributedString, AttributedString) -> AttributedString
static func + <T>(AttributedString, T) -> AttributedString
static func += (inout AttributedString, AttributedString)
static func += <T>(inout AttributedString, T)
func inflected() -> AttributedString
struct AttributedString.InterpolationOptions
static func == (AttributedString, AttributedString) -> Bool
static func == <RHS>(AttributedString, RHS) -> Bool
static func != (AttributedString, AttributedString) -> Bool
var hashValue : Int
func hash(into: inout Hasher)
init(from: Decoder)
init(from: Decoder, configuration: AttributeScopeCodableConfiguration)
typealias AttributedString.DecodingConfiguration
func encode(to: Encoder)
func encode(to: Encoder, configuration: AttributeScopeCodableConfiguration)
typealias AttributedString.EncodingConfiguration
struct AttributeScopeCodableConfiguration
var description: String
struct AttributedSubstring
class NSAttributedString
class NSMutableAttributedString