单击TextView时删除归因文本

时间:2013-06-26 02:48:37

标签: 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; 
        }

2 个答案:

答案 0 :(得分:1)

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

- (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){

答案 1 :(得分:0)

您始终可以使用它。制作一个从第一个字符到最后一个字符的NSRange,并使用空字符串“”。

myAttributedString.replaceCharacters(范围:NSRange,带str:字符串)

相关问题