iOS: Appending an uneditable string to the end of a UITextView

偶尔善良 提交于 2019-12-07 21:05:04

问题


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.

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

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


回答1:


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;
   }
}

Original:

You need to use UITextViewDelegate

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

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)];
       }
 }

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.



来源:https://stackoverflow.com/questions/10263919/ios-appending-an-uneditable-string-to-the-end-of-a-uitextview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!