iphone - 单击 TextView 时删除属性文本

标签 iphone ios objective-c

当用户点击 textView 时,我无法弄清楚如何删除属性文本,就像在 textField 上所做的那样。我知道如何分配属性文本,但我希望它在用户单击 textView 时消失,而不是自己删除属性文本。

这是我用来填充的代码:

 if([self.detailItem.comments.rootBeerComment isEqual: @""] || self.detailItem.comments.rootBeerComment == nil){
            NSAttributedString *string = [[NSAttributedString alloc]initWithString:@"Notes..."];
            self.rootBeerNotes.attributedText = string;
        }else{
            self.rootBeerNotes.text = self.detailItem.comments.rootBeerComment; 
        }

最佳答案

为了在点击 textView 时执行某些操作,您需要知道此事件何时发生。最简单的方法是将 textView 的委托(delegate)设置为 View Controller ,然后实现 UITextViewDelegate 委托(delegate)方法,该方法告诉您:

- (BOOL)textViewShouldBeginEditing:(UITextField *)textView
{
    // You may need to check that it is the right textView if you have more than one.
    textView.attributedText = nil;
    return YES;
}

顺便说一句,您可以替换这行代码:

if([self.detailItem.comments.rootBeerComment isEqual: @""] || self.detailItem.comments.rootBeerComment == nil){

用这个,它更短并且做同样的事情,因为空字符串的长度和 nil 对象的长度都将返回 0:

if([self.detailItem.comments.rootBeerComment length] == 0){

关于iphone - 单击 TextView 时删除属性文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17310754/

相关文章:

objective-c - 检查 SKNode 是否在屏幕上且可见

objective-c - 如何将 NSDitonary 从 Swift 转换为 Objective-c?

iOS – 切换 UIWindow 时的释放问题

ios - iOS 13 会在什么条件下要求用户授予 'Always' 位置访问权限?

Iphone:滚动时 UITableview 中的复选标记会混淆

ios - 当用户手动关闭 react-native-ios 应用程序时如何运行一些代码?

ios - Objective C Delegate - 查找父类的名称

iphone - 如何在 iPod touch 和 iphone 中测试我自己的应用程序?

ios - 我的申请会在一年后消失吗?

iphone - 显示相机胶卷等选项的 View 的名称是什么?