Escaping the #" " characters in Objective C - iphone

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!

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

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

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.

Line breaks not working in UILabel

I'm loading some help text from a plist and displaying the same in the form of UILabels housed in a UIScrollView. Portion of the code follows:
UILabel *sectionDetailLabel = [[[UILabel alloc] initWithFrame:CGRectMake(34, myOriginForThisSection, 286, 20)] autorelease];
sectionDetailLabel.backgroundColor = [UIColor clearColor];
sectionDetailLabel.numberOfLines = 0;
sectionDetailLabel.font = [UIFont systemFontOfSize:12];
sectionDetailLabel.textColor = [UIColor blackColor];
sectionDetailLabel.textAlignment = UITextAlignmentLeft;
sectionDetailLabel.lineBreakMode = UILineBreakModeWordWrap;
[baseScrollView addSubview:sectionDetailLabel];
[sectionDetailLabel setText:myStringForThisSection];
[sectionDetailLabel sizeToFit];
While any 'long' text is getting wrapped into multiple lines correctly, I'm unable to manually insert any line-breaks using newline '\n' characters in 'myStringForThisSection'. I only see the characters '\' and 'n' printed in the UILabel wherever I wanted the line-break, instead.
I looked this up and the general consensus seemed to be that setting numberOfLines to 0, setting the lineBreakMode to a valid value and invoking sizeToFit (or setting the frame of the UILabel based on sizeWithFont:) should do. All of which I seem to be doing in the code above - and works perfectly when fitting long strings of unknown length into multiple lines on the UILabel. So what could be missing here?
Note: All the variables used - baseScrollView, myStringForThisSection and myOriginForThisSection - were loaded before the above code began executing, and work fine.
UILabel doesn't interpret the escape sequence \n. You can insert the real character that represents the Carriage Return and/or the Line Feed. Make a char to hold your newline and then insert it.
unichar newLine = '\n';
NSString *singleCR = [NSString stringWithCharacters:&newLine length:1];
[myStringForThisSection insertString:singleCR atIndex:somePlaceIWantACR];
As long as your myStringForThisSection is mutable, that should do it.
I had trouble with Scot Gustafson's answer above in XCode 4.3
Try this instead:
unichar chr[1] = {'\n'};
NSString *cR = [NSString stringWithCharacters:(const unichar *)chr length:1];
Then use in your code something like this:
self.myLabel.text = [NSString stringWithFormat:#"First Label Line%#Second Label Line", cR];
I couldn't get Scott & DoctorG's solution to work (though I didn't spend too much time trying), but here's the simple solution that works for me when I'm extracting escaped text from an xml file.
Inside my string function class, I define:
+(NSString)escapeXml:(NSString*)string {
return [string stringByReplacingOccurrencesOfString:#"\\n" withString:#"\n"];
}

Cannot create a UIColor using colorFromPatternImage:

The following works without issue:
toolBar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"window_bkd.png"]];
However, I have similar statements scattered throughout my code and wanted to clean it up using the following statements, which crash on executing the first statement:
UIColor *bkdColor = [[UIColor alloc] colorWithPatternImage:[UIImage imageNamed:#"window_bkd.png"]];
toolBar.backgroundColor = bkdColor;
[bkdColor release];
Console output from the crash:
[UIPlaceholderColor colorWithPatternImage:]: unrecognized selector sent to instance 0x5203c90
Thanks for your help, I'm sure this is a Homer Simpson "doh!" mistake.
You accidently placed an alloc call in your second version, so you are calling colorWithPatternImage on an instance, while it is a class method. Doh! :-)
This is how it's done correctly:
UIColor *bkdColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"window_bkd.png"]];