I have an iOS 5 application that allows users to enter username with international keyboards.
I want to check if the input (NSString *) contains an emoji character or not.
Disabling emoji keyboard is not an option (Using UIKeyboardTypeASCIICapable as it disables some of the international keyboards).
I tried this. But it does not detect some of the characters like these.
Is there a good way to solve this problem?
I was able to detect all emojis in iOS 5 and iOS 6 emoji keyboards using following method
https://gist.github.com/4146056
Use https://github.com/woxtu/NSString-RemoveEmoji Category, but note that
iOS 9.1 added more emojis that above mentioned method doesnt recognize (especially these ones:🤐🤑🤒🤓🤔🤕🤖🤗🤘🦀🦁🦂🦃🦄🧀).
FIX: replace
return (0x1d000 <= codepoint && codepoint <= 0x1f77f); in isEmoji method with
return (0x1d000 <= codepoint && codepoint <= 0x1f77f) || (0x1F900 <= codepoint && codepoint <=0x1f9ff);
To iOS 10 and below, you should check if the unicode if between the following values.
if ((0x1d000 <= uc && uc <= 0x1f77f) || (0x1f900 <= 0x1f9ff)) {
//is emoji
}
NSData *data = [yourstringofemo dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *goodValue = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if ([goodValue rangeOfString:#"\u"].location == NSNotFound)
{
goodvalue string contains emoji characters
}
else
{
goodvalue string does not contain emoji characters
}
Related
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Remove HTML Tags from an NSString on the iPhone
I am using google direction api to display map in my mapview and it gives me HTML direction string ,that contain HTML tags.
Now i want to display that string in plain text how can i do that.My strings are here :
Head <b>southwest</b> toward <b>GH Rd</b>
Exit the roundabout onto <b>GH Rd</b><div style="font-size:0.9em">Go through 1 roundabout</div>
At the roundabout, take the <b>1st</b> exit onto <b>Road Number 2</b><div style="font-size:0.9em">Pass by myonlinesearch.blogspot.com (on the left in 600 m)</div>
At the roundabout, take the <b>3rd</b> exit onto <b>CH Rd</b>
At <b>Indroda Cir</b>, take the <b>2nd</b> exit onto <b>Gandhinagar Ahmedabad Rd/SH 71</b><div style="font-size:0.9em">Continue to follow Gandhinagar Ahmedabad Rd</div><div style="font-size:0.9em">Go through 1 roundabout</div>
At the roundabout, take the <b>1st</b> exit onto <b>Sardar Patel Ring Rd</b>
At <b>Ranasan Cir</b>, take the <b>3rd</b> exit onto <b>NH 8</b><div style="font-size:0.9em">Pass by Galaxy Restaurant (on the left in 4.3 km)</div>
Turn <b>left</b> onto <b>Galaxy Rd</b><div style="font-size:0.9em">Pass by Shiv Shakti Food Fort (on the left)</div>
Turn <b>left</b> onto <b>NH 59</b>
Turn <b>right</b><div style="font-size:0.9em">Go through 1 roundabout</div>
Turn <b>right</b>
Turn <b>left</b><div style="font-size:0.9em">Destination will be on the right</div>
You can use RegularExpression/Predicates to remove all characters between < & >.
But if your text contains some <> it will be removed
NSRange range;
NSString *string;
while ((range = [string rangeOfString:#"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound){
string=[string stringByReplacingCharactersInRange:range withString:#""];
}
NSLog(#"Un block string : %#",string);
You will have to parse the text via a parser it is very easy to parse these text just scan the line and use replace function there.
[yourString stringByReplacingOccurrencesOfString:#"<b>"withString:""];
[yourString stringByReplacingOccurrencesOfString:#"</b>"withString:""];
This will do the trick for you.
Either you can go with..
-(NSString *) stringByStrippingHTML
{
NSRange r;
NSString *s = [[self copy] autorelease];
while ((r = [s rangeOfString:#"]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
s = [s stringByReplacingCharactersInRange:r withString:#""];
return s;
}
Or better use NSString category to remove HTML from your string i.e. "GTMNSStringHTMLAdditions".
I'm making a word game and I've finally come up to one of the most important parts of my game, the compare part.
I got this label which will be invisible when it launches with the word that has to be guessed displaying in it with a random word generator. For example the word is: GARAGE
Now, for my game I have to compare the word with the input now I've already done this with the entire word with NSString but I want it to compare every letter. I want to be able to show that if the input has G as the first letter aswell, like garage. I want it to do something.
I want to know if this is possible and which methods you would use. I was thinking about making 6 strings since all my random words have 6 letters, and then break the word to 6 strings and the input aswell and then compare strings?
Hope someone has some usefull tips or example code thanks
So, assuming your string to be guessed...
NSString *stringToGuess = #"GARAGE";
and you were checking to see if it started with "GA"
NSString *myString = #"GA";
you would check it with hasPrefix:
if ([stringToGuess hasPrefix:myString]) {
// <stringToGuess> starts with <myString>
}
The documentation for NSString describes lots of neat methods for just about anything string related.
hasPrefix will let you tell if one string begins with another string. There's also characterAtIndex. You could use that to get one character from each string and compare it to the other.
You could write a method that would take an integer index and compare the two strings at that index:
- (BOOL) compareStringOne: (NSString *) stringOne
toStringTwo: (NSString *) stringTwo
atIndex: (NSUInteger) index;
{
if ([stringOne length] < index+1 || [stringTwo length] < index+1)
return FALSE;
return [stringOne characterAtIndex: index] == [stringTwo characterAtIndex: index];
}
I am trying to "nicely" display fractions in my iPhone application. Previously I have been using a tedious switch statement leading to hardcoded unicode characters for the vulgar fractions, but I have learnt about the unicode fraction slash character which, if I am understanding it correctly, should mean that I can create a string as follows:
[NSString stringWithFormat:#"%i\u2044%i",numerator,denominator];
And the "renderer" will automatically print it with a smaller, superscriped numerator and subscripted denominator. However, the above code just gives me the standard 1/2 appearance. I am using drawAtPoint to put the string on the screen. I have experimented with decomposedStringUsingCanonicalMapping and precomposedStringUsingCanonicalMapping but to be honest the documentation lost me.
Should this be working or does NSString drawing not cope with this?
I happened to only want simple fractions for recipes to be converted to Unicode vulgar fractions.
Here is how you can do it:
CGFloat quantityValue = 0.25f;
NSString *quantity = nil;
if (quantityValue == 0.25f) {
// 1/4
const unichar quarter = 0xbc;
quantity = [NSString stringWithCharacters:&quarter length:1];
} else if (quantityValue == 0.33f) {
// 1/3
const unichar third = 0x2153;
quantity = [NSString stringWithCharacters:&third length:1];
} else if (quantityValue == 0.5f) {
// 1/2
const unichar half = 0xbd;
quantity = [NSString stringWithCharacters:&half length:1];
} else if (quantityValue == 0.66f) {
// 2/3
const unichar twoThirds = 0x2154;
quantity = [NSString stringWithCharacters:&twoThirds length:1];
} else if (quantityValue == 0.75f) {
// 3/4
const unichar threeQuarters = 0xbe;
quantity = [NSString stringWithCharacters:&threeQuarters length:1];
}
NSLog(#"%#", quantity);
I'm not aware of any way for a unicode character to have the properties you describe. AFAIK the only thing that distinguishes U+2044 from a regular slash is it's a bit more angled and has little-to-no space on either side, therefore making it nestle up a lot closer to the surrounding numbers.
Here's a page on using the Fraction Slash in HTML, and as you can see it demonstrates that you simply get something like "1⁄10" if you try and use it on your own. It compensates for this by using the <sup> and <sub> tags in HTML on the surrounding numbers to get an appropriate display.
In order for you to get this to work in NSString you're going to have to figure out some way to apply superscripting and subscripting to the surrounding numbers yourself.
There are some included Unicode chars that do give actual fraction appearances, but they're limited to a 1/2, a 1/3 and a 1/4 I think.
If you want this for arbitrary fractions, seems to me like you need a custom view that draws the appropriate look; either through using positioned subviews or drawRect:.
I know this was a long time ago, but if it helps, there are superscript Unicode characters for all decimal numbers you can use to display arbitrary fractions in most fonts; see answers on a similar question here - https://stackoverflow.com/a/30860163/4522315
Edit:
As per comments, this solution depends on the font you're using. Amsi Pro (left) and other commercial fonts tend to include all the required symbols for superscripts, but the system font (right) does not.
I am trying to parse a string with a format like this:
*date1:
- band1.1 # venue1.1.
- band1.2 # venue1.2.
*date2:
- band 2.1 # venue2.1.
- band 2.2 # venue2.2.
etc
The number of dates and the number of bands and the associated venue can vary. I am using code based on the example at the bottom of this page.
I am using this snippet of code (I left out the bits at the bottom as they are irrelevant, but yes, I do close the loops etc.):
NSScanner *scanner1 = [NSScanner scannerWithString:contents];
NSCharacterSet *colon = [NSCharacterSet characterSetWithCharactersInString:#":"];
NSCharacterSet *at = [NSCharacterSet characterSetWithCharactersInString:#"#"];
NSCharacterSet *dot = [NSCharacterSet characterSetWithCharactersInString:#"."];
NSLog(#"scanner starting");
while ([scanner1 isAtEnd] == NO) {
if ([scanner1 scanString:#"*" intoString:NULL] && [scanner1 scanUpToCharactersFromSet:colon intoString:&tempDate] && [scanner1 scanString:#":" intoString:NULL])
{
NSLog(#"%#", tempDate);
if ([scanner1 scanString:#"-" intoString:NULL] && [scanner1 scanUpToCharactersFromSet:at intoString:&tempBands] && [scanner1 scanString:#"#" intoString:NULL] && [scanner1 scanUpToCharactersFromSet:dot intoString:&tempVenue]
&&[scanner1 scanString:#"." intoString:NULL])
{
NSLog(#"%# %#", tempBands, tempVenue);
}
}
} NSLog(#"ended scanning");
Currently, the first date is parsed and printed to the console, and the first venue and band of that date are printed. "ended scanning" is never printed. I have been battling this for hours and I am unsure of what to do now. I have a feeling that I do not understand the inner workings of NSScanner and there is probably a different way to tackle this problem. Maybe I need a second scanner?
After the first round of the while loop, the scanner's position is right after "venue1.". The next round of the loop starts with scanning an asterisk, which fails (returns NO) because the next (non-whitespace) character is a dash. Therefore, the body of the if statement isn't executed and the scanner's position is not advanced any further, resulting in an infinite loop.
I want to make the UDID of a iOS device to less than or equal to 15 characters.
I am porting a Symbian based client-server software to iOS. The server side uses IMEI (15 digits)to id a client phone. Now on iOS, UDID is too long(40 hex digits). As I want to minimize the change of server program or DB, I need to store the UDID in a varchar(15).
So it's there any way to make the UDID shorter but still unique enlough. It could be much better if I can also get the UDID from the shorted string.
You could convert the hex digits into 20 Latin-1 characters (≤0xff) or 10 Unicode BMP characters (≤0xffff).
If that varchar(15) can accept Unicode BMP characters, then we are done.
Otherwise, you could chop the last 5 characters from that 20 Latin-1 characters. The UDID is in fact a SHA-1 hash of some device-unique values, which can be considered quite random and the digits are uniformly distributed. Therefore, with 15 Latin-1 characters the reduced UDID should be able to represent 25615 ~ 1036 devices, which is much more than enough.
In fact, even if you just take 15 hex characters from the UDID it could already represent ~ 1018 devices.
Note that the last 2 methods are lossy, i.e. there is no way you could get the complete UDID from the 15 characters.
I just wrote this gist -> https://gist.github.com/3996097
What do you think about a CFUUID + sha1 + substring + random uppercase ?
//Get a random hash (Generated from CFUUID+sha1)
NSString *hash = [NSString sha1:[NSString getUUID]];
//Shorten the sha1
NSString *short_random_id = [hash substringFromIndex:[hash length]-10];
//Random uppercase / lowercase the id
NSMutableString *random_id_final = [NSMutableString string];
for (NSUInteger i = 0; i < [short_random_id length]; i++)
{
NSString *substring = [short_random_id substringWithRange:NSMakeRange(i, 1)];
[random_id_final appendString:(rand() % 2) ? [substring lowercaseString] : [substring uppercaseString]];
}