Instance Method インスタンスメソッド

mergingAttributes(_:mergePolicy:)

Returns an attributed string by merging the attributed string’s attributes with those in a specified attribute container.

Declaration 宣言

func mergingAttributes(_ attributes: AttributeContainer, mergePolicy: AttributeMergePolicy = .keepNew) -> AttributedString

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.

Return Value 戻り値

An attributed string with attributed merged from the source string and the provided attributes. In cases where the same attribute exists in both the source string and attributes, the mergePolicy determines which value the returned string uses.

Discussion 議論

The following example shows an AttributedString created with the mergingAttributes(_:mergePolicy:) method. The source string contains both foreground and background NSColor color attributes, while the targetAttributes attribute container includes a different background color value. With a mergePolicy of AttributedString.AttributeMergePolicy.keepNew, the target string uses the value from targetAttributes rather than the value from the original source string.


var sourceAttributes = AttributeContainer([
    .foregroundColor: NSColor.white,
    .backgroundColor: NSColor.green
])
let source = AttributedString("AttributedString",
                              attributes: sourceAttributes)
let targetAttributes = AttributeContainer([
    .backgroundColor: NSColor.blue
])
let target = source.mergingAttributes(targetAttributes,
                                      mergePolicy: .keepNew)

Since targetAttributes doesn’t provide a different foreground color, the target string copies the valeu from the source string.

See Also 参照

Applying and Modifying Attributes 属性の適用と修正