开发者

NSString problem on iphone while displaying on UILabel

开发者 https://www.devze.com 2023-02-04 10:58 出处:网络
i have a issue , i am asking as new question as previoes one was messed NSString *s=@\"hi\\nhello\\n\\nwelcome to this world\\ni m jhon\"

i have a issue , i am asking as new question as previoes one was messed

NSString *s=@"hi\nhello\n\nwelcome to this world\ni m jhon"
label.frame = ...//big enough height
label.numberOfLines = 0;
label.text = s;

this code helps me to separate string based on \n

but if i do this

NSString *s=Ad.content //where Ad.content value is **hi\nhello\n\nwelcome to this world\ni m jhon**


label.numberOfLines = 0;
label.text = s;

i am not able to sperate them b开发者_开发技巧y \n , what i am doing wrong here kindly suggest

Thanks


"\n" is not a special token handled by UILabel. It is, in fact, a special token handled by the compiler. When the compiler sees "Hello\nWorld", it converts that into the sequence of characters 'H' 'e' 'l' 'l' 'o' LF 'W' 'o' 'r' 'l' 'd' (where LF is ASCII code 10, or newline). For whatever reason, your Ad.content contains literal "\n" sequences instead of newlines. The best solution is to look at where the content of Ad.content comes from and fix it to actually have real newlines instead of "\n" sequences. If you absolutely must have "\n" sequences, then you can use -[NSMutableString stringByReplacingOccurrencesOfString:withString:]. Of course, if anyone wants to have the literal sequence "\n" show up, then they can't do that. If you want to support generic backslash-escaping (so the literal sequence "\n" would be represented as "\n"), then you can use a more complicated approach with NSScanner where you find each "\", pull out the next character, and handle it appropriately.

My recommendation is to fix Ad.content to contain actual newlines.


NSString *_stringToSplit = @"hi\nhello\n\nwelcome to this world\ni m jhon";
NSArray *_splitItems = [_stringToSplit componentsSeparatedByString:@"\n"];
NSMutableArray *_mutableSplitItems = [NSMutableArray arrayWithCapacity:[_splitItems count]]; 
[_mutableSplitItems addObjectsFromArray:_splitItems];

[_splitItems count] is your solution

cheers Endo


i did this . it was so easy damn i am mad thanks guys

NSString *ss = [aDl.content stringByReplacingOccurrencesOfString:@"\n" withString:@"\n"];

0

精彩评论

暂无评论...
验证码 换一张
取 消