我如何连接NSAttributedString?

时间:2013-08-29 18:15:18

标签: ios nsattributedstring

我需要在合并字符串之前搜索一些字符串并设置一些属性,因此使用NSStrings - >连接它们 - > make NSAttributedString不是一个选项,有没有办法将referencedString连接到另一个referencedString?

7 个答案:

答案 0 :(得分:191)

我建议您使用@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:将事实后的属性添加到包含输入字符串的范围。我推荐以前的方式。

答案 1 :(得分:25)

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

答案 2 :(得分:24)

试试这个:

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

其中astring1astring2NSAttributedString s。

答案 3 :(得分:5)

2020年| SWIFT 5.1:

您可以通过以下方式添加2个NSMutableAttributedString

let concatenated = NSAttrStr1.append(NSAttrStr2)

另一种方法同时适用于NSMutableAttributedStringNSAttributedString

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

答案 4 :(得分:4)

如果你正在使用Cocoapods,那么上述两个答案都可以让你避免在你自己的代码中出现可变性,那就是在NSAttributedString上使用优秀的NSAttributedString+CCLFormat类别,它可以让你写出如下内容:

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

当然它只是使用NSMutableAttributedString

它还具有成为完全成熟的格式化功能的额外优势 - 因此它可以比将字符串附加在一起做更多的事情。

答案 5 :(得分:1)

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

答案 6 :(得分:1)

您可以尝试SwiftyFormat 它使用以下语法

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