According to this I can set the indentation by using setFirstLineHeadIndent and setHeadIndent.
However, looking at this and this I don't see those functions referenced. Instead I see an attributes of such names.
I'm currently using following code:
double mm2pts = (double) 72 / 25.4;
paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setHeadIndent: 100 * mm2pts / 10.0];
[paragraphStyle setFirstLineHeadIndent: 100 * mm2pts / 10.0];
[m_textView setDefaultParagraphStyle:paragraphStyle];
I'm testing on OSX 10.8 with my program compiled for the 10.7 minimum.
My question is: will my code compile/run on latest OSX with latest OSX SDK?
Thank you.
[EDIT]
It looks like the code above does not work, while the following code does:
NSRange range = NSMakeRange(start, end-start);
NSTextStorage* storage = [m_textView textStorage];
double mm2pts = (double) 72 / 25.4;
paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setFirstLineHeadIndent: 100 * mm2pts / 10.0];
[paragraphStyle setHeadIndent: 100 * mm2pts / 10.0];
[storage addAttribute: NSParagraphStyleAttributeName value: paragraphStyle range: range];
[paragraphStyle release];
the idea is to use former for the whole control while the latter for the selected range. Does this mean I should apply the first piece of code to the range of (0, size_of_text)?
[/EDIT]
I see firstLineHeadIndent and headIndent listed as properties on NSMutableParagraphStyle, just as expected. Since they are not read-only properties, they can be set as well as read, and thus corresponding methods starting with "set" exist, following the language specification for properties. So the answer to your question would appear to be "yes". More to the point, though, it sounds like you need to read up on how properties work in Objective-C.
Related
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)];
I'm wondering, is there a way to do half of a new line character (\n) in an NSString in objective C, i.e. so that it only skips about half the space? Or anyway else to accomplish this in an NSString?
Like Wain said, setting NSParagraphStyle on an NSAttributedString might be what you are looking for. UILabel supports NSAttributedStrings in iOS 6, but anything before that you will have to use a third party component. TTTAttributedLabel is very good and well documented.
NSMutableAttributedString* attrStr = [NSMutableAttributedString attributedStringWithString:#"Hello World!"];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setLineSpacing:24]; //Just a random value, you'll have to play with it till you are hhappy
[attrStr addAttribute:NSParagraphStyleAttributeName
value:style
range:NSMakeRange(0, [myString length])];
label.attributedText = attrStr;
if you end up using TTTAttributedLabel you would use label.text = attrStr; or one of the helper methods (Taken from TTTAttributedLabel docs:
[label setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
NSRange boldRange = [[mutableAttributedString string] rangeOfString:#"ipsum dolar" options:NSCaseInsensitiveSearch];
NSRange strikeRange = [[mutableAttributedString string] rangeOfString:#"sit amet" options:NSCaseInsensitiveSearch];
// Core Text APIs use C functions without a direct bridge to UIFont. See Apple's "Core Text Programming Guide" to learn how to configure string attributes.
UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:14];
CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
if (font) {
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)font range:boldRange];
[mutableAttributedString addAttribute:kTTTStrikeOutAttributeName value:[NSNumber numberWithBool:YES] range:strikeRange];
CFRelease(font);
}
return mutableAttributedString;
}];
Also, TTTAttributedLabel has a lineHeightMultiple (between 0.0 and 1.0) property that you might be able to fiddle with to get the desired effect. That way, you'll still be able to use an NSString and not mess with the sometimes ugly NSAttributedString.
While this answer arguably won't help the asker, some historical perspective on the half-linefeed concept may be of general use.
The concept of half-reverse and half-forward linefeeds was useful on teletypes to combine, say, the o and " characters to approximate the German "o umlaut", ö. The Unix col command recognized ESC-8 (0x1b 0x38) and ESC-9 (0x1b 0x39) as these sequences, respectively, and this standard was adopted by asciitiff.
The printer control language PCL recognized the forward half-linefeed.
An OKI PCL4.5 printer, the OL600e, accepted ESC= (0x1b 0x3d) as half-linefeed, and PCL5 extended that to ESC&= (0x1b 0x26 0x3d).
I'm having difficulty locating older examples.
I wish to use NSNumberFormatter to merely attached a percent ('%') to the supplied number WITHOUT having it multiplied by 100.
The canned kCFNumberFormatterPercentStyle automatically x100 which I don't want.
For example, converting 5.0 to 5.0% versus 500%.
Using the following:
NSNumberFormatter *percentFormatter = [[NSNumberFormatter alloc] init];
[percentFormatter setNumberFormat:#"##0.00%;-##0.00%"];
But 'setNumberFormat' doesn't exist in NSNumberFomatter.
I need to use this NSNumberFormatter for my Core-Plot label.
How can I customize NSNumberFormat?
Ric.
Source: Apple's NSDecimalNumber reference.
Apparently I hinted the answer by saying that I didn't want the output to be 100x.
I'm working with a NSDecimalNumber which has the 'setMultiplier' method.
So, after I used the canned kCFNumberFormatterPercentStyle for the formatter, I used 'setMultiplier:1' as follows:
NSNumberFormatter *percentFormatter = [[NSNumberFormatter alloc] init];
[percentFormatter setNumberStyle:kCFNumberFormatterPercentStyle];
[percentFormatter setMultiplier:[NSNumber numberWithInt:1]];
Have you tried using setMultiplier to prevent it from multiplying by 100?
NSNumberFormatter *percentFormatter = [[NSNumberFormatter alloc] init];
[percentFormatter setNumberStyle: NSNumberFormatterPercentStyle];
[percentFormatter setMultiplier:1];
If adding the percent sign is all you need to accomplish, an alternative using NSNumberFormatterwould be:
[NSString stringWithFormat:#"%3.2f%%", [myNumber doubleValue]];
And you should adjust the precision specifier (3.2) to suit the number of digits you want to display.
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 ;)
Is there a way in NSString to output the st, nd, and rd but in a superscripted format? Any known unicode perhaps?
There doesn't seem to be any Unicode characters for this, but it's easy enough to make an NSAttributedString that will do the trick:
NSDictionary * superscriptAttrs = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:1]
forKey:NSSuperscriptAttributeName];
NSAttributedString * st = [[NSAttributedString alloc] initWithString:#"st"
attributes:superscriptAttrs];
NSMutableAttributedString * premiere = [[NSMutableAttributedString alloc] initWithString:#"1"];
[premiere appendAttributedString:st];
// Don't forget to release everything when you're done with it!
You might also want to change the font size of the superscript. This is accomplished by including the NSFontAttributeName in the attributes dictionary with an appropriate font object. Note that NSAttributedString is only available on the iPhone in iOS 4.0+, and on the iPad in 3.2+ (see comment).