Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何将字符串分成两部分_Iphone_Nsstring - Fatal编程技术网

Iphone 如何将字符串分成两部分

Iphone 如何将字符串分成两部分,iphone,nsstring,Iphone,Nsstring,我正在解析plist中的一个字符串,并尝试将其分成两部分,以便按照中断将其显示为两行或三行 NSArray * parts = [text componentsSeparatedByString:@"\n"]; // this text is coming from plist. int nthLine = 0; for(NSString *str in parts) { CGRect outerFrame = CGRectMake(frame1.origin.x, frame1.origin

我正在解析plist中的一个字符串,并尝试将其分成两部分,以便按照中断将其显示为两行或三行

NSArray * parts = [text componentsSeparatedByString:@"\n"]; // this text is coming from plist.
int nthLine = 0;
for(NSString *str in parts)
{

CGRect outerFrame = CGRectMake(frame1.origin.x, frame1.origin.y + 45*nthLine, frame1.size.width, 45);

SFNDoorStyledView * question = [[SFNDoorStyledView alloc]initWithFrame:outerFrame];
question.backgroundColor = [UIColor clearColor];
question.tag = 2;
[question drawString:text inRect:CGRectMake(0,0,500,150) usingFontNamed:@"Futura-Bold" size:40.0 lineSpacing:40.0 kernValue:-3.0 color:@"#7d7066"];
[self.view addSubview:question];
}
nthLine = nthLine +1;

考虑使用
UILabel
保存文本,您可以根据字符串本身动态调整文本大小。不需要在字符串中包含
\n
。只需将标签的大小/约束为所需的宽度,高度将根据字体和字体大小等其他属性为您计算。下面是一个具有代表性的片段

// Size the label based on the font and actual text
UILabel *l = [[[UILabel alloc] initWithFrame:CGRectMake(10, 5, 300, 50)] autorelease];
l.font = [UIFont boldSystemFontOfSize:14.0];
l.lineBreakMode = UILineBreakModeWordWrap;
l.textAlignment = UITextAlignmentCenter;
l.numberOfLines = 0; // Allow for as many lines as needed
l.backgroundColor = [UIColor clearColor];
CGSize constraintSize = CGSizeMake(300, MAXFLOAT);
CGSize labelSize = [l.text sizeWithFont:l.font 
       constrainedToSize:constraintSize 
       lineBreakMode:UILineBreakModeWordWrap];
CGRect frame = l.frame;
frame.size = labelSize;
// Center it
frame.origin.x = floor((320.0 - labelSize.width) / 2.0);
l.frame = frame;

[v addSubview:l];

检查行分隔符是否实际为
\n
。它也可以是
\r
。因此,使用字符串分隔的组件:@“\r\n”可能会有所帮助。

请注意\n在不同上下文中的含义

在这方面

NSArray * parts = [text componentsSeparatedByString:@"\n"];
\n被解释为换行符

在plist中\n将是实际字符\n和

您可以使用option enter将换行符添加到plist,然后使用上面的代码或更好的代码:

NSArray * parts = [text componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
或者,如果要使用此自定义“\n”分隔符,可以使用

NSArray * parts = [text componentsSeparatedByString:@"\\n"];

实际上,plist将字符串保存为文本。因此,如果输入\n,它将作为文本而不是换行符。尽管这样做了,我还是断线了。例如:如果你有一个字符串,你现在的月收入是多少?你想把它从收入中分离出来。然后你可以写下你要写的内容,然后按alt+enter并在新行中输入文本。

那么,你的问题是什么?您是否收到错误?运行此代码时发生了什么?您希望发生什么?我尝试中断的字符串是“when do you want retire”,我希望它在“you”之后中断,因此我将其转换为“when do you\n want retire?”以便“when do you”显示在第一行中,其余部分显示在下一行中。但是有了这段代码,包括\n在内的所有内容都集中在一行中。为什么要分离字符串来实现这一点?不能按原样使用字符串中的语法吗?如果您的字符串包含\n您对NSLog的调用将在打印时自动\n将所有内容放在新行上。我只这样做。我正在将\n放入字符串中,然后尝试通过\n将其分隔成两行。我不能。我必须使用这个视图来显示内容,因为我们正在使用CoreText。