如何连接 NSAttributedString?

在合并字符串之前,我需要搜索一些字符串并设置一些属性,所以 NSStrings-> Concatenate them-> Make NSAttributedString 不是一个选项,有没有办法将 AttributedString 连接到另一个 AttributedString?

107789 次浏览

试试这个:

NSMutableAttributedString* result = [astring1 mutableCopy];
[result appendAttributedString:astring2];

其中 astring1astring2NSAttributedString

我建议您使用一个@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];

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

如果你正在使用 Cocoapods,除了上面两个答案之外,还有一个可以避免自己代码中可变性的方法,那就是在 NSAttributedString上使用优秀的 NSAttributedString + CCLFormat类别,它可以让你编写如下代码:

NSAttributedString *first = ...;
NSAttributedString *second = ...;
NSAttributedString *combined = [NSAttributedString attributedStringWithFormat:@"%@%@", first, second];

当然,它只是在掩护下使用 NSMutableAttributedString

它还有一个额外的优点,那就是它是一个完全成熟的格式化函数ーー因此它所能做的不仅仅是将字符串附加在一起。

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

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

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

let helloworld = NSAttributedString(string: "Hello ") + NSAttributedString(string: "World")
// Immutable approach
// class method


+ (NSAttributedString *)stringByAppendingString:(NSAttributedString *)append toString:(NSAttributedString *)string {
NSMutableAttributedString *result = [string mutableCopy];
[result appendAttributedString:append];
NSAttributedString *copy = [result copy];
return copy;
}


//Instance method
- (NSAttributedString *)stringByAppendingString:(NSAttributedString *)append {
NSMutableAttributedString *result = [self mutableCopy];
[result appendAttributedString:append];
NSAttributedString *copy = [result copy];
return copy;
}

您可以尝试 < a href = “ https://github.com/Igor-Palaguta/SwiftyFormat”rel = “ nofollow norefrer”> SwiftyFormat 它使用以下语法

let format = "#\{\{user}} mentioned you in a comment. #\{\{comment}}"
let message = NSAttributedString(format: format,
attributes: commonAttributes,
mapping: ["user": attributedName, "comment": attributedComment])

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

迅速上升:

    let captionAttribute = [
NSAttributedString.Key.font: Font.captionsRegular,
NSAttributedString.Key.foregroundColor: UIColor.appGray
]

2020 | SWIFT 5.1:

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

let concatenated = NSAttrStr1.append(NSAttrStr2)

另一种方法同时适用于 NSMutableAttributedStringNSAttributedString:

[NSAttrStr1, NSAttrStr2].joinWith(separator: "")

另一种方法是..。

var full = NSAttrStr1 + NSAttrStr2 + NSAttrStr3

以及:

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




full += NSAttrStr1 // "hello 1"
full += " world"   // "hello 1 world"

您可以通过以下扩展实现这一点:

// 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
}
}