[英]UiTextView text to separate UITextField's
How can the text in my UITextView
be moved to individual UITextFields
. 如何将UITextView
的文本移动到单个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. 例如,如果我在textview中有5行文本,我希望将其移动到为每行分配的5个UITextField。 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. 目前,我已经能够用信息填充UITableView
,但是如果将其移到TextFields上,对于我需要做的事情来说会更容易。
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. 如果您的textView没有\\ n字符,那么您需要做更多的工作来获取基于行的textview文本。
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 . 此代码假定您对配置了布局管理器 ,文本存储和文本容器的textView
进行了引用。 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. textView
返回对布局管理器的引用,该管理器随后返回其关联的文本存储中所有字符的字形数量,并在必要时执行字形生成。 The for loop then begins laying out the text and counting the resulting line fragments. 然后,for循环开始对文本进行布局并计算生成的行片段。 The NSLayoutManager
method lineFragmentRectForGlyphAtIndex:effectiveRange:
forces layout of the line containing the glyph at the index passed to it. NSLayoutManager
方法lineFragmentRectForGlyphAtIndex:effectiveRange:
强制在包含传递给它的索引处包含字形的行的布局。
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. 方法计算NSMaxRangefunction
一行后, NSMaxRangefunction
返回的索引比范围内的最大值大一个,即下一行中第一个字形的索引。 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. numberOfLines
变量递增,并且for循环重复进行,直到index大于文本中的字形数量为止,此时numberOfLines
包含由换行定义的布局过程中产生的行数。
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. 对于第一次迭代,字符串lineText将具有textview的第一行,对于第二次迭代,它将具有textView的第二行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.