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.
Related
I have a label which shows an expression:
(x+y)
But I want to show it in label like this:
(x+y)^2
(But with degree, I can't do it here, because I have too low reputation to insert images)
So, I want to show expression's degree in UIlabel.
Is it possible with single UILabel?
You can use Unicode characters of superscript two \u00B2, it it's always \u followed by the character code.
NSString *equation = [NSString stringWithFormat:#"(x+y)%#", #"\u00B2"];
Swift:
var equation = NSString(format:"(x+y)%#", "\u{00B2}") as String
Result:
http://unicode-table.com/en/
Strings and Characters (Apple iOS Developer Library )
Strings in Swift
I think you are looking for powers e.g. (x + y)⁹.
For this, You have to use unicodes.
you can take list of unicodes character list from here;
http://www.fileformat.info/info/unicode/category/No/list.htm
In code, you will use;
print("(x+y)\u{00B2}");
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.
I am using a UITextField for users to enter usernames, and would like to restrict special characters except for periods and underscores. I was initially set on using the solution from this SO question, until I realized that I do not want to restrict to only alpha-numeric characters, but also allow Asian and Middle Eastern languages characters as well. Is there a way that I would be able to accomplish this?
Thanks!
Update:
Per rmaddy's suggestion, here is what I am presently using:
- (BOOL)userNameIsAcceptable: (NSString *)userNameInputted
{
NSCharacterSet *userNameAcceptedInput = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
NSString *filteredUserName = [[userNameInputted componentsSeparatedByCharactersInSet:userNameAcceptedInput] componentsJoinedByString:#""];
NSLog(#"The filtered result %#", filteredUserName);
return [userNameAcceptedInput isEqual:filteredUserName];
}
Use the solution from the other question but instead of building the character set from the fixed letters, use the standard NSCharacterSet alphanumericCharacterSet.
Maybe testing unicode chars from strings manually in loop against ranges containing unicode sets you need?
I did quick check, and it seems that unicode sets of Japanese letters are usually densely packed, except some special characters - I've looked here http://www.rikai.com/library/kanjitables/kanji_codes.unicode.shtml but I guess similar should be valid to other languages as well.
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
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.