【问题标题】:Display edited NSString in order按顺序显示编辑的 NSString
【发布时间】:2014-05-05 09:49:51
【问题描述】:

在这个伟大社区的帮助下,我已经为此工作了几天。

我有一个NSArray,我需要在其中编辑NSStrings。我设法检测到字符串中的标记并将其设为粗体。但是现在我试图按照它们在 NSArray 中的顺序显示字符串,同时保持添加到特定字符串的粗体。

我可以显示单独的粗体字符串“string”,但我需要它以使其位于数组中。我知道stringByAppendingString,但这会把它放在最后。

任何方向都会很棒。

for (NSString *testWord in legislationArray) {
            if ([testWord rangeOfString:@"BOLDME"].location != NSNotFound) {

            //Remove Marker
            NSString *stripped = [testWord stringByReplacingOccurrencesOfString:@"BOLDME" withString:@""];

            //Get string and add bold
            NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:stripped];

            NSRange selectedRange = [stripped rangeOfString:(stripped)];

            [string beginEditing];

            [string addAttribute:NSFontAttributeName
                           value:[UIFont fontWithName:@"Helvetica-Bold" size:18.0]
                           range:selectedRange];

            [string endEditing];

            //Where to go now with string?

        }
    }
    cell.dynamicLabel.text = [legislationArray componentsJoinedByString:@"\n"];

编辑

根据下面的答案,我得到了它的工作,但是粗体方法调用了这个错误:

【问题讨论】:

  • 您的附加问题不是错误 - 这是 XCode 在调试窗口中显示属性字符串的一种方式。我已在答案中添加了屏幕截图。

标签: ios objective-c nsstring nsarray nsattributedstring


【解决方案1】:

componentsJoinedByString 在您需要 NSAttributedString 时返回 NSString。 另外,您将文本设置为等待NSString (cell.dynamicLabel.text) 的接收器,您想要的应该是cell.dynamicLabel.attributedText

由于 NSAttributedString 返回没有与 componentsJoinedByString 等效的方法,因此您必须以旧方式执行此操作,使用 for 循环,从初始化 NSMutableAttributedString 开始,并向其中添加每个组件(即你可以“转换”)它。 Here 是一个示例和相关问题。

【讨论】:

    【解决方案2】:

    只需使用额外的数组。将您的代码更改为

    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] init];
    for (NSString *testWord in legislationArray) {
        if ([testWord rangeOfString:@"BOLDME"].location != NSNotFound) {
    
            //Remove Marker
            NSString *stripped = [testWord stringByReplacingOccurrencesOfString:@"BOLDME" withString:@""];
    
            //Get string and add bold
            NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:stripped];
    
            NSRange selectedRange = [stripped rangeOfString:(stripped)];
    
            [string beginEditing];
    
            [string addAttribute:NSFontAttributeName
                           value:[UIFont fontWithName:@"Helvetica-Bold" size:18.0]
                           range:selectedRange];
    
            [string endEditing];
    
            //Where to go now with string?
            [attrString appendAttributedString:string];
        }
        else
        {
            [attrString appendAttributedString:[[NSAttributedString alloc] initWithString:testWord]];
        }
        // NEW LINE
        [attrString appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
    }
    cell.dynamicLabel.attributedText = attrString;
    

    更新:

    您的附加问题不是错误 - 这是 XCode 在调试窗口中显示属性字符串的一种方式:

    【讨论】:

    • 这行得通。但是,如更新的答案所示,它会在粗体上产生一个奇怪的错误。有什么建议吗?
    • @memyselfandmyiphone 我已经改进了答案。请检查一下。
    • 看起来不错。现在唯一的问题是我已经失去了将 componentsJoinedByString:@"\n" 放在哪里,因为从阅读它开始,这不受属性字符串的支持
    • 在追加前添加“\n”。
    • @memyselfandmyiphone 我添加了 '[attrString appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];'答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多