我需要搜索一些字符串,並在合併字符串之前設置一些屬性,所以有NSStrings - >連接它們 - >使NSAttributedString不是一個選項,有沒有什麼辦法來連接屬性字符串到另一個屬性字符串?如何連接NSAttributedStrings?
回答
我建議你使用一個可變屬性串一個@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:
將事後的屬性添加到包含輸入字符串的範圍中。我推薦以前的方式。
爲什麼你推薦附加字符串而不是添加屬性? – ma11hew28
試試這個:
NSMutableAttributedString* result = [astring1 mutableCopy];
[result appendAttributedString:astring2];
凡astring1
和astring2
是NSAttributedString
秒。
或'[aString1 mutableCopy] appendAttributedString:aString2]'。 – JWWalker
@JWWalker你的'oneliner'已損壞。你不能得到這個「連接」結果,因爲appendAttributedString不返回字符串。與詞典同樣的故事 – gaussblurinc
@gaussblurinc:好點,當然你的批評也適用於我們評論的答案。它應該是'NSMutableAttributedString * aString3 = [aString1 mutableCopy]; [aString3 appendAttributedString:aString2];'。 – JWWalker
如果您使用的CocoaPods,替代上述兩種答案,讓你避免在自己的代碼的可變性是使用優秀NSAttributedString+CCLFormat類別上NSAttributedString
s表示可以讓你寫類似:
NSAttributedString *first = ...;
NSAttributedString *second = ...;
NSAttributedString *combined = [NSAttributedString attributedStringWithFormat:@"%@%@", first, second];
它當然只是使用NSMutableAttributedString
。
它還有一個額外的優勢,就是它是一個完全成熟的格式化函數 - 所以它可以做的不僅僅是將字符串附加到一起。
如果您在使用雨燕,你可以重載+
操作,這樣就可以以同樣的方式將它們連接起來您連接普通字符串:
// 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")
但結果是可變的!它對於類型沒有意義! – gaussblurinc
可變類是不可變類的子類型。 – algal
所以我可以將結果強制轉換爲可變對象並且沒有錯誤? – 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;
}
您可以嘗試SwiftyFormat 它採用以下語法
let format = "#{{user}} mentioned you in a comment. #{{comment}}"
let message = NSAttributedString(format: format,
attributes: commonAttributes,
mapping: ["user": attributedName, "comment": attributedComment])
您能否詳細說明一下它的工作原理? –
斯威夫特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
- 1. 附加NSAttributedStrings返回一個錯誤
- 2. 如何使用連接池連接表?
- 3. JButton - 如何連接?
- 4. 如何連接表
- 5. 如何連接HandlerExceptionResolver
- 6. 如何連接cookie?
- 7. 如何連接servlet
- 8. 我如何連接
- 9. 如何連接$ .getJSON?
- 10. 如何連接由\
- 11. 如何連接點?
- 12. 如何連接blob?
- 13. 如何使用NSAttributedStrings創建NSarray,但保留數組中的屬性?
- 14. 如何添加具有NSAttributedStrings的NSComboBox中的自動完成功能
- 15. 如何在兩個接口之間隧道連接Telnet連接?
- 16. 如何將套接字連接到調制解調器連接?
- 17. mysql C++連接器,如何保持連接活動並重新連接,如果連接中斷?
- 18. 如何使php和python連續連接
- 19. 如何不退出服務器連接,如果沒有連接
- 20. 如何使net.Dial在Go重新連接,如果連接丟失?
- 21. 如何連接SQL和R.Net?
- 22. 如何命名SSH連接
- 23. UKHost4U和MySQL - 如何連接?
- 24. 如何連接QML和Java?
- 25. 如何連接到HSQLDB
- 26. 如何關閉NHibernate連接?
- 27. CI如何連接oracle?
- 28. 如何通過連接列
- 29. 如何加密dnode連接?
- 30. 如何索引自連接
這是荒謬多麼困難這還是* *是在八月的2016年 –