How to put out the alphabet? - ios

is there any way to put out the alphabet in iOS?
I want to populate a table with all the letters of an alphabet.
And also other alphabets.
Any suggestion to do that without any extensions or smth. else ??
If any way to do so with an extension, please let me know :)
Edit 1:
Thank you Carlos Chiari, here is my solution:
int asciiCode = 65;
for (asciiCode=65; asciiCode<=90; asciiCode++) {
NSString *string = [NSString stringWithFormat:#"%c", asciiCode];
NSLog(#"%#", string);
}
found here and just putted in a for condition:
How to convert ASCII value to a character in Objective-C?

Not sure about the exact syntax since I haven't played with iOS in a while, but just loop through the different ASCII characters from A - Z.
For instance in .Net it would be something like
For i as Integer = 65 To 90
Label.Text &= Char(i)
Next i
Char(i) prints the character with ASCII code "i", ASCII code for capital A is 65. ASCII code for capital Z is 90. So i loop through it from 65 to 90, which gives me A-Z.
Hope this helps :)

Related

UITextView control line breaks

I have a UITextView with items looking like this:
artist1 - song name1, artist2 - song name 2,
artist3 - song name3, etc.
I only want to disallow line breaks except between artists (after comma, before next artist), so this would be invalid:
artist 1 - song name1, artist 2 - // <- invalid break
song name 2.
I have tried two approaches for accomplishing this: Firstly, I put things in a webview, making this trivial to solve. Unfortunately, that is not possible for a couple of other reasons.
Secondly, I added _ where there are white spaces and hid them, like this:
artist_1_-_song_name1, artist_2_-_song_name_2, etc // <- setting colour of _ to invisible.
This almost worked, but now it breaks at the hyphen, like this:
artist_1_-
song_name1
I'm firstly looking for a good way to solve this. If that doesn't work, I would settle for a Unicode Char that looks like a dash but doesn't break (the U+2015 horizontal bar is too long)
Edit: Just found out about about Unicode Character 'NON-BREAKING HYPHEN' (U+2011), so while the problem is technically solved, I'm still looking for a less hackish way to do it though.
What you want is a non breaking space, or Unicode Character u+00a0.
NSString *nonbreakingspace = #"\u00A0";
NSString *nonbreakinghyphen = #"\u2011";
An replace your spaces in the artist1 - song name1 with the nonbreakingspace string and the hyphen with the non breaking hyphen nonbreakinghyphen string.
NSString *text = ...;
text = [text stringByReplacingOccurrencesOfString:#" " withString:nonbreakingspace];
text = [text stringByReplacingOccurrencesOfString:#"-" withString:nonbreakinghyphen];
myTextView.text = text;
swift 4 version!
var nonbreakinghyphen = "\u{2011}"
var text = "i-21"
myTextView.text = text.replacingOccurrences(of: "-", with: nonbreakinghyphen)}

Text string with EMOJI causing issues with NSRange

I am using TTTAttributedLabel to apply formatting to text, however it seems to crash because I am trying to apply formatting to a range which includes emoji. Example:
NSString *text = #"#user1234 🍺🍺 #hashtag"; // text.length reported as 22 by NSLog as each emoji is 2 chars in length
cell.textLabel.text = text;
int length = 8;
int start = 13;
NSRange *range = NSMakeRange(start, length);
if (!NSEqualRanges(range, NSMakeRange(NSNotFound, 0))) {
// apply formatting to TTTAttributedLabel
[cell.textLabel addLinkToURL:[NSURL URLWithString:[NSString stringWithFormat:#"someaction://hashtag/%#", [cell.textLabel.text substringWithRange:range]]] withRange:range];
}
Note: I am passed the NSRange values from an API, as well as the text string.
In the above I am attempting to apply formatting to #hashtag. Normally this works fine, but because I have emoji involved in the string, I believe the range identified is attempting to format the emoji, as they are actually UTF values, which in TTTAttributedLabel causes a crash (it actually hangs with no crash, but...)
Strangely, it works fine if there is 1 emoji, but breaks if there are 2.
Can anyone help me figure out what to do here?
The problem is that any Unicode character in your string with a Unicode value of \U10000 or higher will appears as two characters in NSString.
Since you want to format the hashtag, you should use more dynamic ways to obtain the start and length values. Use NSString rangeOfString to find the location of the # character. Use that results and the string's length to get the needed length.
NSString *text = #"#user1234 🍺🍺 #hashtag"; // text.length reported as 22 by NSLog as each emoji is 2 chars in length
cell.textLabel.text = text;
NSUInteger start = [text rangeOfString:#"#"];
if (start != NSNotFound) {
NSUInteger length = text.length - start;
NSRange *range = NSMakeRange(start, length);
// apply formatting to TTTAttributedLabel
[cell.textLabel addLinkToURL:[NSURL URLWithString:[NSString stringWithFormat:#"someaction://hashtag/%#", [cell.textLabel.text substringWithRange:range]]] withRange:range];
}
I assume this is from the Twitter API, and you are trying to use the entities dictionary they return. I have just been writing code to support handling those ranges along with NSString's version of the range of a string.
My approach was to "fix" the entities dictionary that Twitter return to cope with the extra characters. I can't share code, for various reasons, but this is what I did:
Make a deep mutable copy of the entities dictionary.
Loop through the entire range of the string, unichar by unichar, doing this:
Check if the unichar is in the surrogate pair range (0xd800 -> 0xdfff).
If it is a surrogate pair codepoint, then go through all the entries in the entities dictionary and shift the indices by 1 if they are greater than the current location in the string (in terms of unichars). Then increment the loop counter by 1 to skip the partner of this surrogate pair as it's been handled now.
If it's not a surrogate pair, do nothing.
Loop through all entities and check that none of them overrun the end of the string. They shouldn't, but just incase. I found some cases where Twitter returned duff data.
I hope that helps! I also hope that one day I can open source this code as I think it would be incredibly useful!

drop decimals in this method

I have a statement that shows 50.02%
How can I drop the decimals and show 50%?
Sorry this is so basic, pretty new to xcode.
Thank you in advance.
-(void)updatePercent{
percent.text = [NSString stringWithFormat:#"%.2f%#", 100.0f, #"%"];
}
Use this:
percent.text = [NSString stringWithFormat:#"%.0f%%", 100.0f];
Also note the use of %% to print a single %.
When printing floats, you can control how many decimals places are printed out by using numbers between the % and the f in the %f format statement.
The number on the right of the decimal tells how many decimal places should be printed. The number on the left of the decimal tells at least how many total places should be printed (including the decimal and all digits). If the number on the left is prefixed by a zero, the resulting string will have zeros appended to the left.
float myNum = 32.142;
// without declaring total digits
[NSString stringWithFormat:#"%.0f"] // 32
[NSString stringWithFormat:#"%.2f"] // 32.14
[NSString stringWithFormat:#"%.5f"] // 32.14200
// put a zero in front of the left digit
[NSString stringWithFormat:#"%010.0f"] // 0000000032
[NSString stringWithFormat:#"%010.2%"] // 0000032.14
// without the zero prefix (leading whitespace)
[NSString stringWithFormat:#"%10.2f"] // 32.14

Character code in NSString to unicode character

I have an NSString with a charactercode like this: 0x1F514.
I want to take this NSString and add it to another NSString, but not with the literal value of it, but the icon hidden behind it. In this case an emoticon of a bell.
How can I easily convert this NSString to show the emoticon instead of the character code?
Something like this would do:
NSString *c = #"0x1F514";
unsigned intVal;
NSScanner *scanner = [NSScanner scannerWithString:c];
[scanner scanHexInt:&intVal];
NSString *str = nil;
if (intVal > 0xFFFF) {
unsigned remainder = intVal - 0x10000;
unsigned topTenBits = (remainder >> 10) & 0x3FF;
unsigned botTenBits = (remainder >> 0) & 0x3FF;
unichar hi = topTenBits + 0xD800;
unichar lo = botTenBits + 0xDC00;
unichar unicodeChars[2] = {hi, lo};
str = [NSString stringWithCharacters:unicodeChars length:2];
} else {
unichar lo = (unichar)(intVal & 0xFFFF);
str = [NSString stringWithCharacters:&lo length:1];
}
NSLog(#"str = %#", str);
The reason simply #"\u1f514" doesn't work is because those \u values cannot be outside the BMP, i.e. >0xFFFF, i.e. >16-bit.
So, what my code does is check for that scenario and does the relevant surrogate pair magic to make the right string.
Hopefully that is actually what you want and makes sense!
If your NSString contains this "bell" character, then it does. You just append strings the usual way, like with stringByAppendingString.
The drawing of a bell instead of something denoting an unknown character is a completely separate issue. Your best bet is to ensure you're not using CoreText for drawing this, as it's been reported elsewhere, and I've seen it myself at work, that various non-standard characters may not work when printed that way. They do work, however, when printed with UIKit (that should be standard UI components, UIKitAdditions, and so on).
If using CoreText, you might get a bit lucky if you disable some text properties for the string with this special character, or choose appropriate font (but I won't help you here; we decided to leave the issue as Won't fix).
Having said that, the last time I was dealing with those was in pre-iOS 6 days...
Summary: your problem is not appending strings, but how you draw them.

Xcode iPhone SDK - Keep NSInteger zero at beginning

I want to keep the zero at the beginning of my NSInteger, but when i NSLog it, the zero is removed.
NSInteger myInteger = 05;
NSLog("%d", myInteger);
log: 5
I get 5 instead of 05. How can i keep the 0 at the beginning of the integer?
NSInteger doesn't do "leading" zeros. You're thinking about a number formatting thing.
If you just want to print out leading zeros via "NSLog", try something like:
NSLog( "%02d", myInteger);
Which instructs NSLog to have two digits and if it doesn't reach two digits, do a leading zero.
Take a look at the printf format specifiers (which NSLog tries to conform to) and you'll see how leading zeros are added there.
I am not sure, but I think it is a question of how you write your "%d" flag. So you cas use NSLog(#"%03d", var);.
It will print 005.
This is how it works for strings:
INT
String(format:"%05.2f", 5.0) will output: 05.00
DOUBLE
String(format:"%02d", 5) will output: 05
If you need it just for logging try it like this
NSLog(#"0%d" , 5) ;
or if you need to print it on the screen convert to string with format :
NSString *result = [[NSString alloc]initWithFormat:#"0%d" , yourInteger] ;

Resources