文字列をマージする前にいくつかの文字列を検索していくつかの属性を設定する必要があるため、NSStrings->それらを連結します-> NSAttributedStringを作成するオプションはありません。attributedStringを別のattributedStringに連結する方法はありますか?
文字列をマージする前にいくつかの文字列を検索していくつかの属性を設定する必要があるため、NSStrings->それらを連結します-> NSAttributedStringを作成するオプションはありません。attributedStringを別のattributedStringに連結する方法はありますか?
回答:
@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:して、事後の属性を入力文字列を含む範囲に追加できます。前者の方法をお勧めします。
Swiftを使用している場合は、+演算子をオーバーロードして、通常の文字列を連結するのと同じ方法で演算子を連結できます。
// 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")
              return NSAttributedString(attributedString: result)
                    Helpersまたはというフォルダを作成し、Extensionsこの関数をというファイルに入れますNSAttributedString+Concatenate.swift。
                    Swift 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
swift5 upd:
    let captionAttribute = [
        NSAttributedString.Key.font: Font.captionsRegular,
        NSAttributedString.Key.foregroundColor: UIColor.appGray
    ]
              これを試して:
NSMutableAttributedString* result = [astring1 mutableCopy];
[result appendAttributedString:astring2];
どこastring1とastring2しているNSAttributedStringの。
[[aString1 mutableCopy] appendAttributedString: aString2]。
                    NSMutableAttributedString* aString3 = [aString1 mutableCopy]; [aString3 appendAttributedString: aString2];。
                    resultとして戻りますNSMutableAttributedString。それは著者が見たいものではありません。stringByAppendingString-この方法は適切です
                    2020 | SWIFT 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
    }
}
              NSAttrStr1.append(NSAttrStr2)
                    Cocoapodsを使用している場合、独自のコードで可変性を回避できるようにする上記の両方の回答の代わりに、NSAttributedString次のようなものを記述できるsで優れたNSAttributedString + CCLFormatカテゴリを使用することができます。
NSAttributedString *first = ...;
NSAttributedString *second = ...;
NSAttributedString *combined = [NSAttributedString attributedStringWithFormat:@"%@%@", first, second];
もちろんそれは隠れて使用するだけNSMutableAttributedStringです。
また、本格的なフォーマット機能であるという追加の利点もあります。そのため、文字列を一緒に追加するだけではありません。
// 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])