I feel like this is a rookie question but I'm stumped. The app will become much more complex than this (subtraction, division, multiplication, math) but in order to work from a foundation... Well, I don't even have a foundation.
-(IBAction)change:(id)sender {
NSString *xText = x.text;
int xValue = [xText intValue];
NSString *yText = y.text;
int yValue = [yText intValue];
int zValue = xValue + yValue;
NSString *zText = [NSString stringWithFormat:#"%d", zValue];
zText = z.text;
Basically what I'm trying to do right now is have two separate text fields collect user-typed numbers. When the "Calculate" button is pressed, the label will change to show the answer. (i.e. x = 4, y = 3, press calculate, z label changes to 7)
My guess is you would like to add the zText to a label?
Simply go:
[label setText:zText];
Where label is the IBOutlet to your label
Related
In old RPGs, the dialogue text would type on screen, and if there was more than could fit on the page there'd be a ... and you'd press to NEXT to continue reading.
I've got a lot of this already working. What I'm stuck on is I need a block of text to programmatically know how to break itself up from page to page using the ...
Normally, the easy route would be to just specify in the dialogue where the breaks should be, but for this specific project I have to allow it to take in a large block of text, then break it into the correct sizes for each page.
Anyone have any thoughts on how to do this? Just counting characters won't work because the font won't be monospaced.
I was having a hell of a time trying to search for this. Found this, but it didn't answer my question.
Much appreciated!
One solution could be:
separate all the words from the big text into an array
go through of it and search for the boundaries based on textbox height
You can implement it eg.:
CGFloat w = 200.0; // RPG textbox width. Get it from actual UI object.
CGFloat h = 150.0; // RPG textbox height. Get it from actual UI object.
NSArray *words = [yourRPGtext componentsSeparatedByString:#" "];
NSString *cur_txt = [words objectAtIndex:0];
int i = 1;
RPG_txt_pages = [[NSMutableArray alloc] init];
while (i < [words count]) {
NSString *next_txt = [cur_txt stringByAppendingFormat:#" %#",[words objectAtIndex:i]];
CGSize size = [next_txt sizeWithFont:yourRPGtextlable.font
constrainedToSize:CGSizeMake(w, 99999)
lineBreakMode:UILineBreakModeWordWrap];
if (size.height > h) {
cur_txt = [cur_txt stringByAppendingString:#"..."];
[RPG_txt_pages addObject:cur_txt];
cur_txt = [words objectAtIndex:i];
} else {
cur_txt = next_txt;
}
i++;
}
[RPG_txt_pages addObject:curText];
The key here is NSString's sizeWithFont method: here is the link to the docs.
IOS7 comment: sizeWithFont is deprecated you can use sizeWithAttributes. Here is an SO answer on this.
If you tell what IOS version are you using I'll modify this answer. Hope it helped!
I have a uislider connected to a label, when the slider moves, the label adjusts its value. I am able to set the increments and the max and min, but i can't figure out how to remove the decimal places from the value..i simply want whole steps. suggestions please
- (IBAction)sliderValueChanged:(id)sender {
self.myLabel.text = [NSString stringWithFormat:#"%f", (roundf(self.mySlider.value)];
}
Display the result as an int instead of a float.
self.myLabel.text = [NSString stringWithFormat:#"%d", (int)roundf(self.mySlider.value)];
I am creating a Point-of-Sale (POS) receipt and need to have item names with a certain width and need to have a price on the right (like float-right in CSS).
I am using https://github.com/FuerteInternational/FTCoreText to create the content. Any ideas how to achieve this without having to create two views and place them on top of each other?
Just to clarify, I need to have this:
Coffee £1.45
milk, skinny
Danish Pastry £1.75
Coca Cola £1.10
Full English Bfst £12.50
So pretty much I need a string to be floating on the left and other string floating on the right ...
The only thing I can think of is inserting in each string f.e. 30 spaces after the first string and assigning wordWrap = truncate middle. Not so sure that this is exactly what you're looking for.
I agree with Brian Shamblen it'l be pretty easy to do with 2 labels.
It looks like what you want isn't really a second column, which Core Text cannot do out of the box, but rather a tabular layout (like good ol' <table>). Fortunately tabular layout can be acheived in Core Text by using tab stops. The only 2 things you need are:
the CHARACTER TABULATION (U+0009) character,
and a paragraph style with the correct tab stops setting.
That can be done this way:
NSString *string = #"Coffee\t£1.45\n milk, skinny\nDanish Pastry\t£1.75\nCoca Cola\t£1.10\nFull English Bfst\t£12.50";
CGFloat width = 200;
NSArray *tabs = #[CFBridgingRelease(CTTextTabCreate(kCTTextAlignmentRight, width, NULL))];
CTParagraphStyleSetting setting = {kCTParagraphStyleSpecifierTabStops, sizeof(tabs), &tabs};
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(&setting, 1);
NSDictionary *attributes = #{CFBridgingRelease(kCTParagraphStyleAttributeName): CFBridgingRelease(paragraphStyle)};
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:string attributes:attributes];
There are two caveats:
You need to know the width of the view when creating the paragraph style.
Core Text won't wrap lines on tab stops which means you have to make sure your text is short enough to fit.
Perhaps you could do the following with just one UILabel per line:
Define a left word like "Coffee" and a right one like "£1.45".
Make a string by concatenating the left word with a space and the
right one.
Define the width and height of the frame that you want the label with that
string to be contained in, with CGSizeMake(width, height).
Estimate the size of that label using sizeThatFits providing the
CGSize constraints that you want as an argument.
If the estimated width of the label is smaller than the required
width, increase the size of your label, by adding a space to your
string and repeat.
In code:
NSArray *leftWords = #[#"Coffee", #" milk, skinny", #"Danish Pastry", #"Coca Cola", #"Full English Bfst"];
NSArray *rightWords = #[#"£1.45", #"", #"£1.75", #"£1.10", #"£12.50"];
CGFloat xOffset = 5;
CGFloat yOffset = 60;
CGFloat labelHeight = 44;
CGFloat labelWidth = 300;
for (int i = 0; i < leftWords.count; i ++) {
UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(xOffset, yOffset + i * labelHeight, labelWidth, labelHeight)];
aLabel.numberOfLines = 1;
aLabel.textAlignment = NSTextAlignmentLeft;
aLabel.layer.borderColor = [UIColor greenColor].CGColor;
aLabel.layer.borderWidth = 1.0f;
NSString *leftWord = [leftWords objectAtIndex:i];
NSString *rightWord = [rightWords objectAtIndex:i];
NSMutableString *spaces = [[NSMutableString alloc] initWithString:#" "];
const CGFloat MAX_WIDTH = 310;
CGSize maxLabelSize = CGSizeMake(MAX_WIDTH, CGFLOAT_MAX);
CGSize expectedSize = CGSizeMake(0, 0);
CGFloat expectedWidth = 0.0;
NSString *phrase = #"";
do {
phrase = [NSString stringWithFormat:#"%#%#%#", leftWord, spaces, rightWord];
aLabel.text = phrase;
expectedSize = [aLabel sizeThatFits:maxLabelSize];
expectedWidth = expectedSize.width;
// Increase spaces
[spaces appendString:#" "];
} while (expectedWidth < MAX_WIDTH);
CGRect frame = aLabel.frame;
frame.size = expectedSize;
aLabel.frame = frame;
[self.view addSubview:aLabel];
}
I have placed the above within my viewDidLoad and did #import <QuartzCore/QuartzCore.h> to allow marking of the border of the UILabel.
Here is a screenshot of my working code:
It works for the "milk, skinny" line too as the text alignment of the label is set to NSTextAlignmentLeft.
I recently came across this problem and found no answers.. so I created a class for anyone that needs it! Let's say you want to create a list of strings like the example in the question:
- (void)createReceipt {
NSString *coffee = [LRString LRStringWithLeft:#"Coffee" right:#"£1.45" maxlength:29 maxleft:16 maxright:11];
}
This "coffee" string would look like this:
Coffee £1.45
"maxlength" is the maximum length of your string
"maxleft" is the maximum length of your left string (coffee)
"maxright" is the maximum length of your right string (price)
If the name exceeds the "maxleft" length, then the last 3 letters of the name (coffee) will be replaced with "..."
Check it out on GitHub here: https://github.com/iosdec/LRString
I'm doing the following in my App right now:
NSInteger myReell1 = 6;
NSInteger myReell2 = 7;
NSInteger myImag1 = -5;
NSInteger myImag2 = 3
+ (WSData *)scientificData
{
float dataSet2D[2] = {0,myReell1 + myReell2};
float dataSet3D[2] = {0,myImag1 + myImag2};
return [WSData dataWithValues:[WSData arrayWithFloat:dataSet2D len:2]
valuesX:[WSData arrayWithFloat:dataSet3D len:2]];
}
This worked fine for me up to now.
Here is what I would like to change and what I would like to ask you for:
I would like to use 4 UITextFields to give the opportunity of personal input to the user, instead of the consistent Integers (myReell1, … ,myImag2) which I use right now.
How would you implement those UITextfields (maybe just 1 example?) into the code above to make the NSInteger part needless.
Please don't mind about the actual stuff inside of the scientific data. ;-)
Presuming this code is inside a view controller, you can:
Create your UITextField member variables in the class.
Subscribe the class to UITextFieldDelegate.
Wire your text fields up in Interface Builder (or instantiate them programatically and add them to the view).
Implement the didFinishEditing delegate method to handle the
actual input.
Say you make 4 textfield boxes, you can get the integer from going
myReell1 = [textFieldOne.text intValue];
and so on, there you have an integer you can use constantly, with no hardcoding
First put Textfield and then when user click some button just put bellow code in you button click metho...
NSInteger myReell1 = [textField1.text intValue];
NSInteger myReell2 = [textField2.text intValue];
NSInteger myImag1 = [textField3.text intValue];
NSInteger myImag2 = [textField4.text intValue];
:)
I am creating a crossword puzzle-type app, and am using the following code to make the grid:
- (void) viewDidLoad{
[...]
//we are inside 2 loops (puzzleRow and i), 15 rows (puzzleRow), 15 cols (i)...
UITextField *inputBox = [[UITextField alloc] initWithFrame:letterFrame];
key = (puzzleRow * 100) + i;
[inputBox setTag:key];
[....]
}
Later, I want to reference the UITextField and get their value, check it against the correct answer, etc.
- (IBAction)checkSolutionButton:(id)sender {
int i = 1;
for (id obj in self.Puzzle) {
int len = [obj length];
for(int j = 0; j < len; j++){
NSRange range = NSMakeRange(j, 1);
NSString *answer = [obj substringWithRange:range];
int key = (i * 100) + j;
UIView *userGuessSquare = [self.view viewWithTag:key];
UITextField *userGuessTextField = [userGuessSquare viewWithTag:key];
NSString *guess = userGuessTextField.text;
NSLog(#"guess: %#", guess);
}
}
}
self.Puzzle is an NSArray of strings (ANSWER1..ANSWER2,ANSWER3..ANSWER4) constituting a crossword puzzle's rows, dots are black squares. The UIView are holders for the UITextField, each UIView and its UITextField has an identical key.
Here I am stuck. I get a warning Incompatible pointer types initializing UITextField with an expression type UIView I thought it was a subclass?
NSString *guess = userGuessTextField.text;
is a no go.
QUESTION:
How can I make a bunch of UITextField(s) and then access them and their values later?
(EDITED to include entire IBAction method).
Using the tags should be fine. To get the UITextField you should be doing this:
UITextField *userGuessTextField = (UITextField*) [self.view viewWithTag:key];
NSString *guess = userGuessTextField.text;
This will cast the UIView to the more specific UITextField, which is ok since you know you've used it. Make sure your tags start above 0 as well.