64

我想用另一个替换一个子字符串(例如@"replace")。NSAttributedStringNSAttributedString

我正在寻找与NSString's stringByReplacingOccurrencesOfString:withString:for等效的方法NSAttributedString

4

10 回答 10

84
  1. 将您的属性字符串转换为NSMutableAttributedString.

  2. 可变属性字符串有一个mutableString属性。根据文档:

    “接收器跟踪此字符串的更改并使其属性映射保持最新。”

    因此,您可以使用生成的可变字符串来执行替换replaceOccurrencesOfString:withString:options:range:

于 2011-11-22T18:05:45.957 回答
20

以下是如何更改 NSMutableAttributedString 的字符串,同时保留其属性:

迅速:

// first we create a mutable copy of attributed text 
let originalAttributedText = nameLabel.attributedText?.mutableCopy() as! NSMutableAttributedString

// then we replace text so easily
let newAttributedText = originalAttributedText.mutableString.setString("new text to replace")

目标-C:

NSMutableAttributedString *newAttrStr = [attribtedTxt.mutableString setString:@"new string"];
于 2013-06-05T14:47:05.707 回答
20

就我而言,以下方式是唯一的(在 iOS9 上测试):

NSAttributedString *attributedString = ...;
NSAttributedString *anotherAttributedString = ...; //the string which will replace

while ([attributedString.mutableString containsString:@"replace"]) {
        NSRange range = [attributedString.mutableString rangeOfString:@"replace"];
        [attributedString replaceCharactersInRange:range  withAttributedString:anotherAttributedString];
    }

当然,找到另一种更好的方法会很好。

于 2016-03-25T08:30:04.367 回答
16

Swift 4: 更新了 Swift 4 的sunkas优秀解决方案,并包含在“扩展”中。只需将其剪辑到您的 ViewController 中(在类之外)并使用它。

extension NSAttributedString {
    func stringWithString(stringToReplace: String, replacedWithString newStringPart: String) -> NSMutableAttributedString
    {
        let mutableAttributedString = mutableCopy() as! NSMutableAttributedString
        let mutableString = mutableAttributedString.mutableString
        while mutableString.contains(stringToReplace) {
            let rangeOfStringToBeReplaced = mutableString.range(of: stringToReplace)
            mutableAttributedString.replaceCharacters(in: rangeOfStringToBeReplaced, with: newStringPart)
        }
        return mutableAttributedString
    }
}
于 2018-08-26T14:17:20.353 回答
13

使用 Swift 4 和 iOS 11,您可以使用以下两种方法之一来解决您的问题。


#1。使用NSMutableAttributedString replaceCharacters(in:with:)方法

NSMutableAttributedString有一个方法叫做replaceCharacters(in:with:). replaceCharacters(in:with:)有以下声明:

用给定属性字符串的字符和属性替换给定范围内的字符和属性。

func replaceCharacters(in range: NSRange, with attrString: NSAttributedString)

下面的 Playground 代码显示了如何使用新实例replaceCharacters(in:with:)替换实例的子字符串:NSMutableAttributedStringNSMutableAttributedString

import UIKit

// Set initial attributed string
let initialString = "This is the initial string"
let attributes = [NSAttributedStringKey.foregroundColor : UIColor.red]
let mutableAttributedString = NSMutableAttributedString(string: initialString, attributes: attributes)

// Set new attributed string
let newString = "new"
let newAttributes = [NSAttributedStringKey.underlineStyle : NSUnderlineStyle.styleSingle.rawValue]
let newAttributedString = NSMutableAttributedString(string: newString, attributes: newAttributes)

// Get range of text to replace
guard let range = mutableAttributedString.string.range(of: "initial") else { exit(0) }
let nsRange = NSRange(range, in: mutableAttributedString.string)

// Replace content in range with the new content
mutableAttributedString.replaceCharacters(in: nsRange, with: newAttributedString)

#2。使用NSMutableString replaceOccurrences(of:with:options:range:)方法

NSMutableString有一个方法叫做replaceOccurrences(of:with:options:range:). replaceOccurrences(of:with:options:range:)有以下声明:

用另一个给定字符串替换给定范围内所有出现的给定字符串,返回替换次数。

func replaceOccurrences(of target: String, with replacement: String, options: NSString.CompareOptions = [], range searchRange: NSRange) -> Int

下面的 Playground 代码显示了如何使用新实例replaceOccurrences(of:with:options:range:)替换实例的子字符串:NSMutableAttributedStringNSMutableAttributedString

import UIKit

// Set initial attributed string
let initialString = "This is the initial string"
let attributes = [NSAttributedStringKey.foregroundColor : UIColor.red]
let mutableAttributedString = NSMutableAttributedString(string: initialString, attributes: attributes)

// Set new string
let newString = "new"

// Replace replaceable content in mutableAttributedString with new content
let totalRange = NSRange(location: 0, length: mutableAttributedString.string.count)
_ = mutableAttributedString.mutableString.replaceOccurrences(of: "initial", with: newString, options: [], range: totalRange)

// Get range of text that requires new attributes
guard let range = mutableAttributedString.string.range(of: newString) else { exit(0) }
let nsRange = NSRange(range, in: mutableAttributedString.string)

// Apply new attributes to the text matching the range
let newAttributes = [NSAttributedStringKey.underlineStyle : NSUnderlineStyle.styleSingle.rawValue]
mutableAttributedString.setAttributes(newAttributes, range: nsRange)
于 2017-08-03T15:11:09.287 回答
6

我必须在<b>标签中加粗文本,这就是我所做的:

- (NSAttributedString *)boldString:(NSString *)string {
    UIFont *boldFont = [UIFont boldSystemFontOfSize:14];
    NSMutableAttributedString *attributedDescription = [[NSMutableAttributedString alloc] initWithString:string];

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@".*?<b>(.*?)<\\/b>.*?" options:NSRegularExpressionCaseInsensitive error:NULL];
    NSArray *myArray = [regex matchesInString:string options:0 range:NSMakeRange(0, string.length)] ;
    for (NSTextCheckingResult *match in myArray) {
        NSRange matchRange = [match rangeAtIndex:1];
        [attributedDescription addAttribute:NSFontAttributeName value:boldFont range:matchRange];
    }
    while ([attributedDescription.string containsString:@"<b>"] || [attributedDescription.string containsString:@"</b>"]) {
        NSRange rangeOfTag = [attributedDescription.string rangeOfString:@"<b>"];
        [attributedDescription replaceCharactersInRange:rangeOfTag withString:@""];
        rangeOfTag = [attributedDescription.string rangeOfString:@"</b>"];
        [attributedDescription replaceCharactersInRange:rangeOfTag withString:@""];
    }
    return attributedDescription;
}
于 2015-11-24T12:06:10.450 回答
4
NSMutableAttributedString *result = [[NSMutableAttributedString alloc] initWithString:@"I am a boy."];
[result addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, [result length])];

NSMutableAttributedString *replace = [[NSMutableAttributedString alloc] initWithString:@"a"];
[replace addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, [replace length])];

[result replaceCharactersInRange:NSMakeRange(5, [replace length]) withAttributedString:replace];
于 2014-04-12T11:15:11.637 回答
4

我发现所有其他答案都不起作用。以下是我如何在类别扩展中替换 NSAttributed 字符串的内容:

func stringWithString(stringToReplace:String, replacedWithString newStringPart:String) -> NSMutableAttributedString
{
    let mutableAttributedString = mutableCopy() as! NSMutableAttributedString
    let mutableString = mutableAttributedString.mutableString

    while mutableString.containsString(stringToReplace) {
        let rangeOfStringToBeReplaced = mutableString.rangeOfString(stringToReplace)
        mutableAttributedString.replaceCharactersInRange(rangeOfStringToBeReplaced, withString: newStringPart)
    }
    return mutableAttributedString
}
于 2016-07-31T10:09:56.663 回答
3

我有一个特定的要求并固定如下。这可能会帮助某人。

要求:在storyboard中,直接将富文本添加到UITextView的属性中,其中包含一个单词“App Version: 1.0”。现在我必须通过从 info plist 中读取来动态化版本号。

解决方案: 从情节提要中删除版本号 1.0,只保留“应用程序版本:”并添加以下代码。

NSAttributedString *attribute = self.firsttextView.attributedText;
NSMutableAttributedString *mutableAttri = [[NSMutableAttributedString alloc] initWithAttributedString:attribute];
NSString *appVersionText = @"App Version:";
if ([[mutableAttri mutableString] containsString:appVersionText]) {
    NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary];
    NSString* version = [infoDict objectForKey:@"CFBundleShortVersionString"];
    NSString *newappversion = [NSString stringWithFormat:@"%@ %@",appVersionText,version] ;
    [[mutableAttri mutableString] replaceOccurrencesOfString:appVersionText withString:newappversion options:NSCaseInsensitiveSearch range:NSMakeRange(0, mutableAttri.length)];
    self.firsttextView.attributedText = mutableAttri;
}

完毕!!更新/修改了属性文本。

于 2018-06-27T07:26:04.233 回答
0

我为此创建了一个 Swift 5 扩展

extension NSMutableAttributedString {
    
    func replace(_ findString: String, with replacement: String, attributes: [NSAttributedString.Key : Any]) {
        
        let ms = mutableString
        
        var range = ms.range(of: findString)
        while range.location != NSNotFound {
            addAttributes(attributes, range: range)
            ms.replaceCharacters(in: range, with: replacement)
            
            range = ms.range(of: findString)
        }
        
    }

}

用例

attributedString.replace("%EMAIL%", with: email, attributes: [.font:boldFont])
        
于 2022-02-07T12:02:13.573 回答