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

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.

Related

How can i show degree of expression in UILabel? (Swift)

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}");

NSLocalized Creating a varying string from strings file

I have a string which hold a varying part in the middle according to some cases.
Example: You lost 255 points.
The point value "255" is the varying part and I want to hold the non-varying part in my string file. However I don't want to have two entries in my strings file like.
"string_start" = "You lost"
"string_end" = "points."
Btw the points part which is (255) in the example is an NSMutableAttributedString to support a different color and font style.
Thanks in Advance.
I would do it like this:
[NSString stringWithFormat:NSLocalizedString(#"You lost %d points", nil), 255]
and you localize #"You lost %d points" in whatever language you want.

Escape Unicode Characters for iOS

There are some Unicode arrangements that I want to use in my app. I am having trouble properly escaping them for use.
For instance this Unicode sequence: 🅰
If I escape it using an online tool i get: \ud83c\udd70
But of course this is an invalid sequence per the compiler:
var str = NSString.stringWithUTF8String("\ud83c\udd70")
Also if I do this:
var str = NSString.stringWithUTF8String("\ud83c")
I get an error "Invalid Unicode Scalar"
I'm trying to use these Unicode "fonts":
http://www.panix.com/~eli/unicode/convert.cgi?text=abcdefghijklmnopqrstuvwxyz
If I view the source of this website I see sequences like this:
&#x1D552
Struggling to wrap my head around what is the "proper" way to work with/escape unicode.
And simply need a to figure out a way to get them working on iOS.
Any thoughts?
\ud83c\udd70 is a UTF-16 surrogate pair which encodes the unicode character 🅰 (U+1F170). Swift string literals do not use UTF-16, so that escape sequence doesn't make sense. However, since 1F170 has five digits you can't use a \uXXXX escape sequence (which only accepts four hexadecimal digits). Instead, use a \UXXXXXXXX sequence (note the capital U), which accepts eight:
var str = "\U0001F170" // returns "🅰"
You can also just paste the character itself into your string:
var str = "🅰" // returns "🅰"
Swift is an early Beta, is is broken in many ways. This issue is a Swift bug.
let ringAboveA: String = "\u0041\u030A" is Å and is accepted
let negativeSquaredA: String = "\uD83D\uDD70" is 🅰 and produces an error
Both are decomposed UTF16 characters that are accepted by Objective-C. The difference is that the composed character 🅰 is in plane 1.
Note: to get the UTF32 code point either use the OSX Character Viewer or a code snippet:
NSLog(#"utf32: %#", [#"🅰" dataUsingEncoding:NSUTF32BigEndianStringEncoding]);
utf32: <0001f170>
To get the Character Viewer in the Apple Menu go to the "System Preferences", "Keyboard", "Keyboard" tab and select the checkbox: "Show Keyboard & Character Viewers in menu bar". The "Character View" item will be in the menu bar just to the left of the Date.
After entering the character right (control) click on the character in favorites to copy the search results.
Copied information:
🅰
NEGATIVE SQUARED LATIN CAPITAL LETTER A
Unicode: U+1F170 (U+D83C U+DD70), UTF-8: F0 9F 85 B0
Better yet: Add unicode in the list on the left and select it.

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.

How to locate and display a Unicode character in iOS

I'm currently using an star/asterisk character to separate syllables in a vocabulary quiz program. But, I would prefer to use a black dot that sits about midway between the top and bottom of the line height. So, my questions are
1) How do I find the unicode for this character?
edit: from wikipedia, it looks like the character might be "middle dot", U+00B7
2) How do I display it?
You can just escape unicode characters inside NSString like this:
NSString *string = #"Hi \u00B7 there!";
Here's the great website to reference Unicode codes and how to place them inside programming languages (encodings section).
Simply copy and paste this " • " or use option+8

Resources