ios 如何连接 NSAttributedStrings?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18518222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I concatenate NSAttributedStrings?
提问by Mick MacCallum
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?
我需要在合并字符串之前搜索一些字符串并设置一些属性,所以让 NSStrings -> 连接它们 -> 制作 NSAttributedString 不是一个选项,有没有办法将 attributesString 连接到另一个 attributedString?
回答by Mick MacCallum
I'd recommend you use a single mutable attributed string a @Linuxios suggested, and here's another example of that:
我建议您使用@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];
However, just for the sake of getting all the options out there, you could also create a single mutable attributed string, made from a formatted NSString containing the input strings already put together. You could then use addAttributes: range:
to add the attributes after the fact to the ranges containing the input strings. I recommend the former way though.
但是,为了获得所有选项,您还可以创建一个单一的可变属性字符串,该字符串由包含已放在一起的输入字符串的格式化 NSString 制成。然后,您可以使用addAttributes: range:
事后将属性添加到包含输入字符串的范围。不过我推荐前一种方式。
回答by algal
If you're using Swift, you can just overload the +
operator so that you can concatenate them in the same way you concatenate normal strings:
如果您使用 Swift,您可以重载+
运算符,以便您可以像连接普通字符串一样连接它们:
// concatenate attributed strings
func + (left: NSAttributedString, right: NSAttributedString) -> NSAttributedString
{
let result = NSMutableAttributedString()
result.append(left)
result.append(right)
return result
}
Now you can concatenate them just by adding them:
现在你可以通过添加它们来连接它们:
let helloworld = NSAttributedString(string: "Hello ") + NSAttributedString(string: "World")
回答by Josh O'Connor
Swift 3: Simply create a NSMutableAttributedString and append the attributed strings to them.
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 upd:
swift5 更新:
let captionAttribute = [
NSAttributedString.Key.font: Font.captionsRegular,
NSAttributedString.Key.foregroundColor: UIColor.appGray
]
回答by Linuxios
Try this:
尝试这个:
NSMutableAttributedString* result = [astring1 mutableCopy];
[result appendAttributedString:astring2];
Where astring1
and astring2
are NSAttributedString
s.
哪里astring1
和astring2
是NSAttributedString
s。
回答by fatuhoku
If you're using Cocoapods, an alternative to both above answers that let you avoid mutability in your own code is to use the excellent NSAttributedString+CCLFormatcategory on NSAttributedString
s that lets you write something like:
如果您正在使用 Cocoapods,那么上述两个让您避免自己代码中的可变性的答案的替代方法是在s上使用出色的NSAttributedString+CCLFormat类别,NSAttributedString
它可以让您编写如下内容:
NSAttributedString *first = ...;
NSAttributedString *second = ...;
NSAttributedString *combined = [NSAttributedString attributedStringWithFormat:@"%@%@", first, second];
It of course it just uses NSMutableAttributedString
under the covers.
它当然只是NSMutableAttributedString
在幕后使用。
It also has the extra advantage of being a fully fledged formatting function —?so it can do a lot more than appending strings together.
它还具有作为完全成熟的格式化功能的额外优势——因此它可以做的不仅仅是将字符串附加在一起。
回答by Andrew
2020 | SWIFT 5.1:
2020 | 斯威夫特 5.1:
You're able to add 2 NSMutableAttributedString
by the following way:
您可以NSMutableAttributedString
通过以下方式添加 2 :
let concatenated = NSAttrStr1.append(NSAttrStr2)
Another way works with NSMutableAttributedString
and NSAttributedString
both:
另一种方式适用于NSMutableAttributedString
和NSAttributedString
两者:
[NSAttrStr1, NSAttrStr2].joinWith(separator: "")
Another way is....
另一种方式是......
var full = NSAttrStr1 + NSAttrStr2 + NSAttrStr3
and:
和:
var full = NSMutableAttributedString(string: "hello ")
// NSAttrStr1 == 1
full += NSAttrStr1 // full == "hello 1"
full += " world" // full == "hello 1 world"
You can do this with the following extension:
您可以使用以下扩展程序执行此操作:
// 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
}
}
回答by gaussblurinc
// 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;
}
回答by Igor Palaguta
You can try SwiftyFormatIt uses following syntax
你可以试试SwiftyFormat它使用以下语法
let format = "#{{user}} mentioned you in a comment. #{{comment}}"
let message = NSAttributedString(format: format,
attributes: commonAttributes,
mapping: ["user": attributedName, "comment": attributedComment])