Xcode - Multiple values from single input - objective-c

I'm trying to learn a little about Xcode, and I'm stuck on trying to get multiple values from a single input.
- (void)degreeConvert:(id)sender
{
double timelonn = [tempTextBox.text doubleValue];
double monedslonn = (timelonn * 162.5);
// double arslonn = (timelonn * 1950);
[tempTextBox resignFirstResponder];
NSString *convertResult = [[NSString alloc] initWithFormat: #"Månedslønn: %0.f", monedslonn];
// NSString *convertResult = [[NSString alloc] initWithFormat: #"Årslønn: %0.f", arslonn];
calcResult.text = convertResult;}
This takes my input 'timelonn' (hourly wage/income) and returns 'monedslonn' (monthly wage/income). The double-dashed comments is my rookie idea of how I could get it to display 'arslonn' (yearly wage/income) as well.
Am I far off here?

You're not too far off. If you uncomment your first commented line, and then change your line where you set your convertResult, you can set the convertResult string to something with multiple lines, like so:
- (void)degreeConvert:(id)sender
{
double timelonn = [tempTextBox.text doubleValue];
double monedslonn = (timelonn * 162.5);
double arslonn = (timelonn * 1950);
[tempTextBox resignFirstResponder];
NSString *convertResult = [[NSString alloc] initWithFormat: #"Månedslønn: %0.f\nÅrslønn: %0.f", monedslonn, arslonn];
calcResult.text = convertResult;
}
Note here that in the format string, "Månedslønn: %0.f\nÅrslønn: %0.f", the \n stands for a newline.
Also note that you will have to edit your .nib and change a property of your UILabel. Click on the label and change the (I'm going off Xcode 4 here) Lines property to 2 (or however many lines you wanted). On Xcode 4 it should be the second from the top in the 4-th tab of the Utilities pane (Command-Option-4). While you're at it, resize your UILabel so that it has enough space to hold your multiple lines ;)

Related

How to save text from textview?

I have a textview where you can write multiple lines of text. How do you save that text in a file or variables?
I used the multiline text field but it doesn't let me go to next line unless I hit control enter.
What I'm thinking is like a text editor, after you type everything you save that in a file. Or I can get each line from the text view into a variable.
What's the best way to do this?
NSString can save text up to 4.2 billion characters. \n denotes a line break, so no need to save into multiple parameters.
NSString *text = textView.text;
OSX
NString *text = [[textView textStorage] string];
If you're looking for each individual line for whatever reason, you could use componentsSeparatedByString
NSArray *linesArray = [textView.text componentsSeparatedByString:#"\n"];
Each line will be available at linesArray[0], linesArray[1] etc...
[linesArray count] will give you the total number of lines... with linesArray[[linesArray count]-1] being the last line in the string.
The textView.text property is an NSString also... so when you say saved.. do you mean intra app session? If so you can use NSUserDefaults
Save object
[[NSUserDefaults standardUserDefaults]setObject:textView.text forKey:#"TheKeyForMyText"];
Get object
NSString *text = [[NSUserDefaults standardUserDefaults]objectForKey:#"TheKeyForMyText"];
Assign
In swift
let var_name = textfield.text
Or in objective C
NSString *string_name = textfield.text;
And use the variable where you want to.

Superscript cents in an attributed string

I'm trying to get my label to look like so:
But using attributed string, I managed to get this result:
My code:
NSString *string = [NSString stringWithFormat:#"%0.2f",ask];
NSMutableAttributedString *buyString = [[NSMutableAttributedString alloc] initWithString:string];
[buyString addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:15.0]
range:NSMakeRange(2, buyString.length - 2)];
self.labelBuy.attributedText = buyString;
As you see, the numbers after the dot, stay below, and I would like to pop them to the top as the first example.
Is there any way to set attributed string frame?
You have to use NSBaselineOffsetAttributedName.
From the doc:
NSBaselineOffsetAttributeName
The value of this attribute is an
NSNumber object containing a floating point value indicating the
character’s offset from the baseline, in points. The default value is
0. Available in iOS 7.0 and later.
From your example:
[buyString addAttribute:NSBaselineOffsetAttributeName
value:#(10.0)
range:NSMakeRange(2, buyString.length - 2)];
You may have to change the value to fit your needs.
Why not actually use superscript? You must first #import "CoreText/CoreText.h"
[buyString addAttribute:(NSString *)kCTSuperscriptAttributeName
value:#1
range:NSMakeRange(2, buyString.length - 2)];

UILabel Multiple Lines

I have a UILable which displays text as I press buttons. The text is from an attributed string. One of the buttons calls for a superscript attribute:
string = [[NSMutableAttributedString alloc]initWithString:#"A"];
[string addAttribute:NSFontAttributeName value:(font) range:NSMakeRange(string.length-1, 1)];
[string addAttribute:(NSString*)kCTSuperscriptAttributeName value:#"1" range:NSMakeRange(string.length-1, 1)];
[string2 appendSttributedString: string];
label.attributedText = string2;
This code works as long as string2 fits onto one line in the UILable. When the text begins to span two lines at first it appears as it should. However when the kCTSuperscriptAttributeName superscript attribute is added the second line of the label disappears and gets truncated. Im not sure whats going on. Anyone have an idea?
Make sure that you are setting the numberOfLines property on the UILabel to be 2 or something, so that the label doesn't truncate beyond the first line. Hope this helps!
Did you try 'NSBaselineOffsetAttributeName' for NSAttributedString?
-(void)setText:(id)text withPrefixText:(id)prefixText andSuffixText:(id)suffixText
{
NSString * compondText = [self textByCompoundingText:text withPrefixText:prefixText WithsuffixText:suffixText];
NSMutableAttributedString * attributedCompoundText = [[NSMutableAttributedString alloc] initWithString:compondText];
NSMutableDictionary * prefixTextAttributes = [#{} mutableCopy];
prefixTextAttributes[NSFontAttributeName] = _prefixTextFont? _prefixTextFont:self.font;
prefixTextAttributes[NSForegroundColorAttributeName] = _prefixTextColour? _prefixTextColour:self.textColor;
NSNumber *baselineOffSet =[NSNumber numberWithUnsignedInteger:_prefixTextVerticalPositionning];
prefixTextAttributes[(NSString*)NSBaselineOffsetAttributeName] = baselineOffSet;
NSMutableDictionary * suffixTextAttributes = [#{} mutableCopy];
suffixTextAttributes[NSFontAttributeName] = _suffixTextFont? _suffixTextFont:self.font;
suffixTextAttributes[NSForegroundColorAttributeName] = _suffixTextColour ?_suffixTextColour:self.textColor;
baselineOffSet = [NSNumber numberWithUnsignedInteger:_suffixTextVerticalPostioning];
suffixTextAttributes[(NSString*)NSBaselineOffsetAttributeName] = baselineOffSet;
if(prefixText)
[attributedCompoundText addAttributes:prefixTextAttributes range:[compondText rangeOfString:prefixText]];
if(suffixText)
[attributedCompoundText addAttributes:suffixTextAttributes range:[compondText rangeOfString:suffixText]];
self.attributedText = attributedCompoundText;
}

Setting UILabel Text Multiple Times Within A Loop

I'm fooling around in XCode, trying to learn a little about the iOS SDK and Objective-C.
I have this for loop below, and it should print out several values to the screen (depending on the amount of months chosen), but instead, it's only printing out the final value.
Can anybody point out why?
Thanks a bunch, in advance!
for (int i = 1; i <= myMonthsDouble; i++)
{
myPaymentAmount = (myBalanceDouble/10) + myInterestDouble;
myBalanceDouble -= myPaymentAmount;
//convert myPaymentAmount double into string named myPaymentAmountString
NSString *myPaymentAmountString = [NSString stringWithFormat:#"%f", myPaymentAmount];
NSString *paymentInformation = [[NSString alloc] initWithFormat:#"%# months, %# per month.", monthsString, myPaymentAmountString];
myInterestDouble = (myBalanceDouble * (myInterestDouble/100))/12;
self.label.text = paymentInformation;
}
It is only printing the last value to the screen because you only have one label. Each time you get to the end of the loop, you are setting that label's text which is overriding the last value. If you want to print all of them to the screen, you will need to have either multiple labels or you will have to append the strings together and put them in either a label or a UITextView that is formatted so that they can all be seen (most likely a text view but it can be done with a label.)
One example of doing this would be:
label.text = [label.text stringByAppendingString:newString];
numLines++; //this starts at 0;
and then at the end:
label.numberOfLines = numLines;

Programmatically change the state of a UIButton

I've got a pop-up view that loads when a user clicks on a TableView with Core Data elements. On the pop-up view I have a label that represents an int value.
The pop-up view has two butons, one for decreasing the value of the label by 1 and one for increasing it by one. So + and -
What I want to do is to disable the minus button if the label's value is 0. What I've tried is:
-(void)viewDidLayoutSubviews{
NSString *daString = currentVal.text;
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:daString];
int number = [myNumber intValue];
if (number==0)
minus.enabled = NO;
else
minus.enabled = YES
}
The problem with my code is that the button stays disabled after I increase the label's value, and it's no longer equal to 0.
Any suggestions?
You should keep a reference to minus button e.g.
#property (strong, nonatomic) IBOutlet UIButton *minusButton;
Set it with a value of your minus button, or connect outlet in Interface Builder
in your action handler for plusButton, do something like that
-(IBAction)plusAction:(id)sender {
//Do your business logic
...
self.minusButton.enabled = YES;
}
//In your minusButton action handler
-(IBAction)minusAction:(id)sender {
//Do your business logic
...
NSString *daString = currentVal.text;
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:daString];
int number = [myNumber intValue];
if (number==0)
self.minusButton.enabled = NO;
else
self.minusButton.enabled = YES
}
It seems like you have things the other way around. I would take a totally different approach:
Keep an instance variable (which we'll call 'count') in this viewController which holds the number. it can be an NSInteger. now add a target (self) to both buttons with a #selector(buttonPressed:). now this is how this selector should look like:
- (void)buttonPressed:(id)sender{
if (sender==plusButton)
self.count++;
if (sender==minusButton)
self.count--;
label.text = [NSString stringWithFormat:#"%d",self.count];
minusButton.enabled = (self.count>0);
}
I would just do this with a UIStepper, instead of the 2 buttons. You can set properties right in your storyboard/IB file that specify the max and min, increments, and a bunch of other useful things too. There are a couple video tutorials posted on YouTube that probably cover everything you'll need to know to use it.
Also, I have noticed one thing that...
If the button in disabled state and you are trying to change the title of normal state, it wont work.
I had to change the state to enabled and then I could manipulate title and set back to disabled.