Search code examples
iosobjective-cnsstringnsarraynsattributedstring

Display edited NSString in order


I have been working on this for a few days with help from this great community.

I have a NSArray that I need to edit NSStrings within. I have managed to detect a marker in the string and make it bold. However now I am trying to display the strings in the order that they are within the NSArray whilst maintaining the Bold that was added to the specific strings.

I can display the individual Bold String 'string' but I need it to be in order that it is within the array. I know of stringByAppendingString but this would put it at the end.

Any directions would be brilliant.

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"];

EDIT

Based on the answers below I got it working however the bold method invokes this error:

enter image description here


Solution

  • Just use additional array. Change your code to

    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;
    

    UPDATE:

    Your additional issue is not a error - this is a way how XCode shows attributed strings in debug window: enter image description here