UILabel and right to left text - cocoa-touch

I have a collection of NSString objects that contain arabic text. However, when I try to display any of them using a UILabel, the text shows left-to-right instead of right-to-left (NSLog shows the strings properly)
I am thinking about a work-around, applying a transform to the UILabel to make a y-axis symmetry, but how can I detect if a NSString contains a RTL string?

Try prepending the unicode character 0x200F to the beginning of each string. This character is an invisible marker character that indicates text directionality.

Have you tried setting your region to Arabic? I would have thought this type of thing would be handled automatically.

Related

NSTextField: integerValue behaving inconsistently

I have found that [NSTextField integerValue] behaves differently with values containing thousands separators depending on how the value was set.
(I am in Germany, so my thousands separator in these examples is a point ".").
If I call [myTextField setIntegerValue:4567], the text field will contain 4.567 (with tousands separator), and [myTextField integerValue] returns 4567.
If I type the value 4.567 info the text field manually, or use [myTextField setStringValue:#"4.567"], then [myTextField integerValue] returns just 4.
Apparently it stops parsing the number at the thousands separator, even though it inserts such a separator itself when calling setIntegerValue.
So I have actually two questions:
Is there a setting or other easy way that I can prevent it to format the number when using -setIntegerValue: ?
Can I "enable" the number parsing to understand/accept thousands separators when calling -integerValue? Or, if not, what would be the simplest way to parse a number with thousands separator from an NSString?
Add a Number Formatter (NSNumberFormatter) to the text field in the storyboard or XIB. This makes the contents of the cell and objectValue of the text field a NSNumber instead of a NSString.
Is there a setting or other easy way that I can prevent it to format the number when using -setIntegerValue: ?
Switch off Grouping Separator of the formatter.
Can I "enable" the number parsing to understand/accept thousands separators when calling -integerValue?
Switch on Grouping Separator of the formatter and set Primary Grouping to 3.
Or, if not, what would be the simplest way to parse a number with thousands separator from an NSString?
Use a NSNumberFormatter, see the class reference and Number Formatters.
integerValue is not locale-aware, it will always use . as the decimal point.
You should use NSNumberFormatter.numberFromString: instead.
Link to NSNumberFormatter class reference.

how to insert extra glyphs?

I want to an UITextView to switch between two display modes.
In mode 1 it should show abbreviations and in the full word in mode 2. For example "Abbr." vs "abbreviation".
What would be the best way to do this? Keeping in mind that some words can have the same abbreviation and that the user is free to type either the full word or the abbreviation?
So far I tried to subclass NSLayoutManager.
Assuming I get an abbreviated string and I have to draw the full word, I would implement the following method:
-(void)setGlyphs:(const CGGlyph *)glyphs
properties:(const NSGlyphProperty *)props
characterIndexes:(const NSUInteger *)charIndexes
font:(UIFont *)aFont
forGlyphRange:(NSRange)glyphRange
{
NSUInteger length = glyphRange.length;
NSString *sourceString = #"a very long string as a source of characters for substitution"; //temp.
unichar *characters = malloc(sizeof(unichar) * length+4);
CGGlyph *subGlyphs = malloc(sizeof(CGGlyph) * length+4);
[sourceString getCharacters:characters
range:NSMakeRange(0, length+4)];
CTFontGetGlyphsForCharacters((__bridge CTFontRef)(aFont),
characters,
subGlyphs,
length+4);
[super setGlyphs:subGlyphs
properties:props
characterIndexes:charIndexes
font:aFont
forGlyphRange:NSMakeRange(glyphRange.location, length+4)];
}
However this method complains about invalid glyph indices "_NSGlyphTreeInsertGlyphs invalid char index" when I try to insert 4 additional glyphs.
You're barking way up the wrong tree; trying to subclass NSLayoutManager in this situation is overkill. Your problem is merely one of swapping text stretches (replace abbrev by original or original by abbrev), so just do that - in the text, the underlying NSMutableAttributedString being displayed.
You say in a comment "some words map to the same abbreviation". No problem. Assuming you know the original word (the problem would not be solvable if you did not), store that original word as part of the NSMutableAttributedString, i.e. as an attribute in the place where the word is. Thus, when you substitute the abbreviation, the attribute remains, and thus the original word is retained, ready for you when you need to switch it back.
For example, given this string: #"I love New York" You can hide the word "New York" as an attribute in the same stretch of text occupied by "New York":
[attributedString addAttribute:#"realword" value:#"New York" range:NSMakeRange(7,8)];
Now you can set that range's text to #"NY" but the attribute remains, and you can consult it when the time comes to switch the text back to the unabbreviated form.
(I have drawn out this answer at some length because many people are unaware that you are allowed to define your own arbitrary NSAttributedString attributes. It's an incredibly useful thing to do.)

DBLCDTempBlack only for some letters in UILabel

I am uising this code for marquee effect. i want digital clock like font so i gave font as
UIFont *lcdFont = [UIFont fontWithName:#"DBLCDTempBlack" size:60.0];
self.font = lcdFont ;
But i am getting this font only for some random characters.I am using only uppercase letters. I want i for all the characters in UILabel.
Why is it happening so.
It's nothing wrong with your code -- that font only has certain characters in it. (Why? Who knows.) For those it's missing, the OS automatically falls back to another font.
If you want to draw LCD-looking text using a broader character set, you'll need to find such a font elsewhere and bundle it in your app.

Truncating NSString to fit a particular width

I've got a variable-width-font NSString that has to fit inside a fixed size UIView. Currently, the string gets truncated and rendered.
I want to obtain the visible substring so I can append an elipsis (…) to it.
If you're using a UILabel, you can set the lineBreakMode to one of
UILineBreakModeHeadTruncation
UILineBreakModeTailTruncation
UILineBreakModeMiddleTruncation
The different positions refer to where the ... goes. You want UILineBreakModeTailTruncation.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UILabel_Class/Reference/UILabel.html#//apple_ref/occ/instp/UILabel/lineBreakMode

diffrent between NSLog and -print description- with NSString and UTF-8

I'm confused. I'm parsing json string.
Before parsing, I check what is the content of the NSString.
In Xode4:
When I click on the NSString variable "print description"
The console show the value as \u434 \u433 format of the UTF-8
When I call NSLog("%#",content) the console show the "readble" character of the UTF-8 encoding.
Why is this different? How can I know that the string I got to parse is 100% UTF-8 ?
Thanks.
If you can see the Cyrillic characters you're looking for, rather than the escapes, through any method, then you're working with a UTF-8 string.
The "-description" method is not what you want to use here. It's more likely to show escaped characters; in particular, any time you store a value in a property list item like an NSArray or NSDictionary, its -description will generally escape any characters other than plain ASCII.
NSLog is a more reliable guide, because it doesn't use -description. If it's showing up in NSLog, it's probably just fine.
If you want to be absolutely sure your string is properly encoded UTF-8, the best way to test it is to display it. Create a text interface element (an NSTextField or UITextField) in your user interface, wire it up, and set your string as the value. If it displays there, it is properly formatted.
Short version: if it shows up in the debugger as escaped characters, it doesn't necessarily mean it's not UTF8. If it's showing up anywhere (including NSLog) with the proper characters, it's probably in the proper encoding. If you want to be sure, set up a test interface element and see how it looks there.