Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

pppfff

macrumors newbie
Original poster
Dec 23, 2010
8
0
Freiburg / Germany
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]
 
Last edited by a moderator:
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.
 
This works for me in the Simulator. Not very different from the Obj-C Version ;)
 

Attachments

  • Screenshot 2014-06-21 23.41.42.png
    Screenshot 2014-06-21 23.41.42.png
    69.5 KB · Views: 350
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)];
 
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?
 
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
 
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.
 
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"
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.