Replace
    &
  1. tags from html code with number in iOS 6

前端 未结 1 1141
梦如初夏
梦如初夏 2021-01-27 08:33

I am getting html code from server & I want to display it in UITextView. I need to parse the html & show plain text in UITextView. I am using below code to parse html fr

1条回答
  •  借酒劲吻你
    2021-01-27 09:13

    Replace you Function with following and pass your HTML string snippet that you have posted.

    NSString *str = @"
      " "
    1. Coffee
    2. " "
    3. Tea
    4. " "
    5. Milk
    6. " "
    "; [self stringByStrippingHTML:str];

    Function to replace string and Generate Desire output

    - (NSString *)stringByStrippingHTML:(NSString *)inputString
    {
        NSMutableString *outString;
    
        if (inputString)
        {
            outString = [[NSMutableString alloc] initWithString:inputString];
    
            if ([inputString length] > 0)
            {
                NSRange r;
                int index = 1;
                while ((r = [outString rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
                {
                    if([[outString substringWithRange:r] isEqualToString:@"
  • "]) { outString = [[outString stringByReplacingCharactersInRange:r withString:[NSString stringWithFormat:@"\n %d. ",index++]] mutableCopy]; } else [outString deleteCharactersInRange:r]; } } } return outString; }
  • 0 讨论(0)
提交回复
热议问题