Rendern einen ASCII-Art-String mit Zeilenumbrüchen und Schrägstrichen
-
08-10-2019 - |
Frage
Ich mache eine App, wo es eine Anforderung zum Speichern von ASCII-Art in eine Datenbank. Ich speichere die Strings in dem folgende Format.
"___________ \ n | --------- | -O \ n / ___________ \\ n | ______________ | \ n \ ____________ /"
Wenn ich die Daten abrufen und in einem Etikett angezeigt werden, möchte ich die Zeilenumbrüche und Schrägstriche analysiert werden, um die reale Form der ASCII-Art angezeigt werden soll.
Wie soll ich diese Art von Strings analysieren?
Lösung
NSString has a method to do what you want, which is to replace a litteral \n, with a newline character (which is symbolized as \n). In a c-format string you can use a double slash to let the library know the second slach is a real one and not an escape symbol. So this should work assuming you have been able to load your data from sqlite into an NSString:
newString = [yourStringFromSQLite stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
Andere Tipps
If you are using \n just for creating new lines then instead of that just keep space while inserting value in database and set following properties for label ->
1)keep width just to fit first word.
2)linebreakmode to wordwrap (so as width will not be available it will wrap next word to new line)
3)set no. of lines to 0
Hope this will help.
try to use the scanner for remove the html entities
- (NSString *)flattenHTML:(NSString *)html trimWhiteSpace:(BOOL)trim {
NSScanner *theScanner;
NSString *text = nil;
theScanner = [NSScanner scannerWithString:html];
while ([theScanner isAtEnd] == NO) {
[theScanner scanUpToString:@"<" intoString:NULL] ;
[theScanner scanUpToString:@">" intoString:&text] ;
html = [html stringByReplacingOccurrencesOfString:[ NSString stringWithFormat:@"%@>", text] withString:@" "];
}
return trim ? [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] : html;
}
and call this method where your trimmed string need to display