问题
i have a webveiw where i can show small html value but i have a issue
if is do this
NSString *HTMLData =@"<h3><span style=font-family:Helvetica-Bold > <strong> Information</strong> </span></h3>";
HTMLData= [HTMLData stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
[web loadHTMLString:HTMLData baseURL:nil];
then its wokring fine
but if my NSString has a break line lets say
NSString *HTMLData =@"<h3><span style=font-family:Helvetica-Bold >
<strong> Information</strong>
</span></h3>";
then i am getting error how to avoid this error??? my error is missing terminator charcter
回答1:
To break long NSStrings across multiple lines, you need to put double quotes at the end and beginning of each line:
NSString *HTMLData =@"<h3><span style=font-family:Helvetica-Bold >"
"<strong> Information</strong>"
"</span></h3>";
Edit:
An example with NSMutableString:
NSMutableString *HTMLData = [[NSMutableString alloc] initWithCapacity:100];
[HTMLData appendString:@"<h3><span style=font-family:Helvetica-Bold >"];
[HTMLData appendString:@"<strong> Information</strong>"];
[HTMLData appendString:@"</span></h3>"];
//more appends...
//do something with HTMLData here
[HTMLData release];
The initWithCapacity just tells how many characters to allocate space for initially (it's not a limit).
NSMutableString also has the appendFormat:
method which works like stringWithFormat:
.
回答2:
One solution for this could be simply write your HTML as an String. and show the string as an Attributed String for Label.
let htmlText = "<p>etc</p>"
if let htmlData = htmlText.dataUsingEncoding(NSUnicodeStringEncoding) {
do {
someLabel.attributedText = try NSAttributedString(data: htmlData,
options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
} catch let e as NSError {
print("Couldn't translate \(htmlText): \(e.localizedDescription) ")
}
}
reference : How to show an HTML string on a UILabel
来源:https://stackoverflow.com/questions/4789447/write-html-content-to-nsstring-and-display-on-iphone