如何显示 iOS 中 String 服务器响应中数字格式的新行
How to show new line with numbers format from server response of String in iOS
我收到服务器响应 "We've organizations in following something locations. 1. First test 2. Second test 3. Third test 4. Fourth test. Choose one for more information";
我正在研究 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;
我收到服务器响应 "We've organizations in following something locations. 1. First test 2. Second test 3. Third test 4. Fourth test. Choose one for more information";
我正在研究 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;