2013-08-29 35 views
105

我需要搜索一些字符串,並在合併字符串之前設置一些屬性,所以有NSStrings - >連接它們 - >使NSAttributedString不是一個選項,有沒有什麼辦法來連接屬性字符串到另一個屬性字符串?如何連接NSAttributedStrings?

+3

這是荒謬多麼困難這還是* *是在八月的2016年 –

回答

162

我建議你使用一個可變屬性串一個@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:將事後的屬性添加到包含輸入字符串的範圍中。我推薦以前的方式。

+0

爲什麼你推薦附加字符串而不是添加屬性? – ma11hew28

23

試試這個:

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

astring1astring2NSAttributedString秒。

+13

或'[aString1 mutableCopy] appendAttributedString:aString2]'。 – JWWalker

+0

@JWWalker你的'oneliner'已損壞。你不能得到這個「連接」結果,因爲appendAttributedString不返回字符串。與詞典同樣的故事 – gaussblurinc

+0

@gaussblurinc:好點,當然你的批評也適用於我們評論的答案。它應該是'NSMutableAttributedString * aString3 = [aString1 mutableCopy]; [aString3 appendAttributedString:aString2];'。 – JWWalker

4

如果您使用的CocoaPods,替代上述兩種答案,讓你避免在自己的代碼的可變性是使用優秀NSAttributedString+CCLFormat類別上NSAttributedString s表示可以讓你寫類似:

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

它當然只是使用NSMutableAttributedString

它還有一個額外的優勢,就是它是一個完全成熟的格式化函數 - 所以它可以做的不僅僅是將字符串附加到一起。

40

如果您在使用雨燕,你可以重載+操作,這樣就可以以同樣的方式將它們連接起來您連接普通字符串:

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

但結果是可變的!它對於類型沒有意義! – gaussblurinc

+3

可變類是不可變類的子類型。 – algal

+0

所以我可以將結果強制轉換爲可變對象並且沒有錯誤? – gaussblurinc

0
// 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; 
} 
1

您可以嘗試SwiftyFormat 它採用以下語法

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

您能否詳細說明一下它的工作原理? –

12

斯威夫特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