Question

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

Was it helpful?

Solution 2

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

OTHER TIPS

componentsJoinedByString return a NSString, when you want a NSAttributedString. Plus, you're setting your text to a receiver that awaits a NSString (cell.dynamicLabel.text), where what you want should be cell.dynamicLabel.attributedText.

Since there is no equivalent to componentsJoinedByString for a NSAttributedString return, you have to do it the oldway, with a for loop, starting with initializing a NSMutableAttributedString, and adding to it each components (that you may "transform") to it. Here is a example and related question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top