iPhone - Replacing line breaks in my TextView with <br />?
I am trying to convert lineBreaks
in my UITextView
to <br />
tags,
why does the following line crash? It complains about [NSCharacterSet newlineCharacterSet]
because when I pass it a normal string it works fine
[myMutableString stringByReplacingOccurrencesOfString:
[NSCharacterSet newlineCharacterSet] withString:@"<br />"];
Terminating app due to uncaught exception 'NSInvalidArgumentException', 开发者_开发知识库
reason: '-[__NSCFCharacterSet length]: unrecognized selector sent to
instance 0x7b1cc80'
A NSCharacterSet
is not a NSString
.So you're passing a wrong argument.
If you have only newline "\n" characters you can use:
[myMutableString stringByReplacingOccurrencesOfString:@"\n"
withString:@"<br />"];
If you have also "\r" and "\lf" you can try something like this (untested)
NSRange range;
while((range = [myMutableString rangeOfCharacterFromSet:
[NSCharacterSet newlineCharcaterSet]]).location != NSNotFound) {
[myMutableString replaceCharactersInRange:range withString:@"<br />"];
}
精彩评论