Remove HTML Tags from an NSString on the iPhone
A quick and "dirty" (removes everything between < and >) solution, works with iOS >= 3.2:
-(NSString *) stringByStrippingHTML {
NSRange r;
NSString *s = [[self copy] autorelease];
while ((r = [s rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
s = [s stringByReplacingCharactersInRange:r withString:@""];
return s;
}
I have this declared as a category os NSString.
Remove HTML tags from NSString
You can tweak as
First replce <br/>
with *br/*
then replace all html tags and then again replace *br/*
to <br/>
.
Or, you can use conditional replacing .. if read tag is br
continue else replace.
Remove html tags and display in UILabel Objective c
Use below UILabel
formatting control which provides Rich text formatting based on HTML-like markups for iOS.
RTLabel
Remove broken HTML tags from NSString
try this code
- (NSString *)removeIncompleteHTMLTagInString:(NSString *)HTMLString {
NSArray *subStringByOpenTabs = [HTMLString componentsSeparatedByString:@"<"];
NSArray *subStringByCloseTabs = [HTMLString componentsSeparatedByString:@">"];
if (subStringByOpenTabs.count > subStringByCloseTabs.count) {
return [HTMLString substringToIndex:(HTMLString.length - ((NSString *)[subStringByOpenTabs lastObject]).length) -1];
}
else {
return HTMLString;
}
}
test:
NSLog(@"%@",[self removeIncompleteHTMLTagInString:@"This is some <xx> broken html tag<b"]);
output is: "This is some <xx> broken html tag"
How can I remove html tags from a string?
You can remove html tag from string by using NSAttributedString
.
Please find the below code :
let htmlString = "<p style=\"text-align: right;\"> text and text"
do {
let encodedData = htmlString.dataUsingEncoding(NSUTF8StringEncoding)!
let attributedOptions : [String: AnyObject] = [
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding
]
let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)
print("final strings :",attributedString.string)
} catch {
fatalError("Unhandled error: \(error)")
}
Hope it works for you!!!
You can also create String extension for reusability:
extension String {
init(htmlString: String) {
do {
let encodedData = htmlString.dataUsingEncoding(NSUTF8StringEncoding)!
let attributedOptions : [String: AnyObject] = [
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding
]
let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)
self.init(attributedString.string)
} catch {
fatalError("Unhandled error: \(error)")
}
}
}
Swift 3.0 - (Xcode 8.2) Update
extension String {
var normalizedHtmlString : String {
do {
if let encodedData = self.data(using: .utf8){
let attributedOptions : [String: AnyObject] = [
NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType as AnyObject,
NSCharacterEncodingDocumentAttribute: NSNumber(value: String.Encoding.utf8.rawValue)
]
let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)
if let stringNormalized = String.init(attributedString.string){
return stringNormalized
}
}
}
catch {
assert(false, "Please check string")
//fatalError("Unhandled error: \(error)")
}
return self
}
}
And call the htmlString method :
let yourHtmlString = "<p style=\"text-align: right;\"> text and text"
let decodedString = String(htmlString:yourHtmlString)
Related Topics
Download Array of Images as Byte Array and Convert to Images
Swift Tableview in a Uiview Not Displaying Data
How to Determine the Current Iphone/Device Model
Set the Maximum Character Length of a Uitextfield
Applications Are Expected to Have a Root View Controller At the End of Application Launch
How to Load Custom Uitableviewcells from Xib Files
How to Create a Uicolor from a Hex String
How to Open Phone Settings When a Button Is Clicked
Iphone Navigation Bar Title Text Color
Can't Get Correct Value of Keyboard Height in Ios8
How to Center Horizontally Uicollectionview Cells
Cell Size Not Updating After Changing Flow Layout'S Itemsize
Test iOS App on Device Without Apple Developer Program or Jailbreak
How to Exit Iphone Application
How to Find Topmost View Controller on Ios
Xcode 6 Bug: Unknown Class in Interface Builder File