当用户点击 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/