对NSMutableString追加和预先附加\ n

时间:2014-05-07 13:37:22

标签: ios

我使用以下内容将换行符附加到我的NSStrings

// Add line break to strings
           [attrString appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];

我理想的是在字符串之前和之后添加 换行符 的解决方案,而不是在结尾处使用\n\n两次?这可能吗?

如果需要,可以在此处查看完整代码:

 NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] init];

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

               //Remove Marker
               NSString *stripped = [testWord stringByReplacingOccurrencesOfString:@"*" 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:14.0]
                              range:selectedRange];

               [string endEditing];

               [attrString appendAttributedString:string];
           }
           else
           {
               [attrString appendAttributedString:[[NSAttributedString alloc] initWithString:@"\u2022 "]];
               [attrString appendAttributedString:[[NSAttributedString alloc] initWithString:testWord]];
           }

           // Add line break to strings
           [attrString appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];

1 个答案:

答案 0 :(得分:1)

NSMutableAttributedString也会回复

- (void)insertAttributedString:(NSAttributedString *)attributedString atIndex:(NSUInteger)index

因此,您可以使用它在\n处插入index 0(并在结尾处继续附加\n)。