I'm trying to full justify and allow for hyphenation in an NSAttributedString. Right now I set the paragraph style using the code:
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = NSTextAlignmentJustified;
paragraph.hyphenationFactor = 0.5;
paragraph.lineBreakMode = NSLineBreakByWordWrapping;
This doesn't hyphenate, it just splits words based on how much they fit. I've also tried NSLineBreakByCharWrapping which ends up being even worse. Any ideas? I'm using the iOS 6 libraries.
Thanks,
Pete
Recently found this out:
There is a bug in the library, in that any value BETWEEN 0.0 and 1.0 does not work. That being said, the values 0.0 and 1.0 DO work. So to get hyphenation, set the hyphenation factor to 1.0 as of iOS6.
Related
Seems like there is a bug with NSNumberFormatter on iOS14 when your region is set to Cambodia where the amount is being formatted to "1.234,56" which is not the format being used there.
It was okay on iOS13. I cannot see any references in regards to this change.
Is there any workaround for this?
UPDATE:
Issue still exists with iOS 14.2
I think you can modify the parameters for NumberFormatter to suit your needs. Something like this
let formatter = NumberFormatter()
//formatter.locale = Locale.init(identifier: Locale.current.identifier)
formatter.numberStyle = .currency
formatter.currencyGroupingSeparator = ","
formatter.currencyDecimalSeparator = "."
//by default its 3
formatter.maximumFractionDigits = 2
print(formatter.string(from: 1000000.00))
//Outputs Optional("TND 1,000,000.00")
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.
I have a request from a customer to use a certain font in a iOS 7 project because it has medieval numbers.
Is there any way to activate those numbers for a NSAttributedString? As default lining numbers are used, that are included in the font as-well.
Here is an example. Both lines have the same font with no variant (Regular), once with medieval numbers activated, the second wit the default lining numbers.
These are called lowercase numbers and can be turned on using UIFontDescriptor.
First, you need to import CoreText for some constants:
#import <CoreText/SFNTLayoutTypes.h>
or
#import CoreText.SFNTLayoutTypes;
Then create font using font descriptor. Here I use Georgia family:
NSDictionary *lowercaseNumbers = #{
UIFontFeatureTypeIdentifierKey: #(kNumberCaseType),
UIFontFeatureSelectorIdentifierKey: #(kLowerCaseNumbersSelector),
};
UIFontDescriptor *descriptor = [[UIFontDescriptor alloc] initWithFontAttributes:
#{
UIFontDescriptorFamilyAttribute: #"Georgia",
UIFontDescriptorFeatureSettingsAttribute:#[ lowercaseNumbers ],
}];
UIFont *font = [UIFont fontWithDescriptor:descriptor size:15];
Result:
Edit: As #Random832 pointed out, Georgia has only lowercase numbers, so the result is irrelevant. However, #vikingosegundo confirmed this code works on supported fonts. Thanks.
The top line was generated with
UIFont *font = [UIFont fontWithName:#"DIN Next LT Pro" size:12];
if (font)
label.font = font;
the second line with
NSDictionary *lowercaseNumbers = #{ UIFontFeatureTypeIdentifierKey:#(kNumberCaseType), UIFontFeatureSelectorIdentifierKey: #(kLowerCaseNumbersSelector)};
UIFontDescriptor *descriptor = [[UIFontDescriptor alloc] initWithFontAttributes:
#{UIFontDescriptorFamilyAttribute: #"DIN Next LT Pro",UIFontDescriptorFeatureSettingsAttribute:#[ lowercaseNumbers ]}];
UIFont *font = [UIFont fontWithDescriptor:descriptor size:12];
if (font)
label.font = font;
Another question has a pointer in the right direction, though the question mentions tabular figures [vs proportional] rather than text vs lining.
It looks like you can use CTFontDescriptorCreateCopyWithFeature with kNumberCaseType set to kLowerCaseNumbersSelector to display the digits this way.
Here's another related question, and here's the blog post provided in the answer.
I'm trying to change the kerning on a couple of SKLabelNodes. I tried to use some code from another answer:
NSMutableAttributedString *attributedString;
attributedString = [[NSMutableAttributedString alloc] initWithString:#"Please get wider"];
[attributedString addAttribute:NSKernAttributeName value:#5 range:NSMakeRange(10, 5)];
[self.label setAttributedText:attributedString];
This isn't allowed:
[myLabelNode setAttributedText:attributedString];
And this doesn't carry over the changes I made:
myLabelNode.text = attributedString.string;
Is it possible to change kerning on an SKLabelNode?
As of iOS 11, NSAttributedStrings are supported in SKLabelNode. So kerning and other options should now be available to you.
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).