Display edited NSString in order

六月ゝ 毕业季﹏ 提交于 2020-01-15 12:13:51

问题


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:


回答1:


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:




回答2:


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.



来源:https://stackoverflow.com/questions/23469509/display-edited-nsstring-in-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!