How can the text in my UITextView be moved to individual UITextFields. For example if i have 5 lines of text in the textview i want that to move to 5 UITextFields allocated for each line. I have at the moment been able to populate a UITableView with the information however it would be easier for what i need to do if it were moved to TextFields instead.
问题:
回答1:
Try something like:
NSArray *subStrings = [myTextView.text componentsSeparatedByString: @"\n"]; textField1.text=subStrings[0]; textField2.text=subStrings[1]; If your textView doens't have any \n characters, then you need to do some more bit of work in getting the textview text based on lines.
Try this:
- (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]; } This code assumes you have a reference to a textView configured with a layout manager, text storage, and text container. The textView returns a reference to the layout manager, which then returns the number of glyphs for all the characters in its associated text storage, performing glyph generation if necessary. The for loop then begins laying out the text and counting the resulting line fragments. The NSLayoutManager method lineFragmentRectForGlyphAtIndex:effectiveRange: forces layout of the line containing the glyph at the index passed to it.
The method returns the rectangle occupied by the line fragment (here ignored) and, by reference, the range of the glyphs in the line after layout. After the method calculates a line, the NSMaxRangefunction returns the index one greater than the maximum value in the range, that is, the index of the first glyph in the next line. The numberOfLines variable increments, and the for loop repeats, until index is greater than the number of glyphs in the text, at which point numberOfLines contains the number of lines resulting from the layout process, as defined by word wrapping.
For more info.
and then you can do
textField1.text=yourArray[0]; textField2.text=yourArray[1]; For the first iteration, the string lineText will have the first line of your textview, and for the second iteration, it will have the second line of your textView.