I'm currently implementing a translation application. One that when a user selects a word from a UITextView the application translates to a different language.
I would like to restrict the options for selection to only whole words for example:
"Hello World" and not "Hello Wo"
Similar to the iOS Kindle application when defining words in the dictionary.
I have the UITextViewDelegate in my .h file.
I currently have in my .m:
- (void)textViewDidChangeSelection:(UITextView *)textView{
[self translate];
}
and
- (void)translate {
if(![[self.descriptionTextView selectedTextRange] isEmpty]) {
NSString * selectedWord = [self.descriptionTextView textInRange:[self.descriptionTextView selectedTextRange]];
...
I then go on to convert this to an encodedString and fires off the Google Translate API. Is there a way to only select whole words from the UITextView? So that the selection increments in whole word chunks.
You can add yourself as the inputDelegate of the text field and implement selectionDidChange:. When the selection changes you can use positionWithinRange:atCharacterOffset: to investigate around the selected range for white space. Once you've found your desired range you can update the selectedTextRange.
Related
I have Objective C code that continually updates a set of numerical values. I need to display these values on the screen. That's it! I can convert numerical values into a text string, no problem. But how do I display this string in a UI element? Do I use a text box or a text field or a text view, or a...? I cannot find examples to show how to pass a string from code into the UI. I assume I need to set up a text thingy, and then periodically refresh the contents of that text thingy when the values change?
I assume the answer is simple but is just obscured by a smokescreen of technical jargon.
Thanks!
From the UI perspective you might want something like a
UITextView - multi-line text input
UITextField - single-line text input
UILabel - just text
For your purpose of just printing text, you should use UILabel, since you dont want / need any kind of input. You can access its text using:
// yourLabel is your current UILabel* you want to output yourValue to
yourLabel.text = yourValue;
Of course that yourValue needs to be converted to NSString before.
To actually get hold of the UILabel, you need to connect it from the Interface Builder as an IBOutlet. For tutorials on that topic, take a look at tutorials like Interface Tutorials by Ray Wenderlich or youtube or just google Interface Builder tutorial.
I have been struggling with this. User needs to enter text and/or emoticons in a TextView. I got an emoticon keyboard with my own images to enter emoticons. Problem is I need to keep a symbol (e.g. "(smile)" for the emoticon within the text while AT THE SAME TIME showing the emoticon picture on top of the symbol.
So user would see "Hello [the picture]" while the TextView.text property would return "Hello (smile)".
On Android you can use Spanned strings which allow you to cover part of your text with an image. Thus on Android I managed to achieve my objective without problem.
On iOS, I thought Attributed Strings were a similar concept to Spanned but so far all I have been able to do is entirely replace the emoticon's code with the picture (using NSTextAttachment). Is there a way to achieve my objective without having to maintain one attributed string containing pictures and one separate string containing codes?
You can use this method, Hope it will work for you.
- (NSAttributedString*) parseEmoticons:(NSAttributedString*)text {
text = [text stringByReplacingOccurrencesOfString:#":-)" withString:#"😄"];
text = [text stringByReplacingOccurrencesOfString:#";P" withString:#"😜"];
text = [text stringByReplacingOccurrencesOfString:#"B-)" withString:#"😎"];
text = [text stringByReplacingOccurrencesOfString:#";-P" withString:#"😜"];
return text;
}
Having failed to find a more elegant solution, I resorted to maintaining one attributedstring containing the emoticon picture, and one regular string to hold the emoticon codes. So my attString is for instance "Hello [Smiling picture]" while my string is "Hello %101%". If you are interested in building a chatting app as I am, here is the pseudo code:
In emoticon keyboard:
{
Insert picture into attributed string at location loc;
Call textView shouldChangeTextInRange:(loc,0) replacementText:"%101";
}
In the view controller at shouldChangeTextInRange:(loc,length) replacementText:text:
{
Parse regular string to jump over emoticon codes already there to find the location matching loc;
Replace text (for instance %101%) in regular string} at the identified location.
}
Note: shouldChangeTextInRange is also called for regular keyboard entries including delete.
I am trying to a text parser for iOS. I have some syntax to bold, italic, highlight, etc. I have set it up in a UITextView with an instance of NSTextStorage to monitor the changes and set the correct attributes. If I preload the UITextView with text it works great.
However when I'm typing and let's say I type something like this
==a==
It works great it highlights it yellow and everything is fine. However if I place the cursor right next to the last == and starting typing to create something like this:
==a==hello my name is...
Then the rest of the text also gets highlighted.
I know the regex I use is not the problem because if I pre fill the UITextView with that text it parses it correctly.
Question: So what I need help with is resetting the the current cursor position to no text attributes. I'm not sure how to do that because if it is at the end of the text view I can't go forward because then I get an NSRangeException.
Thanks!
I figured this out, what you have to do is in the delegate for UITextView implement the textViewDidChange: method and set the typingAttributes.
Like this:
- (void)textViewDidChange:(UITextView *)textView {
textview.typingAttributes = #{...};
}
Now every time the text updates it will reset to the default attributes.
I have custom TextView to edit text with simple UIKeyInput.
When I select word from quick type suggestions it sends empty string to
- (void) insertText:(NSString *)text
How to get word from quick type?
I've got a few UITextFields in an UITableView. The user should be able to insert only numbers and dots. To do this, I set the keyboard type to UIKeyboardTypeNumberPad and added a '.'-Button at the bottom left corner. Every time the button is pressed, a function is called. This function should insert a dot at the current cursor position, but this is the problem: UITextField hasn't got an selectedRange property, so I'm not able to get the current cursor position. Does anybody know how to solve this problem or is there any other way to do this? Thanks.
I've finally found a solution for this problem! You can put the text you need inserted into the system pasteboard and then paste it at the current cursor position:
[myTextField paste:self]
I found the solution on this person's blog:
http://dev.ragfield.com/2009/09/insert-text-at-current-cursor-location.html
The paste functionality is OS V3.0 specific, but I've tested it and it works fine for me with a custom keyboard.
Update: As per Jasarien's comment below, it is good practice to save off the pasteboard contents first and restore them afterward. For convenience, here is my code:
// Get a reference to the system pasteboard
UIPasteboard* lPasteBoard = [UIPasteboard generalPasteboard];
// Save the current pasteboard contents so we can restore them later
NSArray* lPasteBoardItems = [lPasteBoard.items copy];
// Update the system pasteboard with my string
lPasteBoard.string = #"-";
// Paste the pasteboard contents at current cursor location
[myUIField paste:self];
// Restore original pasteboard contents
lPasteBoard.items = lPasteBoardItems;
[lPasteBoardItems release];
- (void)insertText:(NSString *)text
available in iOS 3.2 and later.
In Swift
This inserts text at the current cursor position.
textField.insertText("Hello")
My full answer about working with the cursor position is here.
There is now a fantastic number pad type keyboard that includes a decimal point, no configuration necessary except setting (not in IB, not an option there yet) the UITextField's keyboardType property to UIKeyboardStyleDecimalPad, like so:
textField.keyboardType = UIKeyboardTypeDecimalPad;
This does all the fancy stuff you've been trying to do automatically.
If you're looking to create text fields that allow you to enter decimal places may I suggest that you rather fix the decimal point to a certain precision and allow the user to enter numbers as follows:
Assuming your precision is 2 decimal points:
start: value is 0.00
user enters 1: value is 0.01
user enters 2: value is 0.12
user enters 3: value is 1.23
user enters 4: value is 12.34
See What is the best way to enter numeric values with decimal points? for a similar solution for currency.
It is by far a more simple solution than creating a custom keyboard and dealing with all the nuances that that approach presents. If, however, you need variable length precision then that approach might suite you better.
Read the value of the text field as a string when your button is pressed. Edit the string and then set the value of the field to the new string.