我怎样才能连接NSAttributedStrings?

我需要search一些string,并在合并string之前设置一些属性,所以有NSStrings – >连接它们 – >使NSAttributedString不是一个选项,有什么办法连接属性string到另一个属性string?

我build议你使用@Linuxiosbuild议的一个可变的属性string,这里是另一个例子:

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]; 

但是,为了获得所有选项,还可以创build一个可变的归属string,该string由格式化的NSString构成,其中包含已经放在一起的inputstring。 然后可以使用addAttributes: range:将事实之后的属性添加到包含inputstring的范围中。 我推荐以前的方式。

如果你使用的是Swift,你可以重载+运算符,这样就可以像连接普通string一样连接它们:

 // 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") 

尝试这个:

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

astring1astring2NSAttributedString s。

Swift 3:只需创build一个NSMutableAttributedString并将属性string追加到它们。

 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 

如果你正在使用Cocoapods,上面的两个答案的替代scheme可以让你避免在自己的代码中的可变性,那就是使用NSAttributedString上的优秀的NSAttributedString + CCLFormat类别,它可以让你编写如下的代码:

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

当然,它只是在封面下使用NSMutableAttributedString

它还具有完全成熟的格式化function的额外优势 – 因此它可以做比将string附加在一起更多的function。

你可以试试SwiftyFormat它使用下面的语法

 let format = "#{{user}} mentioned you in a comment. #{{comment}}" let message = NSAttributedString(format: format, attributes: commonAttributes, mapping: ["user": attributedName, "comment": attributedComment]) 
 // 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; }