iOS:将不可编辑的字符串附加到 UITextView 的末尾 [英] iOS: Appending an uneditable string to the end of a UITextView

查看:35
本文介绍了iOS:将不可编辑的字符串附加到 UITextView 的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重现一个类似于 iOS 中 Facebook更新状态"视图的屏幕.

I would like to reproduce a screen similar to the Facebook "Update Status" view in iOS.

(此文本应该是可编辑的)步行(任何经过这里的东西都不能编辑)- 在南纳拉宾海滩

(This text should be editable) Walking (Anything past here should not be editable) - at South Narrabeen Beach

用户应该能够在附加字符串的左侧输入/编辑文本.附加的字符串需要包含在其父级中并且可以点击.

The user should be able to enter/edit text to the left of the appended string. The appended string will need to wrap within its parent and be clickable.

有人知道这是怎么做的吗?(我最近也在 Viddy 应用中看到了它).

Does anyone know how this is done? (I have recently seen it in the Viddy app as well).

它会不会是一个不断增长的 UITextField,其中 UIAttributedString 分成 2 行,并在输入文本时更新其框架?

Could it be a growing UITextField with a UIAttributedString split over 2 lines that updates its frame as the text is entered?

推荐答案

更新:看起来您想要的是让用户无论如何都将光标放在签名中,而不是让她输入
在这种情况下,您可能希望使用它

Update: It looks like what you want is to let the user place the cursor in the signature anyway, but not let her type
In that case, you would want to use this instead

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
   NSInteger signatureLength=20;
   if(range.location>self.textView.text.length-signatureLength){
       return false;
   }
   else{
       return true;
   }
}

原文:

你需要使用 UITextViewDelegate

You need to use UITextViewDelegate

实现 - (void)textViewDidChangeSelection:(UITextView *)textView 方法,类似于:

Implement the - (void)textViewDidChangeSelection:(UITextView *)textView method, something like:

对于这个例子,我们假设签名长度是 20,这看起来像这样:

For this example, let's assume the signature length is 20, this would look something like this:

-(void)textViewDidChangeSelection:(UITextView *)textView{
       NSInteger signatureLength=20;
       NSRange newSelection=self.textView.selectedRange;
       if(newSelection.location>self.textView.text.length-signatureLength){
           [self.textView setSelectedRange:NSMakeRange(self.textView.text.length-signatureLength, 0)];
       }
 }

所以基本上每次选择(== 在这种情况下是光标)改变时你都会拦截,如果光标将在签名的中间,你就在之前重新定位它.将选择设置为 0 长度只会更改光标位置.

So basically you intercept every time the selection (== the cursor in this case) changes, and if the cursor is going to be in the middle of the signature, you reposition it just before. Setting a selection with a 0 length just changes the cursor position.

这篇关于iOS:将不可编辑的字符串附加到 UITextView 的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆