I need to search some strings and set some attributes prior to merging the strings, so having NSStrings -> Concatenate them -> Make NSAttributedString is not an option, is there any way to concatenate attributedString to another attributedString?
8 回答
我建议您使用@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:
在事后将属性添加到包含输入字符串的范围中。我推荐前一种方式。
如果您使用的是 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")
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
swift5更新:
let captionAttribute = [
NSAttributedString.Key.font: Font.captionsRegular,
NSAttributedString.Key.foregroundColor: UIColor.appGray
]
尝试这个:
NSMutableAttributedString* result = [astring1 mutableCopy];
[result appendAttributedString:astring2];
在哪里astring1
和astring2
是NSAttributedString
s。
2020 | 斯威夫特 5.1:
您可以NSMutableAttributedString
通过以下方式添加 2:
let concatenated = NSAttrStr1.append(NSAttrStr2)
另一种方法适用于NSMutableAttributedString
两者NSAttributedString
:
[NSAttrStr1, NSAttrStr2].joinWith(separator: "")
另一种方法是......
var full = NSAttrStr1 + NSAttrStr2 + NSAttrStr3
和:
var full = NSMutableAttributedString(string: "hello ")
// NSAttrStr1 == 1
full += NSAttrStr1 // full == "hello 1"
full += " world" // full == "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
}
}
如果您使用的是 Cocoapods,则上述两个答案的替代方法可以让您避免自己代码中的可变性,即在 s 上使用出色的NSAttributedString+CCLFormat类别NSAttributedString
,让您可以编写如下内容:
NSAttributedString *first = ...;
NSAttributedString *second = ...;
NSAttributedString *combined = [NSAttributedString attributedStringWithFormat:@"%@%@", first, second];
它当然只是NSMutableAttributedString
在幕后使用。
它还具有一个完全成熟的格式化功能的额外优势——因此它可以做的不仅仅是将字符串附加在一起。
// 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;
}
您可以尝试SwiftyFormat 它使用以下语法
let format = "#{{user}} mentioned you in a comment. #{{comment}}"
let message = NSAttributedString(format: format,
attributes: commonAttributes,
mapping: ["user": attributedName, "comment": attributedComment])