ios UITextView 上的清除按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3877980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:51:17  来源:igfitidea点击:

Clear button on UITextView

iosobjective-ciphoneuitextfielduitextview

提问by Abhinav

How can I add a clear button (cross inside a circle) for UITextView like UITextField has?

我怎样才能为的UITextView添加一个清除按钮(圆圈内交叉)喜欢的UITextField了?

采纳答案by GhostRider

just make a uibutton and put it on uitextview and set its action for clear text view;

只需制作一个 uibutton 并将其放在 uitextview 上并将其操作设置为明文视图;

uitextview.frame = (0,0,320,416);

uibutton.frame = (310,0,10,10);
[uibutton setimage:@"cross.png" forcontrolstate:uicontrolstatenoraml];
[uibutton addTarget:self action:@selector(clearButtonSelected:) forControlEvents:UIControlEventTouchUpInside];

-(void)clearButtonSelected{
    uitextview=@"";
}

hope you want to clear the text view text when you click on cross button above is help if not understand then i can send you proper program for that

希望您想在单击上面的十字按钮时清除文本视图文本是有帮助的,如果不明白,那么我可以向您发送适当的程序

回答by PakitoV

Based on the answer from GhostRider a more accurate and up to date implementation:

基于 GhostRider 的回答,一个更准确和最新的实现:

int kClearButtonWidth = 15;
int kClearButtonHeight = kClearButtonWidth;

//add the clear button
self.clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.clearButton setImage:[UIImage imageNamed:@"UITextFieldClearButton.png"] forState:UIControlStateNormal];
[self.clearButton setImage:[UIImage imageNamed:@"UITextFieldClearButtonPressed.png"] forState:UIControlStateHighlighted];

self.clearButton.frame = CGRectMake(0, 0, kClearButtonWidth, kClearButtonHeight);
self.clearButton.center = CGPointMake(self.textView.frame.size.width - kClearButtonWidth , kClearButtonHeight);

[self.clearButton addTarget:self action:@selector(clearTextView:) forControlEvents:UIControlEventTouchUpInside];

[self.textView addSubview:self.clearButton];

And the method

和方法

- (void)clearTextView:(id)sender{
    self.textView.text = @"";
}

You can use this images for the two states of the button:

您可以将此图像用于按钮的两种状态:

UITextFieldButtonUITextFieldButtonPressed

UITextField 按钮UITextFieldButtonPressed

回答by Zorayr

From product perspective, if you're going to have a clear button, you probably want to use a UITextFieldinstead of a UITextViewand UITextFieldsupports a clear button natively - set the clearButtonModeproperty as such:

从产品的角度来看,如果您想要一个清除按钮,您可能希望使用 aUITextField而不是 aUITextView并且UITextField本机支持清除按钮 - 将clearButtonMode属性设置为:

UITextField *textfield = ...;
textfield.clearButtonMode = UITextFieldViewModeAlways;

See screenshot:

看截图:

enter image description here

在此处输入图片说明

You could use UITextFieldViewModeWhileEditingto only present the clear button while the user is actively updating the content.

您可以使用UITextFieldViewModeWhileEditing仅在用户主动更新内容时显示清除按钮。

回答by Kris Markel

There's nothing built in like there is for the UITextField. You'd have to add the view yourself (probably a UIButton) and place it correctly and also somehow get the text to wrap around it correctly. (And I don't think the latter is really possible.)

没有像 UITextField 那样内置任何东西。您必须自己添加视图(可能是 UIButton)并正确放置它,还要以某种方式让文本正确环绕它。(而且我认为后者不太可能。)

Maybe instead you should display a toolbar above the keyboard (or an inputAccessoryViewif you're targeting 3.2 and later) that provides a clear button.

也许您应该在键盘上方显示一个工具栏(或者inputAccessoryView如果您的目标是 3.2 及更高版本),它提供了一个清晰的按钮。

回答by Brabbeldas

For me changing the .frame or the .contentInset properties did not work.

对我来说,更改 .frame 或 .contentInset 属性不起作用。

For me the best result came from:

对我来说,最好的结果来自:

1) adding a UIView to the controller, give it round corners and a border to mimic a UITextView.

1)向控制器添加一个 UIView,给它圆角和一个边框来模仿 UITextView。

self.viewTextBackground.layer.borderColor = [UIColor colorWithRed:171/255.0 green:171/255.0 blue:171/255.0 alpha:1.0].CGColor;
self.viewTextBackground.layer.borderWidth = 1.0f;
self.viewTextBackground.layer.cornerRadius = 9.0f;

2) place UITextView on top of this UIView. Place it so that the borders of the underlying UIView stay visible.

2) 将 UITextView 放在这个 UIView 之上。放置它以便底层 UIView 的边框保持可见。

3) give the UITextView round corners:

3)给 UITextView 圆角:

self.textNote.layer.cornerRadius = 9.0f;

3) Make its width fe. 30pixels less compared to the underlying UIView. You now have space for a clear-button.

3)使其宽度为fe。与底层 UIView 相比少 30 像素。您现在有一个清除按钮的空间。

4) simply add a UIButton to your controller and place it in the top-right corner of the underlying UIView.

4) 只需将一个 UIButton 添加到您的控制器并将其放置在底层 UIView 的右上角。

5) change the buttons properties: set its type to 'custom' and set its image to the image of a grey cross.

5) 更改按钮属性:将其类型设置为“自定义”并将其图像设置为灰色十字的图像。

6) bind an action to the button te clear the UITextView

6) 绑定一个动作到按钮 te 清除 UITextView

回答by Karoly Nyisztor

You can add a clear button like the one in the attached screenshot with minimal coding. Just follow these steps:

您可以使用最少的编码添加一个像所附屏幕截图中的清除按钮。只需按照以下步骤操作:

  • Select the storyboard and drag a UIButton into your UITextView
  • Set the buttons constraints
  • Assign a title or a background image
  • Create the button's IBOutlet reference and the action (see onClearPressed(_:)below) for "Touch Up Inside" in the ViewController
  • Implement the textViewDidChange(_:) UITextViewDelegatedelegate method, and make sure to set the button's isEnabled property based on the textfield content, e.g.:
  • 选择情节提要并将 UIButton 拖到您的 UITextView 中
  • 设置按钮约束
  • 分配标题或背景图像
  • onClearPressed(_:)在 ViewController 中为“Touch Up Inside”创建按钮的 IBOutlet 引用和操作(见下文)
  • 实现textViewDidChange(_:) UITextViewDelegate委托方法,并确保根据文本字段内容设置按钮的 isEnabled 属性,例如:

func textViewDidChange(_ textView: UITextView) { clearButton.isEnabled = !textView.text.isEmpty }

func textViewDidChange(_ textView: UITextView) { clearButton.isEnabled = !textView.text.isEmpty }

  • Implement the onClearPressed(_:)action:
  • 实施onClearPressed(_:)行动:

@IBAction func onClearPressed(_ sender: Any) { textView.text = "" clearButton.isEnabled = false }

@IBAction func onClearPressed(_ sender: Any) { textView.text = "" clearButton.isEnabled = false }

That's it.

就是这样。

clear button in UTextView

UTextView 中的清除按钮