0

就像标题所说的那样......我该怎么做?我希望用户输入大量文本,然后以某种方式最小化键盘并返回与该视图中的其他对象交互。但是,他们可能需要“输入”键来在键入时格式化他们的文本。有什么建议么?

Anil 建议了一个包含以下代码的链接:

(void)textViewDidBeginEditing:(UITextView *)textView {

[self createInputAccessoryView];
[textView setInputAccessoryView:_inputAccessoryView];
self.myTextView = textView;  }

-(void)createInputAccessoryView {

_inputAccessoryView = [[UIToolbar alloc] init];
_inputAccessoryView.barStyle = UIBarStyleBlackOpaque;
[_inputAccessoryView sizeToFit];

_inputAccessoryView.frame = CGRectMake(0,_collageView.frame.size.height - 44, _collageView.frame.size.width, 44);

UIBarButtonItem *fontItem = [[UIBarButtonItem alloc] initWithTitle:@"Font"
                                                             style:UIBarButtonItemStyleBordered
                                                            target:self action:@selector(changeFont:)];
UIBarButtonItem *removeItem = [[UIBarButtonItem alloc] initWithTitle:@"Remove"
                                                             style:UIBarButtonItemStyleBordered
                                                            target:self action:@selector(removeTextView:)];
//Use this to put space in between your toolbox buttons
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                          target:nil
                                                                          action:nil];
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                             style:UIBarButtonItemStyleDone
                                                            target:self action:@selector(dismissKeyBoard:)];

NSArray *items = [NSArray arrayWithObjects:fontItem,removeItem,flexItem,doneItem, nil];
[_inputAccessoryView setItems:items animated:YES];
[_myTextView addSubview:_inputAccessoryView];
}

但是,我在概念上有点落后……我应该把对我的 TextView、.h 或 .m 文件的引用放在哪里?没看懂怎么...

-(void)textViewDidBeginEditing:(UITextView *)textView {

...链接到我的实际文本字段。此外,xcode 为“_inputAccessoryView”抛出一个错误,说它没有在任何地方定义。非常感激你的帮助!

4

2 回答 2

2

实现UITextViewDelegate让文本视图的第一响应者辞职的方法

- (void)textViewDidEndEditing:(UITextView *)textView
 {
  [textView resignFirstResponder];
 } 

如果您想在键入时关闭键盘,请使用UITextField 的输入附件视图。在键盘上添加一个取消按钮。
检查这个问题
如何添加输入附件视图

编辑

你要做的是(你可以在viewDidLoad中做所有这些事情)

UIToolbar *inputAccessoryView = [[UIToolbar alloc]init]; // Create one input accessory view, tool bar will be easy for you  
inputAccessoryView.frame = CGRectMake(0,self.view.frame.size.height - 44, self.view.frame.size.width, 44);

// Add required buttons
UIBarButtonItem *fontItem = [[UIBarButtonItem alloc] initWithTitle:@"Font"
                                                             style:UIBarButtonItemStyleBordered
                                                            target:self action:@selector(changeFont:)];
UIBarButtonItem *removeItem = [[UIBarButtonItem alloc] initWithTitle:@"Remove"
                                                               style:UIBarButtonItemStyleBordered
                                                              target:self action:@selector(removeTextView:)];
//Use this to put space in between your toolbox buttons
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                          target:nil
                                                                          action:nil];
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                             style:UIBarButtonItemStyleDone
                                                            target:self action:@selector(dismissKeyBoard:)];

NSArray *items = [NSArray arrayWithObjects:fontItem,removeItem,flexItem,doneItem, nil];
[inputAccessoryView setItems:items animated:YES];

//You should create an outlet for the text view before doing this 
[self.textView setInputAccessoryView:inputAccessoryView];
于 2013-05-07T03:43:50.087 回答
2

您可以添加UIToolBar上面的键盘。然后在工具栏上添加一个完成的 UIBarBtttonItem 。因此,每当用户按下完成按钮时,然后调用一个方法并退出您的 textview。

#define IS_IPHONE ( [[[UIDevice currentDevice] model] isEqualToString:@"iPhone"])
#define IS_HEIGHT_GTE_568 [[UIScreen mainScreen ] bounds].size.height >= 568.0f
#define IS_IPHONE_5 ( IS_IPHONE && IS_HEIGHT_GTE_568 )

前任。: 在您的 ViewDidLoad 方法中添加此代码

float y = IS_IPHONE_5:308:220
         toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,y,320,44)]; 
                [toolBar setBarStyle:UIBarStyleBlackOpaque];
                UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Item" style:UIBarButtonItemStyleBordered target:self action:@selector(doneButtonTouched:)];

textViewShouldBeginEditing代理将被调用时,显示您的工具栏

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    [toolBar setHidden:NO];
    return YES;
}

    #pragma mark---
    #pragma mark---IBAction Methods

    -(IBAction)doneButtonTouched:(id)sender
    {
        [YOUR_TEXTVIEW resignFirstResponder];
        [toolBar setHidden:YES];
    }

希望它可以帮助你。

于 2013-05-07T04:26:07.613 回答