Swiftで別の属性付きテキストに属性付きテキストを追加したい。 Swiftで2つの属性付き文字列の操作を追加するためのサンプルコードを提供してください。
NSMutableAttributedStringを使用する
小さな例
let yourAttributes = [NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: UIFont.systemFontOfSize(15)]
let yourOtherAttributes = [NSForegroundColorAttributeName: UIColor.redColor(), NSFontAttributeName: UIFont.systemFontOfSize(25)]
let partOne = NSMutableAttributedString(string: "This is an example ", attributes: yourAttributes)
let partTwo = NSMutableAttributedString(string: "for the combination of Attributed String!", attributes: yourOtherAttributes)
let combination = NSMutableAttributedString()
combination.appendAttributedString(partOne)
combination.appendAttributedString(partTwo)
スイフト3
let yourAttributes = [NSForegroundColorAttributeName: UIColor.black, NSFontAttributeName: UIFont.systemFont(ofSize: 15)]
let yourOtherAttributes = [NSForegroundColorAttributeName: UIColor.red, NSFontAttributeName: UIFont.systemFont(ofSize: 25)]
let partOne = NSMutableAttributedString(string: "This is an example ", attributes: yourAttributes)
let partTwo = NSMutableAttributedString(string: "for the combination of Attributed String!", attributes: yourOtherAttributes)
let combination = NSMutableAttributedString()
combination.append(partOne)
combination.append(partTwo)
combination
は、yourAttributes
とyourOtherAttributes
によって提供される両方のフォーマットを含む最終的な文字列を表します
@ glace's answer 、空のNSMutableAttributedString
宣言を避けるために修正。 Swift 3.1で有効:
let yourAttributes = [NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: UIFont.systemFontOfSize(15)]
let yourOtherAttributes = [NSForegroundColorAttributeName: UIColor.redColor(), NSFontAttributeName: UIFont.systemFontOfSize(25)]
let partOne = NSMutableAttributedString(string: "This is an example ", attributes: yourAttributes)
let partTwo = NSMutableAttributedString(string: "for the combination of Attributed String!", attributes: yourOtherAttributes)
partOne.append(partTwo)
partOne
は、すべての属性を含む最終的な文字列です。中間の「コンバイナ」は必要ありません。
Swift 4
let yourAttributes: [NSAttributedString.Key: Any] = [.foregroundColor: UIColor.black, .font: UIFont.systemFont(ofSize: 15)]
let yourOtherAttributes: [NSAttributedString.Key: Any] = [.foregroundColor: UIColor.red, .font: UIFont.systemFont(ofSize: 25)]
let partOne = NSMutableAttributedString(string: "This is an example ", attributes: yourAttributes)
let partTwo = NSMutableAttributedString(string: "for the combination of Attributed String!", attributes: yourOtherAttributes)
partOne.append(partTwo)
「グレイス」の回答に従って、フォント属性とSwiftバージョンを更新するだけです。
let boldFontAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black, NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 17)]
let normalFontAttributes = [NSAttributedString.Key.foregroundColor: UIColor.darkGray, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 15)]
let partOne = NSMutableAttributedString(string: "This is an example ", attributes: boldFontAttributes)
let partTwo = NSMutableAttributedString(string: "for the combination of Attributed String!", attributes: normalFontAttributes)
let combination = NSMutableAttributedString()
combination.append(partOne)
combination.append(partTwo)
lblUserName.attributedText = combination
拡張機能を使用して、
extension NSMutableAttributedString{
func getAttributedStringByAppending(attributedString:NSMutableAttributedString) -> NSMutableAttributedString{
let newAttributedString = NSMutableAttributedString()
newAttributedString.append(self)
newAttributedString.append(attributedString)
return newAttributedString
}
}
使用法:attributedString1、attributedString2は2つのNSMutableAttributedStringであり、
let combinedAttributedString = attributedString1.getAttributedStringByAppending(attributedString: attributedString2)