CCLabelBMFont loses tailing characters for each newline characters - character-encoding

creating a label this style :
CCLabelBMFont *label1_=
[CCLabelBMFont labelWithString:#"description: -" fntFile:#"comicsans.fnt" width:270 alignment:kCCTextAlignmentLeft];
and:
[label1_ setString:
#"someText\n and some newline \nand another new line too but this is last"];
this string have 2 escape characters for new line as seen.and when I set this Im losing last 2 words
its shown something like this
someText
and some newline
and another new line too but this is la
so last two letters lost somehow.
what could be a reason for this problem ?
a cocos2d v2.1(stable) bug or Im in a horror film ?if so what should I do ?
\r does same effect as \n
dont know why. may be you know.
if I dont use \r \n escape characters;CCLabelFont String shows correct string.without losing any amount of characters tailing.
so my temporal solution is removing escape characters from string fix problem.
but this not fixes bug for cocos2d v2.1 (stable).
I think CCLabel kind of classes cannot calculate doesnt work stable if there is \n escape characters.

I had the same problem since I was using CCLabelBMFont to animate text typing.
I realized that whenever the text to type has newlines \n, CCLabelBMFont will not type the trailing characters.
I resolved this issue through a simple hack.
First I count the number of newlines in the text to be displayed by CCLabelBMFont.
NSRegularExpression *regx = [NSRegularExpression regularExpressionWithPattern:#"\n"
options:0
error:nil];
NSUInteger newlinesCount = [regx numberOfMatchesInString:typeString
options:0
range:NSMakeRange(0, typeString.length)];
Then I just append some white spaces at the end of the string that I'm about to type. The number of white spaces to add equals to the number of newlines.
for (int i = 0; i < newlinesCount; i++) {
typeString = [typeString stringByAppendingString:#" "];
}
// This sets the string for the BMFont, it should now display all the characters
// that you wanted to type originally.
[self.labelBMFont setString:typeString];
Tested on cocos2d 2.1

Related

IOS NSCharacterSet for emoji and latin characters

I'm trying to limit text user input to latin/english characters and emojis.
Is it possible to create an NSCharacterSet that includes all of these characters?
I tried using a keyboard type ASCIICapable on my input views, but then I don't get emoji input.
There's nothing built in to create such a specific character set. You'll have to do it yourself by character range.
The Emoji characters are essentially in the range \U1F300 - \U1F6FF. I suppose a few others are scattered about.
Use an NSMutableCharacterSet to build up what you need.
NSMutableCharacterSet *aCharacterSet = [[NSMutableCharacterSet alloc] init];
[aCharacterSet addCharactersInRange:NSMakeRange(0x1F300, 0x1F700 - 0x1F300)]; // Add most of the Emoji characters
[aCharacterSet addCharactersInRange:NSMakeRange('A', 'Z'-'A'+1)]; // Add uppercase
[aCharacterSet addCharactersInRange:NSMakeRange('a', 'z'-'a'+1)]; // Add lowercase
Swift equivalent of rmaddy's answer to add up ranges in a CharacterSet:
var aCharacterSet = CharacterSet()
aCharacterSet.insert(charactersIn: "\u{1F300}"..<"\u{1F700}") // Add most of the Emoji characters
aCharacterSet.insert(charactersIn: "A"..."Z") // Add uppercase
aCharacterSet.insert(charactersIn: "a"..."z") // Add lowercase
Also, you will find the complete list of Unicode ranges for emoji on http://www.unicode.org/charts/ under Emoji & Pictographs.

iOS - How can I put special characters (music notation) in a UILabel?

I would like to create a label with some unicode text and a music note. The notes are shown below:
I have tried:
titleLabel.text = #" title + ♫";
but that results in:
I must be doing something dumb.. Any advice welcome.
The number column in your table actually contains HTML/SGML/XML entities with decimal values. A unicode escape sequence in NSString takes the hexadecimal value, so your note ♫ would be the hex value 0x266b to be used like this
titleLabel.text = #" title \u266b";
Hit cmd+cntrl+space in Xcode, and search for 'note'. There are some u may use. Just double click one and it will be written where your cursor is in the code.

How to put line breaks on strings Objective-C

If I have a string in Objective-C called text that is a really long, such as:
NSString *text = [NSString stringWithFormat:#"%#", _sometext];
How can I scan _sometext and add a line break to the string every hundred characters (letters)?
So if I had the _sometext as
It was November. Although it was not yet late, the sky was dark when I
turned into Laundress Passage. Father had finished for the day,
switched off the shop lights and closed the shutters; but so I would
not come home to darkness he had left on the light over the stairs to
the flat.
How can I make it so it puts a line break after
It was November. Although it was not yet late, the sky was dark when I
turned into Laundress Passage
and after
. Father had finished for the day, switched off the shop lights and
closed the shutters; but so I wo
(since those were 100 characters)?
But instead of stopping in the middle of the word, I could it would skip the word and end at the previous word. Example: if the sentence ended at "but so I wo" and it cut of the word "would", it would stop at this instead of "but so I".
The easiest solution I can think of off the top of my head is a two step process.
Step one involves breaking the original string into an array of 100 character strings.
Step two involves joining that array of strings with the newline character.
NSMutableArray *lines = [NSMutableArray array];
while ([originalString length] > 100) {
[lines addObject:[originalString substringToIndex:100]];
originalString = [originalString substringFromIndex:100];
}
[lines addObject: originalString];
NSString *reformattedString = [lines componentsJoinedByString:#"\n"];
You could write code that would use the method rangeOfString:options:range:
to do that.
You'd create an NSRange for the first 100 characters of your string. Then you'd search backwards in that range for a space. When you found a space, you'd add from the beginning of the string to the space to your output string, plus a line break. Then you'd set your search range to the next 100 characters of your string after the space, and again search backwards for a space. Repeat until you've processed the entire string.
See the NSString class reference for the details on the rangeOfString:options:range: method.

Understanding the Use of invertedSet method of NSCharacterSet

So as I work my way through understanding string methods, I came across this useful class
NSCharacterSet
which is defined in this post quite well as being similar to a string excpet it is used for holding the char in an unordered set
What is differnce between NSString and NSCharacterset?
So then I came across the useful method invertedSet, and it bacame a little less clear what was happening exactly. Also I a read page a fter page on it, they all sort of glossed over the basics of what was happening and jumped into advanced explainations. So if you wanted to know what this is and why we use It SIMPLY put, it was not so easy instead you get statements like this from the apple documentation: "A character set containing only characters that don’t exist in the receiver." - and how do I use this exactly???
So here is what i understand to be the use. PLEASE provide in simple terms if I have explained this incorrectly.
Example Use:
Create a list of Characters in a NSCharacterSetyou want to limit a string to contain.
NSString *validNumberChars = #"0123456789"; //Only these are valid.
//Now assign to a NSCharacter object to use for searching and comparing later
validCharSet = [NSCharacterSet characterSetWithCharactersInString:validNumberChars ];
//Now create an inverteds set OF the validCharSet.
NSCharacterSet *invertedValidCharSet = [validCharSet invertedSet];
//Now scrub your input string of bad character, those characters not in the validCharSet
NSString *scrubbedString = [inputString stringByTrimmingCharactersInSet:invertedValidCharSet];
//By passing in the inverted invertedValidCharSet as the characters to trim out, then you are left with only characters that are in the original set. captured here in scrubbedString.
So is this how to use this feature properly, or did I miss anything?
Thanks
Steve
A character set is a just that - a set of characters. When you invert a character set you get a new set that has every character except those from the original set.
In your example you start with a character set containing the 10 standard digits. When you invert the set you get a set that has every character except the 10 digits.
validCharSet = [NSCharacterSet characterSetWithCharactersInString:validNumberChars];
This creates a character set containing the 10 characters 0, 1, ..., 9.
invertedValidCharSet = [validCharSet invertedSet];
This creates the inverted character set, i.e. the set of all Unicode characters without
the 10 characters from above.
scrubbedString = [inputString stringByTrimmingCharactersInSet:invertedValidCharSet];
This removes from the start and end of inputString all characters that are in
the invertedValidCharSet. For example, if
inputString = #"abc123d€f567ghj😄"
then
scrubbedString = #"123d€f567"
Is does not, as you perhaps expect, remove all characters from the given set.
One way to achieve that is (copied from NSString - replacing characters from NSCharacterSet):
scrubbedString = [[inputString componentsSeparatedByCharactersInSet:invertedValidCharSet] componentsJoinedByString:#""]
This is probably not the most effective method, but as your question was about understanding
NSCharacterSet I hope that it helps.

Determining ascii character in NSString

In one of my UIView classes, I have the UIKeyInput protocol attached to gather input from a UIKeyboard. I'm trying to figure out what ascii character is being used when the space button is pushed (it's not simply ' ' it's something else it appears). Does anyone know what this asci character is or how I can figure out what ascii code is being used?
To look at the value for each character you can do something like this:
NSString *text = ... // the text to examine
for (NSUInteger c = 0; c < text.length; c++) {
unichar char = [text characterAtIndex:c];
NSLog(#"char = %x", (int)char); // Log the hex value of the Unicode character
}
Please note that this code doesn't properly handle any Unicode characters in the range \U10000 and up. This includes many (all?) of the Emoji characters.
If you really need to know what character (or code point) it actually is, use the CFMutableString function CFStringTransform()
That enables you to use transformation argument kCFStringTransformToUnicodeName to get the human readable Unicode name for example or Hex-Any to get escaped Unicode code point.
Otherwise you can do the the unichar approach to simple get the code point.

Resources