ios - Converting HTML text into plain text using Objective-C -
i have huge nsstring
html text inside. length of string more 3.500.000 characters. how can convert html text nsstring
plain text inside. using scanner , works slowly. idea ?
it depends ios version targeting. since ios7 there built-in method not strip html tags, put formatting string:
objective-c
[[nsattributedstring alloc] initwithdata:[htmlstring datausingencoding:nsutf8stringencoding] options:@{nsdocumenttypedocumentattribute: nshtmltextdocumenttype, nscharacterencodingdocumentattribute: [nsnumber numberwithint:nsutf8stringencoding]} documentattributes:nil error:nil];
swift
let attributedstring = try nsattributedstring(data: htmlstring.datausingencoding(nsutf8stringencoding)!, options: [nsdocumenttypedocumentattribute: nshtmltextdocumenttype], documentattributes: nil)
if need remove between <
, >
(dirty way!!!), might problematic if have these characters in string, use this:
- (nsstring *)stringbystrippinghtml { nsrange r; nsstring *s = [[self copy] autorelease]; while ((r = [s rangeofstring:@"<[^>]+>" options:nsregularexpressionsearch]).location != nsnotfound) s = [s stringbyreplacingcharactersinrange:r withstring:@""]; return s; }
Comments
Post a Comment