How to use Swift UITextInput Function textInRange

Discussion in 'iOS Programming' started by pppfff, Jun 20, 2014.

  1. pppfff, Jun 20, 2014
    Last edited by a moderator: Jun 20, 2014

    pppfff macrumors newbie

    Joined:
    Dec 23, 2010
    Location:
    Freiburg / Germany
    #1
    I am still a beginner in IOS programming even I have created some nice Apps for myself.
    You can do a lot with copy and paste :)
    Now I have started to learn SWIFT, because I am coming from AppleScript and not from Objective-C.


    I want to read the marked text from a text view. Pressing a button should move the selected text to a text field.
    Code:
      @IBOutlet var myTextField: UITextView
    
      myTextField.text = "big[B] brown fox[/B] jumps over a lamb"
    
      @IBAction func MyButton(sender: AnyObject) {
    
        var textStart = myTextField.selectedTextRange.start
        println(textStart)
        
       var textEnd = myTextField.selectedTextRange.end
        println(textEnd)
    
        var textRange = myTextField.selectedTextRange
        println(textRange)
      
    
    }
    
    log output is :
    Code:
     
    <_UITextKitTextPosition: 0xb2b78e0> (4F)
    <_UITextKitTextPosition: 0xb2be7c0> (13B)
    <_UITextKitTextRange: 0xb2879e0> (4, 9)B
    
    the Range is startingPoint and Length

    Now my question:

    I need to have the selected text and not just the position.
    There is a class/method with the name = textInRange

    In the documentation I see the following description, but I cannot manage to use it.

    How do I declare it in swift ? (Creating my own functions are working fine.)

    Thank you in advance
    pppfff
    Code:
    [I]Return the text in the specified range. (required)
    
    Declaration
    SWIFT
    func textInRange(_ range: UITextRange!) -> String!
    OBJECTIVE-C
    - (NSString *)textInRange:(UITextRange *)range [/I]
     
  2. pppfff thread starter macrumors newbie

    Joined:
    Dec 23, 2010
    Location:
    Freiburg / Germany
    #2
    Here the solution in Objective-C

    Have created the same code with a new objective-C xcode project and here the solution is:



    Code:
    mySelectedText2 = [myTextField textInRange:(myTextField.selectedTextRange)];
    Any idea about the SWIFT version ?


    PS: Thank you 'balamw' for adding the code tags in my first posting.
     
  3. Dranix macrumors 6502a

    Dranix

    Joined:
    Feb 26, 2011
    Location:
    Gelnhausen, Germany
    #3
    This works for me in the Simulator. Not very different from the Obj-C Version ;)
     

    Attached Files:

  4. pppfff thread starter macrumors newbie

    Joined:
    Dec 23, 2010
    Location:
    Freiburg / Germany
    #4
    Thank you very much Dranix !

    Yes, it is not very different, but was miles away in my mind.

    Swift:

    Code:
    mySelectedText2 =  myTextField.textInRange(myTextField.selectedTextRange)
    Objective-C:

    Code:
    mySelectedText2 = [myTextField textInRange:(myTextField.selectedTextRange)];
     
  5. Dranix macrumors 6502a

    Dranix

    Joined:
    Feb 26, 2011
    Location:
    Gelnhausen, Germany
    #5
    Oh most of the thanks should go to the autocomplete function in Xcode ;)

    And on a side - Does the simulator crash for you as much as it does for me in Xcode6 beta2?
     
  6. pppfff thread starter macrumors newbie

    Joined:
    Dec 23, 2010
    Location:
    Freiburg / Germany
    #6
    Hi Dranix,
    the Beta 2 Simulator is quite stable on my older iMac (early 2009) running with Mavericks. Yesterday Xcode crashed and the days before the indexing process was running all the time and CPU load was round 200%.
    I had to start a new project, because I could not find the issue even I googled a lot.
    But I have only little projects and cannot say something about real big projects.

    You are giving autocomplete most of the credit :)
    I am using autocomplete all the time but I guess I was so focussed on the normal way a function is used that I was looking only in this direction.
    Your free thinking in all directions has enabled your search in autocomplete.
    I am an old boy and I am coming from "a=a+1" and not from "a++".
    Thanks again.
    Peter
     
  7. Dranix macrumors 6502a

    Dranix

    Joined:
    Feb 26, 2011
    Location:
    Gelnhausen, Germany
    #7
    Yeah that can happen ;)

    Well I have been tinkering around the bugs of Swift since day 1 so it gets a little comfy in usage.

    Btw, if you have a question and don't want to resort to english just send a pm and I try to help.
     
  8. pppfff, Jun 22, 2014
    Last edited: Jun 24, 2014

    pppfff thread starter macrumors newbie

    Joined:
    Dec 23, 2010
    Location:
    Freiburg / Germany
    #8
    Thank you Dranix,
    yes, I will surely need to ask questions in our native language in future time.
    But then I will translate and post the answer here, to give back solution and not only post questions. :)


    Here another example I was puzzling but have managed it to translate from objective-C to Swift:

    Paste a text from general clipboard:

    Objective-C code:
    Code:
    [[UIPasteboard generalPasteboard] containsPasteboardTypes:UIPasteboardTypeListString];
    
    
    pasteBoard = [UIPasteboard generalPasteboard];
    
    myTextView.text =  [NSString stringWithFormat:@" %@",[pasteBoard string]];
            
    
    Swift Code:
    
    myTextView.text = UIPasteboard.generalPasteboard().string
    
    
    Update:

    Swift code to copy a text to clipboard is:

    Code:
     UIPasteboard.generalPasteboard().string = "test Text" 
     

Share This Page