Assigning NSString value to label - interface-builder

I have tried to apply the value from a NSString to a label in IB. The problem im seeing, is that the value assigne to the label is:
UITextField: 0x6d4e0f0; frame = (7 86; 97 31); text = '2'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; layer = CALayer: 0x6d4e210
I obviously would like to just see "2" rather than the whole UITextfield data.
Any clues?

In your label say:
[label setText: theTextField.text];
Instead of:
[label setText: theTextField];
My guess is your passing the whole UITextField object, but you really just want to pass the .text part of it.
For your comment:
NSString date = [NSString stringWithFormat:#"%#/%#/%#", day, month, year];

Related

Updating the size of NSTextField with respect to length of the string

I am trying to update the height of the NSTextField w.r.t the length of the string but if the string is light the last portion gets cut off.This is my code.
self.solutionTextView.frame = CGRectMake(self.solutionTextView.frame.origin.x, self.solutionTextView.frame.origin.y, self.solutionTextView.frame.size.width + 25 ,self.solutionTextView.frame.size.height + 25);
//self.solutionTextView.sizeToFit()
What am i doing wrong?
If the NSTextField is allowed to wrap the string, the following steps are the standard (or usual? or one of more possible?) ways to determine the wanted height of the field. Assume the width of the field is given:
NSRect frame = [textField frame];
Now make it very high:
NSRect constraintBounds = frame;
constraintBounds.size.height = 10000.0;
[textField setFrame: constraintBounds];
and set the string:
[textField setStringValue:theString];
Ask now for the natural size:
NSSize naturalSize =
[[textField cell] cellSizeForBounds:constraintBounds];
And you are done:
frame.size = naturalSize;
[textField setFrame:frame];

Add values to NSTableview in a way that lets me reorder them at will

I have an NSTableView and need to add values. I was thinking of doing it with a NSArrayController, but near my table there are four buttons: Move to top, Move up, Move down, Move to bottom. I must be able to reorder the entries in the table with these buttons.
The only thing I can think of is to use an array of dictionaries, where the first entry of the dictionary is the value displayed and the second entry of the dictionary is a double that is used to sort the array. If I move a value up or down, I take the sort value of the entry before and after the destination sum them, and then I divide by two.
I am not sure that the solution I was thinking of is appropriate. What would be the best approach to this scenario?
------EDIT-----
Working on it, and now I am having difficulties writing the updated "order" value into the arraycontroller. Beside that i am having trouble in actually sorting the table with the "order" column once that the value has been updated. Here is my code:
-(IBAction)singleMoveKeywordUp:(id)sender
{
NSInteger selectedRow = [singleKeywordTable selectedRow];
double firstnum;
double secondnum;
double newnum;
NSMutableDictionary *kwmutabledict = [[NSMutableDictionary alloc] init];
if (selectedRow < 2)
{
firstnum = 0;
kwmutabledict = [keywordscontroller.arrangedObjects objectAtIndex:0];
secondnum = [[kwmutabledict valueForKey:#"order"] doubleValue];
}
else
{
kwmutabledict = [keywordscontroller.arrangedObjects objectAtIndex:selectedRow-2];
firstnum = [[kwmutabledict valueForKey:#"order"] doubleValue];
kwmutabledict = [keywordscontroller.arrangedObjects objectAtIndex:selectedRow-1];
secondnum = [[kwmutabledict valueForKey:#"order"] doubleValue];
}
NSMutableDictionary *newkwmutabledict = [[NSMutableDictionary alloc] init];
newnum = (firstnum + secondnum)/2;
[newkwmutabledict setObject:[NSString stringWithFormat:#"%#",[kwmutabledict valueForKey:#"keyword"]] forKey: #"keyword"];
[newkwmutabledict setObject:[NSString stringWithFormat:#"%f",newnum] forKey: #"order"];
[keywordscontroller.arrangedObjects replaceObjectAtIndex:selectedRow withObject:newkwmutabledict] ; //<--------
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:#"order" ascending:YES];
[singleKeywordTable setSortDescriptors:[NSArray arrayWithObject:sort]];
}
This is failing at the row marked with an Arrow with the error message -[_NSControllerArrayProxy replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance 0x100450520
I can't figure out whats wrong.
keywordscontroller.arrangedObjects returns (NSArray *) which is NOT mutable, and NSArray does not contain selector "replaceObjectAtIndex:withObject:". Hence the exception/error "unrecognized selector".

Xcode Add (Math) Label and Textfield

Hi I am trying to add (math not put letters together) a textfield's data, and a label's data together. I have preformed the same code you see below for multiplication but when I do the same (A little changed) it does not seem to work. The code I placed below just shows the value of the billAmountTextField. Do you know why? Thanks for your help!
-(IBAction)totalAllUp:(id)sender
{
NSString *billTotalAmountString = [billAmountTextField text];
float billTotalFloat =[billTotalAmountString floatValue];
NSString *tipTotalAmountString = [resultLabel text];
float tipTotalAmountFloat = [tipTotalAmountString floatValue];
float totalAmountAfterAdding = billTotalFloat + tipTotalAmountFloat;
NSString *finalAnswer = [NSString stringWithFormat:#"%0.2f", totalAmountAfterAdding];
[totalAfterAdd setText:finalAnswer];
}

Change the Color of specific substrings in NSTextView

I want to change the color of specific text in NSTextView. The method should check that after a keydown event.
For example: the word void is finished and the string void changes the
color to blue. Like a code editor.
I searched for a long time but don't find anything.
My Code:
NSRange range = [text rangeOfString:#"void"];
NSString *substring = [[text substringFromIndex:NSMaxRange(range)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
//I think, here is the mistake.
NSAttributedString *now = [NSAttributedString initWithString:substring];
[now setTextColor:[[NSColor blueColor]]];
I have read that i have to use a NSAttributedString but i don't know how I can get this class from a string.
I'am quite new in cocoa programming.
Thanks for every help!
You can do this way:
NSString *str = #"Hello. That is a test attributed string.";
//in place of NSMakeRange put your range
[self.textview setRichText:YES];
[self.textview setString:str];
[self.textview setTextColor:[NSColor redColor] range:NSMakeRange(3,5)];

How do I make 2 labels add together with each other?

Hi I'm kinda new to Xcode and I'm trying to make an app where you press a button and the number will go up; and I have 2 buttons and 2 labels. I've got it to where the 2 labels will count up, but now I'm wanting the numbers from both labels to add together and show in a different label. Is there any line I can add to the buttons to make them just count up in the other label as well or do I need to have a separate action and/or button?
Thanks
Straight up:
int sum = [[label1 text] intValue] + [[label2 text] intValue];
label3.text = [NSString stringWithFormat:#"%#", sum];
Should work, just make sure to replace the pointers I used with the ones you're using.
esqew's answer would do the trick, but the format specifier is incorrect.
If the variable sum is in fact an int ...
label3.text = [NSString stringWithFormat:#"%#", sum];
should be:
label3.text = [NSString stringWithFormat:#"%d", sum];
%# is for Objective-C objects, an int is not an Objective-C object.
Reference:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html

Resources