![](/img/trans.png)
[英]iOS Swift: How to change the Response from server to Specific Format?
[英]How to show new line with numbers format from server response of String in iOS
我得到的服务器响应是:“我们将组织放置在某些位置。1.首次测试2.第二测试3.第三测试4.第四测试。选择一个以获得更多信息”;
我正在研究Objective-C,并试图在UILabel中显示,但是问题是我想显示为段落。 就像通过跟随拆分字符串一样。
我们的组织正在追踪某些地点。
上面的例子是示例,但是数据是完全动态的,并且不知道响应字符串中会有多少个点。
任何人对此都有想法。
尝试这个 :
NSString *str = @"We've organizations in following something locations. 1. First test 2. Second test 3. Third test 4. Fourth test.";
str = [str stringByReplacingOccurrencesOfString:@"1." withString:@"\n 1."];
str = [str stringByReplacingOccurrencesOfString:@"2" withString:@"\n 2"];
str = [str stringByReplacingOccurrencesOfString:@"3" withString:@"\n 3"];
str = [str stringByReplacingOccurrencesOfString:@"4" withString:@"\n 4"];
yourlbl.text = str;
动态方式
NSString *str = @"We've organizations in following something locations. 1. First test 2. Second test 3. Third test 4. Fourth test.";
NSCharacterSet *numberCharset = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
NSScanner *theScanner = [NSScanner scannerWithString:str];
while (![theScanner isAtEnd]) {
// Eat non-digits and negative sign
[theScanner scanUpToCharactersFromSet:numberCharset
intoString:NULL];
int aInt;
if ([theScanner scanInt:&aInt]) {
str = [str stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%d",aInt] withString:[NSString stringWithFormat:@"\n %d",aInt]];
}
}
你可以这样使用
NSArray *array = [NSArray arrayWithObjects:@"First test",@"Second test",@"Third test" , @"Fourth test Choose one for more information.", nil];
__block NSString *strConcate = [NSString new];
[array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
strConcate = [strConcate stringByAppendingString:[NSString stringWithFormat:@"%lu. %@\n",(unsigned long)(idx + 1),obj]];
}];
self.labelString.text = strConcate;
最后,我找到了解决方案,就在这里。 这可能会在将来对某人有所帮助。
NSArray *pointsArray = [trimmedString componentsSeparatedByString:@"."];
NSString *strOccurence, *strReplacing;
for (int i=1; i<pointsArray.count; i++){
strOccurence = [NSString stringWithFormat:@"%d.",i];
strReplacing = [NSString stringWithFormat:@"\n%d.",i];
trimmedString = [trimmedString stringByReplacingOccurrencesOfString:strOccurence withString:strReplacing];
}
cell.textLabel.text = trimmedString;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.