UITextView上的清除按钮

Abh*_*nav 10 iphone objective-c uitextfield uitextview ios

如何为UITextView添加一个清晰的按钮(在圆圈内交叉),如UITextField?

Pak*_*toV 11

基于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];
Run Code Online (Sandbox Code Playgroud)

和方法

- (void)clearTextView:(id)sender{
    self.textView.text = @"";
}
Run Code Online (Sandbox Code Playgroud)

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

UITextFieldButton UITextFieldButtonPressed


Gho*_*der 9

只需制作一个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=@"";
}
Run Code Online (Sandbox Code Playgroud)

希望你想清除文本视图文本当你点击上面的交叉按钮是帮助,如果不明白然后我可以发送给你合适的程序

  • 遇到同样的需要,在哪里得到这个cross.png? (3认同)

Zor*_*ayr 8

从产品的角度来看,如果你想要一个清晰的按钮,你可能想要使用a UITextField而不是a UITextView并原生UITextField支持一个清除按钮 - 设置clearButtonMode属性如下:

UITextField *textfield = ...;
textfield.clearButtonMode = UITextFieldViewModeAlways;
Run Code Online (Sandbox Code Playgroud)

看截图:

在此输入图像描述

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

  • (叹气.)问题是如何将一个Clear按钮添加到UITextView(而不是UITextField)......并且没有ClearButtonMode成员函数...... !! (7认同)