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
}
}