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";
Related
Hello I want the create the Keypad as shown in picture. I have added buttons but don't know how to customize the UIButton as shown below in following link.
http://i.stack.imgur.com/Wt4uu.png
Any suggestion will be highly appreciated .....
We can set attributed string for button titleLabel.
NSString *string = #" 6\n MNO";
NSMutableAttributedString *attribString = [[NSMutableAttributedString alloc]initWithString:string];
//specify proper font for letters on button.
[attribString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:8] range:NSMakeRange(attribString.length -3, 3)];
//similarly add attribute for number also if needed.
self.attribButton.titleLabel.numberOfLines = 2;
[self.attribButton setAttributedTitle:attribString forState:UIControlStateNormal];
#Christeena John, Thanks for quick reply,
The UI looks like this.
http://i.stack.imgur.com/Mvq4k.png
I have used this code snippet from the github :-
https://github.com/kosyloa/PinPad
and updated UI a little bit as per my requirement.
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!
I'm reading some text from a local xml file and displaying it in a UILabel. The text in the xml initially had tabbed spacing in it. I removed this tabbing manually in the editor but it's still showing up in the UILabel and it makes the text layout look very messy.
How can I resolve this?
Try with below
myLabel.text = [myText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet];
When you assign the text to your label you can do this:
myLabel.text = [textWithTabs stringByReplacingOccurrencesOfString:#"\t" withString:#""];
This will remove the tabs completely.
stringByTrimmingCharactersInSet:
Returns a new string made by removing
from both ends of the receiver
characters contained in a given
character set.
- (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set
NSString Class Reference
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"];
}
I want to include "degree" symbol for weather report as 45 degree C. How to add degree symbol in string to include that UILabel?
If anybody knows it, please share to me.
Thank you.
Shift-option-8?
In Swift
cell.lblTemperature.text = NSString(format:"23%#", "\u{00B0}") as String
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:#"80\u00b0c"];
[attributedString setAttributes:#{NSFontAttributeName : [UIFont fontWithName:#"Helvetica-light" size:10.0]
, NSBaselineOffsetAttributeName : #22} range:NSMakeRange(2, 2)];
//asign this as a
examplelabel.attributedtext = attributedString;
In Xcode 8.0:
Press Control+Command+Space Bar or Xcode->Edit->Emoji & Symbols, later search the degree symbols from pop-up screen.
You can easily add UILabel or TextView components.