is it possible to set the UILabel distance between the Letters? - iphone

is it possible to set a distance between the Letters like a b c

Yes, you could do by using the StringWithFormat or initWithFormat function of NSString.
Do something like below ..
NSString* myString = [[NSString alloc] initWithFormat:#"%# %# %#",#"a",#"b",#"c"];
Assign myString to your label.

Neither UILabel nor the UIFont you set it to have character spacing options. There's likely a way to do with with Core Text, but a quick look through the framework documentation didn't reveal it.

Related

attributed string which can accept instances depending upon what is passed from a table view

I have a tableView which users will select an instance. I have segue set-up to pass the instance to the textView. I have an NSLog setup so I know the correct data is being passed. I want to setup an NSAttributedString to accept the data and import the relevant portions into the attributedString template.
Seems to me it should look like this:
displayText = [[NSMutableAttributedString alloc] initWithString:#"%#\n%#-%#",detailName, beginDate, endDate") attributes:#{NSFontAttributeName: [UIFont fontWithName:#"Helvetica" size:26]}];
My ideal formatting is would be to create the string and have identify the name, begin, and end spaces in a way that allows them to have individual attributes. I've tried every combination of moving pieces but I simply cannot make this work. I receive errors such as Incompatible pointer types sending NSString to NSAttributedString, too many arguments, etc.
I'm new to programming and learning as I go but I've bought books and watched videos but I cannot figure out where I'm going wrong. Thanks for the help.
Isaac
That's because you need to use NSString's stringWithFormat: in order to pass variables to the string. Here's an example: (and there was an extraneous quotation mark after the argument list)
NSDictionary *attributes = #{NSFontAttributeName: [UIFont fontWithName:#"Helvetica" size:26]};
NSMutableAttributedString *displayText = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:#"%#\n%#-%#",detailName, beginDate, endDate] attributes:attributes];

Showing emoji in a UILabel?

Not sure what I'm missing here, and searching hasn't helped me. I want to display emoji characters in a UILabel, and this isn't doing it:
UILabel *label = [[UILabel alloc] init];
label.font = [UIFont fontWithName:#"AppleColorEmoji" size:16.0];
label.text = [NSString stringWithFormat:#"%C", 0x1F431];
// ... etc.
Works fine with other non-letter unicode characters, e.g. chess pieces, but not with any emoji characters that I have tried.
To use Emoji's just press Control+command+space (⌃⌘Space). No need of using unicode for emoji.
You are probably not using the correct encoding for your emoji characters. For instance in your example I think you are looking for something like this:
label.text = [NSString stringWithFormat:#"%C", 0xe04f];
Have a look at this table to get the encodings you need.
In xcode just go to the top bar and click EDIT > EMOJIS & SYMBOLS and an emoji box will pop up and you can literally add it to any text in the app, even works in the interface builder if you need to add it to the text of a uilabel there.
In Swift you can do:
label.text = "🐈"
Be sure to include the quotes. Otherwise you'll be setting the text to whatever is in the variable.
The following would display as "cat":
let 🐈 = "cat"
label.text = 🐈
The unicode 6.1 encodings work as well, but you would have to specify them like this:
label.text = #"\U0001F431";

Escaping the #" " characters in Objective C

I'm trying to set the background image of a view based on a passed "artistID." Here is my code so far:
NSString *backgroundImageName = [[NSString alloc]initWithFormat:#"artistbackground%i.png",_artistID];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:backgroundImageName]];
NSLog(#"%#",backgroundImageName);
unfortunately, for the parameter in ImageNamed, I'm passing in:
artistibackground1
instead of:
#"artistbackgound1"
Any ideas on how to escape the # and the quotes??
Thanks!!
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:[NSString stringWithFormat:#"#%#",backgroundImageName]];
Essentially make two strings, it will add the #"" in the second.
You should use \ before the character you want. An example:
NSLog(#"\#\"Try\"");
The code prints
#"Try"
Don't forget that even string constants are NSString objects in Objective-C. This is part of the magic! I frequently see programmers new to the language writing this line:
[NSString stringWithFormat:#"#%#",backgroundImageName];
But you can simplify this line to:
[#"#" stringByAppendingString:backgroundImageName];
Magic!

How to replace text in UITextView with selected range?

I want to replace the text in UITextView text in selected range. This is my question. Here i mention what i did? and what i want to do?. I have an UITextView and entered the below text in textview.
Ask question here, i have saved the range in of the text in textview. The text range is {17, 0}. I take the NSRange in -(void) textViewDidChange:(UITextView *)textView delegate.
Now i want to replace the text question with answer and i want to replace the text here with them. The UITextView.text look like this,Ask answer them` after replaced the texts.
How can i replace the texts with the selected ranges? Can you please help me? Thanks in advance.
Since iOS 7 there is the textStorage in the textView that inherits from NSMutableAttributedString so you can use those methods:
Objective-C
[self.textView.textStorage replaceCharactersInRange:withString:];
or
[self.textView.textStorage replaceCharactersInRange:withAttributedString:];
Swift
textView.textStorage.replaceCharacters(in: range, with: string)
Well... I'm not sure to understand correctly what you're trying to do, but if your goal is to change some characters in a selected range you can follow these steps:
Get your UITextView content and put it in a NSString:
NSString *textViewContent = textView.text;
Change the characters in the range you want:
NSString *newContent = [textViewContent stringByReplacingCharactersInRange:range withString:replacement];
Replace old content with new one:
textView.text = newContent;
Anyway if you just want to replace Ask question here with Ask answer them the fastest solution is just:
textView.text = #"Ask answer them";
Well solution from top of my head..
NSArray *Dividedstring = [[self.TextView.text] componentsSeparatedByString:#" question here "]; // this will divide the string into two parts ..one before question here and second after question here.
NSString * firstpart = [Dividedstring objectAtIndex:0];
NSString * secondpart = [Dividedstring objectAtIndex:0];
self.TextView.text = [NSString stringwithFormat:#"%# answer here %#",firstpart,secondpart];

iPhone, change color to each letter

It is possible to change a NSString color by letter?
For example, if you have NSString *A=#"ASDFGH";
show in UILabel ASD with red and FGH with blue.
And another question..
If I have a NSString, can I access the letter I want?
For example, in NSString *A=#"ASDFGH"; how can I know what is the second letter which is S, or the third, which is D?
for your second question you can use these three methods of NSString
– substringFromIndex:
– substringWithRange:
– substringToIndex:
1) You need to either create separate UILabels for each group of characters in the string , use NSAttributedString (see this), or you can just use CoreGraphics to render each group in a different color. The latter is probably the most efficient but also the most complex.
2) Use -[NSString characterAtIndex:] like so:
NSString *string = #"ASDFGH";
unichar char = [string characterAtIndex:1]; //the second letter
if (char == 'S') {
//do something
}