UiTextView文本来分隔UITextField的

我的UITextView的文本如何被移动到单独的UITextFields 。 例如,如果我在textview中有5行文本,我希望移动到为每行分配的5个UITextFields。 我现在已经能够用信息填充一个UITableView ,但是如果它被移动到TextFields上,我将会更容易。

尝试像这样:

  NSArray *subStrings = [myTextView.text componentsSeparatedByString: @"\n"]; textField1.text=subStrings[0]; textField2.text=subStrings[1]; 

如果你的textView没有任何\ n字符,那么你需要做更多的工作来获取基于行的textview文本。

尝试这个:

 - (void)viewDidLoad { [super viewDidLoad]; //set the textView in storyboard or you can do it here: textView.text=@"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda."; //Initialise your array yourArray=[[NSMutableArray alloc]init]; } -(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; NSLayoutManager *layoutManager = [textView layoutManager]; unsigned numberOfLines, index, numberOfGlyphs = [layoutManager numberOfGlyphs]; NSRange lineRange; for (numberOfLines = 0, index = 0; index < numberOfGlyphs; numberOfLines++){ (void) [layoutManager lineFragmentRectForGlyphAtIndex:index effectiveRange:&lineRange]; index = NSMaxRange(lineRange); NSString *lineText= [textView.text substringWithRange:lineRange]; [yourArray addObject:lineText]; } textField1.text=yourArray[0]; textField2.text=yourArray[1]; } 

此代码假定您有一个对使用布局pipe理器 ,文本存储和文本容器configuration的textView的引用。 textView返回对布局pipe理器的引用,然后返回其关联文本存储中所有字符的字形数量,如有必要,执行字形生成。 然后for循环开始铺设文本并计算结果的线段。 NSLayoutManager方法lineFragmentRectForGlyphAtIndex:effectiveRange:强制在传递给它的索引处包含字形的行的布局。

该方法返回线段 (此处忽略)所占据的矩形,并且通过引用,布局之后的行中的字形的范围。 在该方法计算出一行之后, NSMaxRangefunction返回的索引大于该范围内的最大值,即下一行中第一个字形的索引。 numberOfLinesvariables递增,for循环重复,直到index大于文本中字形的数量,在这一点上, numberOfLines包含由单词换行定义的布局过程产生的行数。

欲了解更多信息。

然后你可以做

  textField1.text=yourArray[0]; textField2.text=yourArray[1]; 

对于第一次迭代,stringlineText将有你的textview的第一行,对于第二次迭代,它将有你的textView的第二行。