I have a label that is controlled for a UISlider. When I get half way with the slider it shows .50. How can I change that .50 to 50%? Thanks so much!
My slider code:
- (IBAction) sliderValueChanged:(UISlider *)sender {
tipPercentLabel.text = [NSString stringWithFormat:#" %.2f", [sender value]];
}
UISlider is a fraction between 0.0 and 1.0.
So how about something like:
tipPercentLabel.text = [NSString stringWithFormat:#" %f%%", ([sender value] * 100)];
The "%%" in the format string indicates a percent character to be printed. I might be a tiny bit off in the format string (maybe cast the multiplied value to an integer and use "%d%%").
Related
The following question is for Objective C preferably (Swift is fine too). How can I get my strings to look like the strings in the picture below? The denominators and the right bracket of the percentage portions need to line up. Obviously the percentages could be 100%, 0%, 0%, which means that the left bracket for the percentages wouldn't line up, which is fine. The amount of space that the percentage part requires would be 9 spots.
I would strongly encourage using the layout engine for such things, but you could simulate yourself with something like the following, which I haven't tested...
// given a prefix, like #"5/50" and a suffix like #"(80%)", return a string where they are combined
// add leading spaces so that the prefix is right-justified to a particular pixel position
//
- (NSString *)paddedPrefix:(NSString *)prefix andSuffix:(NSString *)suffix forLabel:(UILabel *)label {
// or get maxWidth some other way, depends on your app
CGFloat maxWidth = [self widthOfString:#"88888/50" presentedIn:label];
NSMutableString *mutablePrefix = [prefix mutableCopy];
CGFloat width = [self widthOfString:mutablePrefix presentedIn:label];
while (width<maxWidth) {
[mutablePrefix insertString:#" " atIndex:0];
}
// the number of blanks between the prefix and suffix is also up to you here:
return [NSString stringWithFormat:#"%# %#", mutablePrefix, suffix];
}
// answer the width of the passed string assuming an infinitely wide label (no wrapping)
//
- (CGFloat)widthOfString:(NSString *)string presentedIn:(UILabel *)label {
NSAttributedString *as = [[NSAttributedString alloc] initWithString:string attributes:#{NSFontAttributeName:label.font}];
CGRect rect = [as boundingRectWithSize:(CGSize){CGFLOAT_MAX, CGFLOAT_MAX}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
return rect.size.width;
}
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)];
In my current app, I have an equation that typically solves to a pretty long amount of decimal numbers, i.e: 0.12345 or .123 . But the way that I need this to work is to only show say 1 or 2 decimal numbers, so that would essentially produce 0.12 or 0.1 based on the values I mentioned.
In order to do this, I have done the following: Taken my CGFloat to a NSString:
CGFloat eX1 = 0.12345
NSLog(#" eX1 = %f", eX1); //This of course , prints out 0.12345
NSNumberFormatter *XFormatter = [[NSNumberFormatter alloc] init];
Xformatter.numberStyle=NSNumberFormatterDecimalStyle;
Xformatter.maximumFractionDigits=1;
NSString *eX1F = [formatter stringFromNumber:#(eX1)];
NSLog (#"eX1F = %#",eX1F); //This prints out 0.1
But my problem is that I need to keep working with this as a CGFloat after it has been formatted, I have tried taken the string back to a number by doing: numberFromString but the problem is that only works with a NSNumber.
What can I do to format my CGFloat and keep working with it as a CGFloat and not a NSString or NSNumber?
Update I have tried:
float backToFloat = [myNumber floatValue];
but the result is number unformatted : 0.10000 I need those extra 0s out
To convert NSString to a CGFloat you can use floatValue:
CGFloat *eX1rounded = [eX1F floatValue];
But you can round eX1 to eX1rounded directly without using a number formatter,
for example:
CFGloat *eX1rounded = roundf(eX1F * 10.0f)/10.0f;
In any case, you should keep in mind that numbers like 0.1 cannot be represented
exactly as a binary floating point number.
Use stringWithFormat: to round and convert to a string for display purposes:
NSString* str = [NSString stringWithFormat:#"%0.2f", eX1];
You can do the same thing in NSLog, the %0.2f says you want 2 decimal places.
NSLog(#" eX1 =%0.2f", eX1); // this prints "0.12"
I have a few actions in xcode where the number of decimals in the output value needs to be limited to 3 decimal places out. What do I need to add to my code to achieve this task?
Here is an example of one of my actions:
- (IBAction)calculateMolarity:(id)sender {
float ourValue = [[_calcTextFieldNumOne text] floatValue] /[ [_calcTextFieldTwo text] floatValue];
NSNumber *ourNum =[NSNumber numberWithFloat:ourValue];
[_outputOfMolarity setText:[ourNum stringValue]];
NSString* formattedNumber = [NSString stringWithFormat:#"%.03f", ourNum];
here %.03f tells the formatter that you will be formatting a float
(%f) and, that should be rounded to three places, and should be padded
with 0's.
but you can do directly with your float ourValue like this
NSString* formattedNumber = [NSString stringWithFormat:#"%.03f", ourValue];
there is no need to convert your float value to NSNumber
you can use "%.3f" or "%.03f", no matter both gives same fromat
#"%.3f" = 1234.567
#"%.03f" = 1234.567 // which is equal to #"%.3f"
This is the valueChanged voids for my two UISliders. Their function is to set the minimum and maximum price for objects. What I cannot figure out it how to make them not exceed each others values.
So if minPrisSlider.value = 100, it should not be possible to drag the maxPrisSlider below 100 and opposite.
- (void)minValueChanged:(UISlider*)sender
{
NSUInteger index = (NSUInteger)(minPrisSlider.value + 0.5); // Round the number.
[minPrisSlider setValue:index animated:NO];
int progressAsInt =(int)(minPrisSlider.value + 0.5f);
NSString *newText =[[NSString alloc] initWithFormat:#"%d,-",progressAsInt];
minPrisText.text = newText;
}
- (void)maxValueChanged:(UISlider*)sender
{
NSUInteger index = (NSUInteger)(maxPrisSlider.value + 0.5); // Round the number.
[maxPrisSlider setValue:index animated:NO];
int progressAsInt =(int)(maxPrisSlider.value + 0.5f);
NSString *newText =[[NSString alloc] initWithFormat:#"%d,-",progressAsInt];
maxPrisText.text = newText;
}
I have tried adding
[minPrisSlider setMaximumValue:maxPrisSlider.value]; in maxValueChanged
and
[maxPrisSlider setMinimumValue:minPrisSlider.value]; in minValueChanged
but it returns strange results...
Got some working solution for this?
The thing is, the slider's thumb position is proportional to its maximum/minimum value. So setting the maximum/minimum value shouldn't do
In your case, you should try this :
if ([minPrisSlider value] > [maxPrisSlider value])
[minPrisSlider setValue:[maxPrisSlider value]];
And reverse it for the max slider. Don't forget to set the continuous value :
[minPrisSlider setContinuous:YES];