Created
March 25, 2011 13:37
-
-
Save danielphillips/886835 to your computer and use it in GitHub Desktop.
Convert raw HTML to basic text preserving line breaks for use in UILabel or UITextView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface HTMLLineBreakConvertor : NSObject <NSXMLParserDelegate> { | |
NSMutableString* resultString; | |
NSDictionary* tags; | |
} | |
- (NSString*)convertEntiesInString:(NSString*)s; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "HTMLLineBreakConvertor.h" | |
#define kXML_ROOT_ELEMENT @"root_of_xml_string" | |
#define kHTML_LINE_BREAK_ELEMENT @"br" | |
#define kHTML_PARAGRAPH_ELEMENT @"p" | |
#define kHTML_SPAN_ELEMENT @"span" | |
#define kHTML_DIV_ELEMENT @"div" | |
#define kHTML_LI_ELEMENT @"li" | |
@implementation HTMLLineBreakConvertor | |
- (id)init{ | |
if([super init]) { | |
resultString = [[NSMutableString alloc] initWithString:@""]; | |
tags = [[NSDictionary alloc] initWithObjectsAndKeys: | |
@"\r\r", | |
kHTML_PARAGRAPH_ELEMENT, | |
@"\r\r", | |
kHTML_DIV_ELEMENT, | |
@"\r", | |
kHTML_LINE_BREAK_ELEMENT, | |
@"\r• ", | |
kHTML_LI_ELEMENT, | |
@"", | |
kHTML_SPAN_ELEMENT, | |
nil]; | |
} | |
return self; | |
} | |
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ | |
NSString* appendValue = [[tags objectForKey:elementName] retain]; | |
if(appendValue){ | |
if([resultString length] > 0){ | |
[resultString appendString:appendValue]; | |
} | |
} | |
[appendValue release]; | |
} | |
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)s { | |
[resultString appendString:s]; | |
} | |
- (NSString*)convertEntiesInString:(NSString*)s { | |
if(s != nil){ | |
// remove existing line breaks in HTML code | |
s = [s stringByReplacingOccurrencesOfString:@"\r" withString:@""]; | |
s = [s stringByReplacingOccurrencesOfString:@"\n" withString:@""]; | |
NSString* xmlStr = [[NSString alloc] initWithFormat:@"<%@>%@</%@>", kXML_ROOT_ELEMENT, s, kXML_ROOT_ELEMENT]; | |
NSData *data = [[xmlStr dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES] retain]; | |
NSXMLParser* xmlParse = [[NSXMLParser alloc] initWithData:data]; | |
[xmlParse setDelegate:self]; | |
[xmlParse parse]; | |
[xmlParse release]; | |
[xmlStr release]; | |
[data release]; | |
return resultString; | |
}else{ | |
return @""; | |
} | |
} | |
- (void)dealloc { | |
[resultString release]; | |
[tags release]; | |
[super dealloc]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment