UiTextView文本分隔UITextField的 [英] UiTextView text to separate UITextField's

查看:144
本文介绍了UiTextView文本分隔UITextField的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 UITextView 中的文本移动到单独的 UITextFields 。例如,如果我在textview中有5行文本,我希望移动到为每行分配的5个UITextField。我目前已经能够使用这些信息填充 UITableView ,但如果将其移动到TextFields,我将需要做的更容易。

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.

推荐答案

尝试类似:

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

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

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.

试试这个:

- (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];

}

此代码假设您引用 textView 配置了布局管理器,文本存储和文本容器 textView 返回对布局管理器的引用,然后返回其中所有字符的字形的数量相关文本存储,必要时执行字形生成。然后for循环开始布置文本并计算得到的线段。 NSLayoutManager 方法 lineFragmentRectForGlyphAtIndex:effectiveRange:强制布局包含字形的行索引传递给它。

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.

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

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.

更多 info。

然后你可以做到

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

对于第一次迭代,字符串lineText将包含textview的第一行,而第二行迭代,它将有textView的第二行。

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.

这篇关于UiTextView文本分隔UITextField的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆