How do I check if there is a selection? - objective-c

I have a NSTextView and I need to check if there is a selection, (blue highlight) of a word (or anything really), and not just a cursor. How can I do this. nil doesn't work, and I can't figure it out.

There can be multiple selection in an NSTextView, the methods selectedRanges returns an array of all the selections. If there is just a cursor this method returns a single NSRange with the location giving where the cursor is and the length set to zero.
So your question can be answer with:
NSArray *allSelections = myTextView.selectedRanges;
BOOL hasSelection = allSelections.count > 1
|| (allSelections.count == 1 && allSelections[0].length != 0);
HTH

You can use [NSTextView selectedRanges] method to see if there is a selection.
if (self.textView.selectedRanges.count > 0) {
NSLog(#"Some text is selected!");
}
You may want to read the documentation for more information.

Related

IBAction to display next object of array

I'm trying to do something I thought was going to be very simple but I can't figure it out. I'm filling an array with objects like this in TableViewController:
if (indexPath.row == 0) {
detailController.textArray = [[NSMutableArray alloc] initWithObjects:#"Text1", #"Text2", #"Text3", #"Text4", #"Text5", nil];
}
Then in the DetailController I added this action to make the label display the next object of the textArray:
- (IBAction)nexTextButtonPressed:(id)sender {
int i = 0;
if (i<[textArray count])
i++;
textLabel.text = [textArray objectAtIndex:i];
}
I connected the button in ib to the action with option Touch Down (tried some of the others too). Something isn't working, the button jumps to the second object of the array but then it doesn't work any more. What could be causing this and how to fix it?
int i = 0; is initialized inside the method, it will always be reinitialized to zero on every call which will not let you move forward from second object
Well every time you hit the button i is getting reset to 0 so when it hits the if statement yes it goes into the statement as the condition is correct, but then you are incrementing i by 1 so i becomes 1 then you retrieve the array index 1 every time. You aren't actually doing anything else. No loop to print through the array or anything.

How to move cursor position forward for a particular length?

I have a UITextField, how can I change the cursor position dynamically based of some condition to a particular length?
Use textview.selectedRange.
NSRange selection = [textview.text rangeOfString:#"SomeText" options:NSCaseInsensitiveSearch];
if( selection.location != NSNotFound ){
textview.selectedRange = selection;
textview.selectedRange = NSMakeRange(selection.location, 0);
}
It may helps you
Use this link maybe it will help you solve your issue. Hope it is of use !! :)
Change cursor in UITextField when set new text
and also Finding the cursor position in a UITextField
// Get the selected text range
UITextRange *selectedRange = [self selectedTextRange];
// Calculate the existing position, relative to the end of the field (will be a - number)
int pos = [self offsetFromPosition:self.endOfDocument toPosition:selectedRange.start];
// Change the text
self.text = lastValidated;
// Work out the position based by offsetting the end of the field to the same
// offset we had before editing
UITextPosition *newPos = [self positionFromPosition:self.endOfDocument offset:pos];
// Reselect the range, to move the cursor to that position
self.selectedTextRange = [self textRangeFromPosition:newPos toPosition:newPos];
Happy coding....

For loop variable always evaluating to true

Annoying newbie problem here. This variable isPlayerTouchingAnotherPlayer is being set to true as soon as I touch the piece. I'm almost positive I know why but I can't find a way to display this in log to confirm. I could probably do it by setting different flag numbers for each of the objects but I was hoping there is another way.
The problem is that piece is an object that is also located in p1Array so as soon as I touch it it hits itself and isPlayerTouchingAnotherPlayer is evaluated to true.
Is there a way I could print out the view or image name of the objects touching somehow to confirm this? And furthermore, is there a way to somehow avoid this annoying conflict.
for (int i = 0; i <[p1Array count]; i++) {
UIImageView *tempP1;
tempP1 =[p1Array objectAtIndex:i];
if (CGRectIntersectsRect(piece.frame, tempP1.frame)) {
NSLog(#"selected piece: %#, touched piece: %# ", piece, tempP1);
isPlayerTouchingAnotherPlayer = TRUE;
}
}
why not use fast enumeration and skip the image view you are not interested in checking.
for (UIImageView *imageView in p1Array) {
if (imageView == piece)
continue;
if (CGRectIntersectsRect(imageView.frame, piece.frame)) {
// do whatever
}
}
it seems like you are already printing out the names of the objects touching in the code sample you have provided. if you want to print out specific properties of the objects you can do that to.
as soon as i touch it it hits itself and isPlayerTouchingAnotherPlayer
is evaluated to true.
Then you should get a log message that shows the same object for the selected piece and the touched piece. If that's what's happening, then just add a condition to your if to prevent it:
if (piece != tempP1 && CGRectIntersectsRect(piece.frame, tempP1.frame)) {
Simply
NSLog(#"%# %#", piece, tempP1);
or
NSLog(#"%ld %ld", (NSInteger) piece, (NSInteger) tempP1);
The first will show you the description of the object, the second the address in memory, if it's same address, it's the same object.
You can simply check if it's the same object (pointer) with a simple equal check to exclude the same object :
if (piece != tempP1) {
if (CGRectIntersectsRect(piece.frame, tempP1.frame)) {
...
}
Furthermore, you would like to write this:
for ( int i = 0;
( ( i < [p1Array count] )
&& ( ! isPlayerTouchingAnotherPlayer )
);
i++
) {
...
}

How can I use multiple items in one if statement?

in this case, i'm using:
- (IBAction)reset:(id) sender {
if ((boxHide1.hidden = YES) && (boxHide2.hidden = YES)) {
resetHide.hidden = NO;
}
}
How can I do this? I have 12 items all together I need in the statement. Thanks!
You could use the solution #Joe posted but as you can tell the code for hiding / unhiding could get very messy and hard to read.
If you want to keep your code clean and easy to understand / maintain, I'd put all these buttons into a NSMutableArray and iterate through it to determine whether you want to show the reset button or not.
BOOL showResetButton = YES;
for (UIButton *button in buttonsArray)
{
if (button.hidden == NO) // If any of the buttons is not hidden do not show the reset button
showResetButton = NO;
}
resetButton.hidden = showResetButton;
Make sure you use == to compare values but since they are already booleans you do not need to compare against YES. If all the comparisons are AND(&&) that is correct and you can drop the parenthesis, otherwise if there are any OR(||) operations then you would need to group the appropriate operations.
if (boxHide1.hidden &&
boxHide2.hidden &&
... &&
boxHide12.hidden)
{
resetHide.hidden = NO;
}

How to check the text of an NSTextField (Label)

In Xcode I'm trying to get the text of an NSTextField (Label) to see if it says Yes or it is says No
I've tried this:
if ([LabelYesNo StringValue] == #"Yes"){
[LabelYesNo setStringValue:#"No"];
else{
[LabelYesNo setStringValue:#"Yes"];
}
}
and
if (LabelYesNo isEqualToString #"Yes"){
[LabelYesNo setStringValue:#"No"];
else{
[LabelYesNo setStringValue:#"Yes"];
}
}
and a few other variations of that. Just can't seem to get it right.... Can anyone help?
Thanks
[[theTextField stringValue] isEqualToString:#"Yes"];
should work
in your first code, you're comparing strings via ==. Using the C == operator will simply compare the addresses of the objects.
in your second code, your whole code is wrong, and you'are trying to compare element of type NSTextField to NSString.
see String comparison in Objective-C