Get surrogate pairs from an emoji - ios

What I'm trying to do is check if an emoji can be rendered on the iOS device by using this:
let font = CTFontCreateWithName("AppleColorEmoji", 12, nil)
var code_point: [UniChar] = [0xD83D, 0xDE0D]
var glyphs: [CGGlyph] = [0, 0]
let has_glyph = CTFontGetGlyphsForCharacters(font, &code_point, &glyphs, 2)
if has_glyph == false {
return false
}
else {
return true
}
It takes two code points and checks if the emoji can be rendered. Now what I'm having trouble with is how do I get the surrogate pairs directly from an emoji. I've Googled around and I can't seem to find any way to do so. Any ideas?

What you are looking for is the UTF-16 representation of a character:
let emoji = "😍"
let utf16codepoints = Array(emoji.utf16)
utf16codepoints is an [UInt16] array, and UniChar is a type alias for UInt16, so this array can be used directly in CTFontGetGlyphsForCharacters() to check if a font has a glyph
for this character (now updated for Swift 3/4):
let font = CTFontCreateWithName("AppleColorEmoji" as CFString, 12, nil)
var glyphs: [CGGlyph] = [0, 0]
let has_glyph = CTFontGetGlyphsForCharacters(font, utf16codepoints, &glyphs, utf16codepoints.count)
print(has_glyph)
// true
Hex dump the array to verify that it is the same as the
code_point array in your question:
print(utf16codepoints.map { String($0, radix: 16)} )
// ["d83d", "de0d"]
print(utf16codepoints == [0xD83D, 0xDE0D])
// true

In case somebody is looking for Obj-C implementation:
- (BOOL)isEmojiSupported:(NSString*)emoji
{
NSUInteger length = [emoji length];
unichar characters[length + 1];
[emoji getCharacters:characters range:NSMakeRange(0, length)];
characters[length] = 0x0;
CGGlyph glyphs[length];
CTFontRef ctFont = CTFontCreateWithName(CFSTR("AppleColorEmoji"), 12, NULL);
BOOL ret = CTFontGetGlyphsForCharacters(ctFont, characters, glyphs, emoji.length);
CFRelease(ctFont);
return ret;
}

Related

Attributed String

a text that has some part with yellow backgroundI need some padding to left and right of yellow colored text and need the label "3" to be center..I am using attributed text for it.can anyone help me on this.Below is the code i am using.Attaching screenshot as well.
string CardTileText = "3 Shared Documents";
NSAttributedString decoratedText = CardTileText.GetAttributedStringFromHtml("#ffe601","3");
public static NSAttributedString GetAttributedStringFromHtml(this string source, UIColor color, string identifier)
{
var atts = new UIStringAttributes();
UIFont newConnFont = UIFont.FromName("NotoSans-Bold", 16);
NSRange range = GetRangeFor(source, identifier);
NSNumber offset = 5;
NSMutableParagraphStyle para = new NSMutableParagraphStyle
{
Alignment = UITextAlignment.Left
};
NSMutableAttributedString attributedString = new NSMutableAttributedString(source, atts);
attributedString.AddAttribute(UIStringAttributeKey.BackgroundColor, color, range);
attributedString.AddAttribute(UIStringAttributeKey.ForegroundColor, UIColor.Black, range);
attributedString.AddAttribute(UIStringAttributeKey.Font, newConnFont, range);
attributedString.AddAttribute(UIStringAttributeKey.KerningAdjustment, offset, range);
attributedString.AddAttribute(UIStringAttributeKey.ParagraphStyle, para, range);
return attributedString;
}
static NSRange GetRangeFor(string source, string substring)
{
var range = new NSRange
{
Location = source.IndexOf(substring, StringComparison.Ordinal),
Length = substring.Length
};
return range;
}
I'm afraid the only solution is that set offset(which is used on KerningAdjustment) with a proper value.
Here is the comparison
offset = 0.5
offset = 10

encoding NSURL in ISO-8859-1

I have a ViewController containing TextFields and I would need to send those values to a dedicated HTTP service.
My main concern comes from the encoding type, as this app is in French and may contain some accents ('é', 'è', etc,...) but also I need to format correctly my fields as it may contain spaces as well....
I tried to use different ways but I still have a wrong encoding on the server side.
here is a sample of my code:
let url_to_request = "http://11.22.33.44:8080/SRV/addRepertoire"
var params = "owner=\(User.sharedInstance.email)&adresse=\(adresse.text!)&nom=\(nom.text!)&telephone=\(telephone.text!)&commentaires=\(commentaires.text!)"
//trying to encode in ISO-8859-1
let dt = NSString(CString: params, encoding: NSISOLatin1StringEncoding)
//preparing string to be used in a NSURL
let final_url = dt!.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
print("URL loadRepertoire: \(url_to_request+"?"+final_url!)")
for instance, field "nom" contains "bébé" which is encoded "b%C4%82%C5%A0b%C4%82%C5%A0" whereas my server is expecting "b%E9b%E9"
EDIT2:
I tried to use the following:
let url_to_request = "http://11.22.33.44:8080/SRV/addRepertoire"
let params = "owner=\(User.sharedInstance.email)&adresse=\(adresse.text!)&nom=\(nom.text!)&telephone=\(telephone.text!)&commentaires=\(commentaires.text!)"
let tmp_url = url_to_request + "?" + params
let final_url = tmp_url.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
print("URL addRepertoire: \(final_url)")
but the result remains:
b%C3%83%C2%A9b%C3%83%C2%A9, diplayed bébé instead of bébé
stringByAddingPercentEncodingWithAllowedCharacters always uses UTF-8 representation, so I'm afraid you may need to do it yourself.
extension String {
func stringByAddingPercentEncodingForISOLatin1() -> String? {
let allowedCharacterSet = NSCharacterSet(charactersInString:
"0123456789"
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz"
+ "_-.~"
+ "=&" //You'd better remove this and encode each param.
)
if let data = self.dataUsingEncoding(NSISOLatin1StringEncoding) {
var result = ""
for i in 0..<data.length {
let ch = UnsafePointer<UInt8>(data.bytes)[i]
if ch >= 0x80 || !allowedCharacterSet.characterIsMember(unichar(ch)) {
result += String(format: "%%%02X", ch)
} else {
result.append(UnicodeScalar(ch))
}
}
return result
} else {
return nil
}
}
}
"bébé".stringByAddingPercentEncodingForISOLatin1()! //->"b%E9b%E9"
Here is a solution in Swift 4:
extension String {
// Url percent encoding according to RFC3986 specifications
// https://tools.ietf.org/html/rfc3986#section-2.1
func urlPercentEncoded(withAllowedCharacters allowedCharacters:
CharacterSet, encoding: String.Encoding) -> String {
var returnStr = ""
// Compute each char seperatly
for char in self {
let charStr = String(char)
let charScalar = charStr.unicodeScalars[charStr.unicodeScalars.startIndex]
if allowedCharacters.contains(charScalar) == false,
let bytesOfChar = charStr.data(using: encoding) {
// Get the hexStr of every notAllowed-char-byte and put a % infront of it, append the result to the returnString
for byte in bytesOfChar {
returnStr += "%" + String(format: "%02hhX", byte as CVarArg)
}
} else {
returnStr += charStr
}
}
return returnStr
}
}
Usage:
"aouäöü!".urlPercentEncoded(withAllowedCharacters: .urlQueryAllowed,
encoding: .isoLatin1)
// Results in -> "aou%E4%F6%FC!"
For Objective-C :
- (NSString *)stringByAddingPercentEncodingForISOLatin1 {
NSCharacterSet *allowedCharacterSet = [NSCharacterSet characterSetWithCharactersInString:#"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-.~=&"];
NSData *data = [self dataUsingEncoding:NSISOLatin1StringEncoding];
if (data) {
NSMutableString *result = [#"" mutableCopy];
const char *bytes = [data bytes];
for (NSUInteger i = 0; i < [data length]; i++)
{
unsigned char ch = (unsigned char)bytes[i];
if (ch >= 0x80 || ![allowedCharacterSet characterIsMember:ch]) {
[result appendFormat:#"%%%02X", ch];
} else {
[result appendFormat:#"%c", ch];
}
}
return [result copy];
}
return nil;}

How to translate push tokens between NSData and NSString representations in Swift?

I have used the following Objective C routines for years, to convert a NSData push token into a NSString (for use by a web side push service), and the inverse, to take a known NSString version of the token and recreate the NSData representation. Now, I find the need for the exact same capabilities, but in Swift.
The dataToHex Objective C code essentially uses printf formatting:
- (NSString *)dataToHex:(NSData *)data
{
NSMutableString *str = [NSMutableString stringWithCapacity:100];
const unsigned char *p = [data bytes];
NSUInteger len = [data length];
for(int i=0; i<len; ++i) {
[str appendFormat:#"%02.2X", p[i]];
}
return str;
}
The inverse translation is:
- (NSData *)hexToData:(NSString *)str
{
const char *ptr = [str cStringUsingEncoding:NSASCIIStringEncoding];
NSUInteger len = [str length]/2;
NSMutableData *data = [NSMutableData dataWithCapacity:len];
while(len--) {
char num[5] = (char[]){ '0', 'x', 0, 0, 0 };
num[2] = *ptr++;
num[3] = *ptr++;
uint8_t n = (uint8_t)strtol(num, NULL, 0);
[data appendBytes:&n length:1];
}
return data;
}
By "cleverly" overwriting a two byes in an ASCII array, the "0xXX" string is converted into a byte, which is then appended to the mutable data object.
Now that I'm coding in Swift, I need the same capabilities but have not found any posts with anything like the code above in Swift.
Converting from the NSData representation as supplied by iOS matches the Objective C code almost line for line:
func dataToHex(data: NSData) -> String
{
var str: String = String()
let p = UnsafePointer<UInt8>(data.bytes)
let len = data.length
for var i=0; i<len; ++i {
str += String(format: "%02.2X", p[i])
}
return str
}
However, given an NSString object, the conversion back to a NSData object is a bit harder. You might need to do this if you are testing in the Simulator, have a string token from a real device, and need it to say register with a service.
The first approach I took tried to replicate code I used before, by creating a string with character pairs, and calling strtol:
func hexToData0(str: NSString) -> NSData {
let len = str.length/2
var data = NSMutableData(capacity:len)!
var num: [Int8] = [ 0, 0, 0 ]
let ptr = str.cStringUsingEncoding(NSUTF8StringEncoding)
for var i=0; i<len; ++i {
num[0] = ptr[i*2+0]
num[1] = ptr[i*2+1]
var n = UInt8 ( strtol(&num, nil, 16) )
data.appendBytes(&n, length:1)
}
return data;
}
I just felt the strtol was a bit of a hack, so I did the same using NSScanner that about the same code size, while most likely less efficient:
func hexToData1(str: NSString) -> NSData {
var data = NSMutableData(capacity: str.length/2)!
for var i = 0; i<str.length; i+=2 {
let r = NSRange(location: i, length: 2)
let s = str.substringWithRange(r)
let sc = NSScanner(string: s)
var val: UInt32 = 0
let ret = sc.scanHexInt(&val)
if ret {
var b = UInt8(val)
data.appendBytes(&b, length: 1)
} else {
assert(false, "Yikes!")
}
}
return data
}
Then, it occurred to me that I could do it all in Swift, no Darwin or Foundation needed, at the expense of a few more lines of code:
// Swift 4
func hexToData(str: String) -> Data {
let len = str.count/2
var data = Data(capacity:len)
let ptr = str.cString(using: String.Encoding.utf8)!
for i in 0..<len {
var num: UInt8 = 0
var multi: UInt8 = 16;
for j in 0..<2 {
let c: UInt8 = UInt8(ptr[i*2+j])
var offset: UInt8 = 0
switch c {
case 48...57: // '0'-'9'
offset = 48
case 65...70: // 'A'-'F'
offset = 65 - 10 // 10 since 'A' is 10, not 0
case 97...102: // 'a'-'f'
offset = 97 - 10 // 10 since 'a' is 10, not 0
default:
assert(false)
}
num += (c - offset)*multi
multi = 1
}
data.append(num)
}
return data;
}
I'm using the final hexToData in my code.

How to check if a character is supported by a font

I'm working on an app with a text field. The text wrote in this field will be printed and I have an issue with some characters like emoji, chinese characters, etc... because the font do not provide these characters.
It's why I want to get all the character provided by a font (The font is downloaded so I can deal directly with the file or with an UIFont object).
I heard about CTFontGetGlyphsForCharacters but I'm not sure that this function do what I want and I can't get it work.
Here is my code :
CTFontRef fontRef = CTFontCreateWithName((CFStringRef)font.fontName, font.pointSize, NULL);
NSString *characters = #"🐯"; // emoji character
NSUInteger count = characters.length;
CGGlyph glyphs[count];
if (CTFontGetGlyphsForCharacters(fontRef, (const unichar*)[characters cStringUsingEncoding:NSUTF8StringEncoding], glyphs, count) == false)
NSLog(#"CTFontGetGlyphsForCharacters failed.");
Here CTFontGetGlyphsForCharacters return false. It's what I want because the character '🐯' is not provided by the font used.
The problem is when I replace NSString *characters = #"🐯" by NSString *characters = #"abc", CTFontGetGlyphsForCharacters return again false. Obviously, my font provide a glyph for all the ASCII characters.
I finally solve it :
- (BOOL)isCharacter:(unichar)character supportedByFont:(UIFont *)aFont
{
UniChar characters[] = { character };
CGGlyph glyphs[1] = { };
CTFontRef ctFont = CTFontCreateWithName((CFStringRef)aFont.fontName, aFont.pointSize, NULL);
BOOL ret = CTFontGetGlyphsForCharacters(ctFont, characters, glyphs, 1);
CFRelease(ctFont);
return ret;
}

How to capture last 4 characters from NSString

I am accepting an NSString of random size from a UITextField and passing it over to a method that I am creating that will capture only the last 4 characters entered in the string.
I have looked through NSString Class Reference library and the only real option I have found that looks like it will do what I want it to is
- (void)getCharacters:(unichar *)buffer range:(NSRange)aRange
I have used this once before but with static parameters 'that do not change', But for this implementation I am wanting to use non static parameters that change depending on the size of the string coming in.
So far this is the method I have created which is being passed a NSString from an IBAction else where.
- (void)padString:(NSString *)funcString
{
NSString *myFormattedString = [NSString stringWithFormat:#"%04d",[funcString intValue]]; // if less than 4 then pad string
// NSLog(#"my formatedstring = %#", myFormattedString);
int stringLength = [myFormattedString length]; // captures length of string maybe I can use this on NSRange?
//NSRange MyOneRange = {0, 1}; //<<-------- should I use this? if so how?
}
Use the substringFromIndex method,
OBJ-C:
NSString *trimmedString=[string substringFromIndex:MAX((int)[string length]-4, 0)]; //in case string is less than 4 characters long.
SWIFT:
let trimmedString: String = (s as NSString).substringFromIndex(max(s.length-4,0))
Try This,
NSString *lastFourChar = [yourNewString substringFromIndex:[yourNewString length] - 4];
You can check this function in Swift 5:
func subString(from myString: NSString, length: Int) {
let myNSRange = NSRange(location: myString.length - length, length: length)
print(myString.substring(with: myNSRange))
}
subString(from: "Menaim solved the issue", length: 4) // Output: ssue

Resources