如何连接NSAttributedStrings?

Iva*_*tic 142 nsattributedstring ios

我需要在合并字符串之前搜索一些字符串并设置一些属性,因此使用NSStrings - >连接它们 - >使NSAttributedString不是一个选项,有没有办法将attributedString连接到另一个referencedString?

Mic*_*lum 201

我建议您使用@Linuxios建议的单个可变属性字符串,这是另一个例子:

NSMutableAttributedString *mutableAttString = [[NSMutableAttributedString alloc] init];

NSString *plainString = // ...
NSDictionary *attributes = // ... a dictionary with your attributes.
NSAttributedString *newAttString = [[NSAttributedString alloc] initWithString:plainString attributes:attributes];

[mutableAttString appendAttributedString:newAttString];
Run Code Online (Sandbox Code Playgroud)

但是,只是为了获得所有选项,您还可以创建一个可变的属性字符串,由包含已放在一起的输入字符串的格式化NSString构成.然后,您可以使用addAttributes: range:将事实后的属性添加到包含输入字符串的范围.我推荐以前的方式.


alg*_*gal 77

如果你正在使用Swift,你可以重载+操作符,以便你可以像连接普通字符串一样连接它们:

// concatenate attributed strings
func + (left: NSAttributedString, right: NSAttributedString) -> NSAttributedString
{
    let result = NSMutableAttributedString()
    result.append(left)
    result.append(right)
    return result
}
Run Code Online (Sandbox Code Playgroud)

现在你可以通过添加它们来连接它们:

let helloworld = NSAttributedString(string: "Hello ") + NSAttributedString(string: "World")
Run Code Online (Sandbox Code Playgroud)

  • 是的,如果你想要防守,你应该做一个防御性的副本.(不是讽刺.) (6认同)
  • mutable类是不可变类的子类型. (5认同)
  • 您可以在任何需要不可变父类型的上下文中使用可变子类型,但反之亦然.您可能想要查看子类和继承. (4认同)
  • @ n13我将创建一个名为`Helpers`或`Extensions`的文件夹,并将此函数放在名为`NSAttributedString + Concatenate.swift`的文件中. (2认同)

Jos*_*nor 30

Swift 3:只需创建一个NSMutableAttributedString并将属性字符串附加到它们.

let mutableAttributedString = NSMutableAttributedString()

let boldAttribute = [
    NSFontAttributeName: UIFont(name: "GothamPro-Medium", size: 13)!,
    NSForegroundColorAttributeName: Constants.defaultBlackColor
]

let regularAttribute = [
    NSFontAttributeName: UIFont(name: "Gotham Pro", size: 13)!,
    NSForegroundColorAttributeName: Constants.defaultBlackColor
]

let boldAttributedString = NSAttributedString(string: "Warning: ", attributes: boldAttribute)
let regularAttributedString = NSAttributedString(string: "All tasks within this project will be deleted.  If you're sure you want to delete all tasks and this project, type DELETE to confirm.", attributes: regularAttribute)
mutableAttributedString.append(boldAttributedString)
mutableAttributedString.append(regularAttributedString)

descriptionTextView.attributedText = mutableAttributedString
Run Code Online (Sandbox Code Playgroud)


Lin*_*ios 25

试试这个:

NSMutableAttributedString* result = [astring1 mutableCopy];
[result appendAttributedString:astring2];
Run Code Online (Sandbox Code Playgroud)

在哪里astring1astring2NSAttributedString.

  • 或者`[[aString1 mutableCopy] appendAttributedString:aString2]`. (13认同)

And*_*rew 5

2020 | 斯威夫特 5.1:

您可以NSMutableAttributedString通过以下方式添加 2 :

let concatenated = NSAttrStr1.append(NSAttrStr2)
Run Code Online (Sandbox Code Playgroud)

另一种方式适用于NSMutableAttributedStringNSAttributedString两者:

[NSAttrStr1, NSAttrStr2].joinWith(separator: "")
Run Code Online (Sandbox Code Playgroud)

另一种方式是......

var full = NSAttrStr1 + NSAttrStr2 + NSAttrStr3
Run Code Online (Sandbox Code Playgroud)

和:

var full = NSMutableAttributedString(string: "hello ")
// NSAttrStr1 == 1


full += NSAttrStr1 // full == "hello 1"       
full += " world"   // full == "hello 1 world"
Run Code Online (Sandbox Code Playgroud)

您可以使用以下扩展程序执行此操作:

// works with NSAttributedString and NSMutableAttributedString!
public extension NSAttributedString {
    static func + (left: NSAttributedString, right: NSAttributedString) -> NSAttributedString {
        let leftCopy = NSMutableAttributedString(attributedString: left)
        leftCopy.append(right)
        return leftCopy
    }

    static func + (left: NSAttributedString, right: String) -> NSAttributedString {
        let leftCopy = NSMutableAttributedString(attributedString: left)
        let rightAttr = NSMutableAttributedString(string: right)
        leftCopy.append(rightAttr)
        return leftCopy
    }

    static func + (left: String, right: NSAttributedString) -> NSAttributedString {
        let leftAttr = NSMutableAttributedString(string: left)
        leftAttr.append(right)
        return leftAttr
    }
}

public extension NSMutableAttributedString {
    static func += (left: NSMutableAttributedString, right: String) -> NSMutableAttributedString {
        let rightAttr = NSMutableAttributedString(string: right)
        left.append(rightAttr)
        return left
    }

    static func += (left: NSMutableAttributedString, right: NSAttributedString) -> NSMutableAttributedString {
        left.append(right)
        return left
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我正在使用 Swift 5.1,我似乎无法将两个 NSAttrStrings 添加在一起...... (2认同)