locked
How do you allow users to paste RRS feed

  • Question

  • User306227 posted

    How do you allow users to paste ? if paste text is in english should past the txt or is should past the question mark.

    Monday, July 13, 2020 9:14 AM

Answers

  • User369979 posted

    We can't stop the user from putting things into the pasteboard. But we could stop it displaying on our UITextField. Customize a textfiled delegate like:

    public class CustomTextFieldDelegate : UITextFieldDelegate
    {
        public override bool ShouldChangeCharacters(UITextField textField, NSRange range, string replacementString)
        {
            // replacementString here is what you want to display return true or return false
            if (...)
            {
                return true;
            }              
            return false;
        }
    }
    

    And then set it to your text field:

    MyTextField.Delegate = new CustomTextFieldDelegate();
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, July 14, 2020 6:52 AM
  • User369979 posted

    What's the issue you are facing now? When returning true, there's no need to reset the textview's text.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, July 15, 2020 5:51 AM
  • User369979 posted

    I still can't get "should replace the question mark after the text but its replacing questing mark". If you don't want to replace the original text, try this:

    NSMutableString str = new NSMutableString();
    str.Append(new NSString(textField.Text));
    str.ReplaceCharactersInRange(range, new NSString(replacementString));
    textField.Text = str;
    return false;
    

    If the replacementString doesn't meet your requirements, replace this value to question marks.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, July 16, 2020 7:18 AM

All replies

  • User369979 posted

    I can't fully get what you want to achieve here. Could you please give more descriptions? If you want to customize the menu items, refer to UIMenuController: https://docs.microsoft.com/en-us/dotnet/api/uikit.uimenucontroller?view=xamarin-ios-sdk-12

    Tuesday, July 14, 2020 1:50 AM
  • User306227 posted

    i have restrict the language while pasting the text ..i have past only english language in textview

    Tuesday, July 14, 2020 3:37 AM
  • User306227 posted

    A user can still paste anything into the text field if i use tf.keyboardType = UIKeyboardType.ASCIICapable

    Tuesday, July 14, 2020 3:55 AM
  • User369979 posted

    We can't stop the user from putting things into the pasteboard. But we could stop it displaying on our UITextField. Customize a textfiled delegate like:

    public class CustomTextFieldDelegate : UITextFieldDelegate
    {
        public override bool ShouldChangeCharacters(UITextField textField, NSRange range, string replacementString)
        {
            // replacementString here is what you want to display return true or return false
            if (...)
            {
                return true;
            }              
            return false;
        }
    }
    

    And then set it to your text field:

    MyTextField.Delegate = new CustomTextFieldDelegate();
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, July 14, 2020 6:52 AM
  • User306227 posted

    Can we replace string???? If it's false

    Wednesday, July 15, 2020 2:40 AM
  • User306227 posted

    Can we replace String with question mark

    Wednesday, July 15, 2020 2:41 AM
  • User369979 posted

    Yes. Change its text directly like:

    textField.Text = "Set whatever you want here";
    return false;
    
    Wednesday, July 15, 2020 2:50 AM
  • User306227 posted

    this my code

    private bool VoiceMsgTextView_ShouldChangeText(UITextView textView, NSRange range, string text) { UIPasteboard pb = UIPasteboard.General;

            var language = NSLinguisticTagger.GetDominantLanguage(text);
            if(language == "en")
            {
                textView.Text = pb.string;
                return true;
            }
            else
            {
                if (language == "" || language == null)
                {
                    textView.Text = "";
                }
                else
                {
                    textView.Text = "????";
                    language = "";
    
                    return false;
                }
            }
            return false;
        }
    

    i m not getting whats wrong with my code

    Wednesday, July 15, 2020 4:16 AM
  • User369979 posted

    What's the issue you are facing now? When returning true, there's no need to reset the textview's text.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, July 15, 2020 5:51 AM
  • User306227 posted

    private bool VoiceMsgTextView_ShouldChangeText(UITextView textView, NSRange range, string text) { if (text.Contains(UIPasteboard.General.String ?? text)) { var language = NSLinguisticTagger.GetDominantLanguage(text); if (language == "en") { return true; } else {
    textView.Text = "????"; return false; } } else { return true; } }

    if there is already text in textview it should replace the question mark after the text but its replacing questing mark .. this is my code

    Thursday, July 16, 2020 6:40 AM
  • User369979 posted

    I still can't get "should replace the question mark after the text but its replacing questing mark". If you don't want to replace the original text, try this:

    NSMutableString str = new NSMutableString();
    str.Append(new NSString(textField.Text));
    str.ReplaceCharactersInRange(range, new NSString(replacementString));
    textField.Text = str;
    return false;
    

    If the replacementString doesn't meet your requirements, replace this value to question marks.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, July 16, 2020 7:18 AM
  • User306227 posted

    this is working for me private bool TextView_ShouldChangeText(UITextView textView, NSRange range, string text) { if (text.Contains(UIPasteboard.General.String ?? text)) { var language = NSLinguisticTagger.GetDominantLanguage(text); NSMutableString str = new NSMutableString(); str.Append(new NSString(textView.Text)); str.ReplaceCharactersInRange(range, new NSString(language == "en"?text:"????")); textView.Text = str; return false; } else { return true; } }

    but cursor is not coming after the question mark please find the attachment


    Thursday, July 16, 2020 11:25 AM