I have NSString strings that represent times in HH:MM:SS but the HH and a digit of the MM may be omitted.
I need to al开发者_C百科ign columns of these values like this:
1:01:43
43:23
7:00
What's the easiest way to do this?
Make use of the stringWithFormat method provided in the NSString class. Something like this:
NSString * dateStr = @"05:30";
NSString * formattedStr = [NSString stringWithFormat:@"%8s", [str UTF8String]];
The 8 in %8s is the number of precision digits (or in this case characters) that you want in your formatted string. Input strings shorter than the specified precision will get left padded with spaces, strings longer than it will get truncated. You can read more about format strings for printf here.
Edit Thanks to @Peter for pointing this out. There are some (minor) differences between the format specifiers in standard C, vs Objective C. Refer to Apples documentation on format string specifiers here.
padding code Different one but might be helpful
NSString *st = [@"abc" stringByPaddingToLength: 9 withString: @"." startingAtIndex:0];
abc……
[@"abc" stringByPaddingToLength: 2 withString: @"." startingAtIndex:0];
ab
[@"abc" stringByPaddingToLength: 9 withString: @". " startingAtIndex:1];
abc . . .
notice that the first character in the padding is a space
I don't think there is an easy way. You can probably write a category method on NSString
to do that.
- (NSString *)stringByPaddingLeftToLength:(NSUInteger)aLength withString:(NSString *)padString {
NSInteger lengthToPad = aLength - [self length];
if ( lengthToPad > 0 ) {
NSString * padding = [@"" stringByPaddingToLength:lengthToPad withString:padString startingAtIndex:0];
NSString * paddedString = [padding stringByAppendingString:self];
return paddedString;
} else {
return [[self copy] autorelease];
}
}
If you mean a fixed point width, both NSTextField (Cocoa) and UILabel (Cocoa Touch) let you set the alignment. For NSTextView and custom drawing, you'll need to make an NSParagraphStyle (AppKit only) or CTParagraphStyle with the alignment specified.
If you want this to have explicit alignment with a proportional font (ie, not a monospace font), then you should use different views for each of the components of the string.
[hour]-[:]-[minute]-[:]-[second]
[hour]-[:]-[minute]-[:]-[second]
[hour]-[:]-[minute]-[:]-[second]
Where each one of those [things]
is a different UILabel
This solution is valid for iOS:
in iOS you can use UILabel and right align them. Of course you will have a perfect alignment only if you use a "fixed font", like Courier New for example. If you must use a proportional font, the only way is to use the NSString+UIKit extension to draw all characters of the string at fixed positions inside the view drawing context: So ideally the flow is: 1. calculate the length of the longest string 2. left-pad all other strings by adding extra space 3. then for each string iterate on each character and draw it as a specific position (don't copy and paste this code, just use it to have an idea; this is obj-c-like pseudo-code)
CGFloat x=0; // the string start let's say at x=0
CGFloat dx=40; // distance between characters
for(NSInteger i=0;i<[myString length];i++) {
NSString *char = [myString substringWithRange:NSRangeMake(i,1)];
[char drawAtPoint:CGPointMake(x,y) withFont:myFont];
x+=dx;
}
Of course dx must be chosen according to the font size.
精彩评论