iPhone:How to add "Degree" symbol in UILabel string text? - iphone

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.

Related

Need to Customize the UIButton with attributed string as shown in figure

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.

Change UILabel color animated

How to change the color of the UILabel text gradually like the following link?
Can any one suggest me some code?
You can use formatted text.
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:#"Hello World"];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(1,5)];
iOS < 6
Second you need to subclass UILabel and print this string inside the drawRect method. You need to create a some type of loop that changes the color according to the speech speed.
iOS 6
You can use the attributedTextproperty (no need to subclass)
(void)drawTextInRect:(CGRect)rect
or reuse code:
https://github.com/mattt/TTTAttributedLabel
The app that you linked [http://www.youtube.com/watch?v=_vOYvaNhSHw] , probably is maded using cocos2d.
In cocos2d, you can change text color easily also with animation.
Here an example:
http://www.cocos2d-iphone.org/forum/topic/5903
Here cosos2d sdk, i suggest to try, because it's very powerful:
http://www.cocos2d-iphone.org/
enjoy.
here is one of my sample code. using block method of TTTAttributedLabel class it may help you .
[cell.lblAtt setText:strAtt afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
UIFont *italicSystemFont = [UIFont boldSystemFontOfSize:12];
CTFontRef italicFont = CTFontCreateWithName((__bridge CFStringRef)italicSystemFont.fontName, italicSystemFont.pointSize, NULL);
NSUInteger lenght = [[tempObj objectForKey:#"username"] length];
NSUInteger lenght2 = [[NSString stringWithFormat:#"%d",[tempArr count]] length];
[mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[ThemeColor CGColor] range:NSMakeRange(0,lenght)];
[mutableAttributedString addAttribute:(NSString*)kCTFontAttributeName value:(__bridge UIFont*)italicFont range:NSMakeRange(0,lenght)];
[mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[ThemeColor CGColor] range:NSMakeRange(lenght+11,lenght2)];
[mutableAttributedString addAttribute:(NSString*)kCTFontAttributeName value:(__bridge UIFont*)italicFont range:NSMakeRange(lenght+11,lenght2)];
return mutableAttributedString;
}];
Use NSAtributedString in UILabel from iOS 6.0. For lesser version below iOS 6.0 use TTTAttributedLabel which supports NSAtributedString
Change attributed string according to your requirement by setting it again in UILabel
EDIT add colored text as u want for example in loop
For 1st second in Label : I am good boy.
For 2nd second in Label : I am good boy.
For 3rd second in Label : I am good boy.
For 4th second in Label : I am good boy.
For 5th second in Label : I am good boy.

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

Justify Text in UILabel iOS

I am having a problem that in iOS I am using UILabel to display 2,3 line text, I want to align text as justified but I am not finding any option to do so. Any suggestions how to make justify text in label?
i put these line to make start it from top
CGSize maximumSize = CGSizeMake(300, 9999);
NSString *textString = someString;
UIFont *textFont = [UIFont fontWithName:#"Futura" size:14];
CGSize textStringSize = [textString sizeWithFont:textFont
constrainedToSize:maximumSize
lineBreakMode:text.lineBreakMode];
CGRect textFrame = CGRectMake(10, 110, 300, textStringSize.height);
text.frame = textFrame;
so any trick like this to make it justfiy
Thanks
There is now a convenient way to justify text since iOS6. You can create an instance of NSAttributedString, set appropriate properties and assign this attributed string to a text representing view such as UILabel, UITextView, etc. It's easy as this:
Create an instance of NSMutableParagraphStyle and set its properties.
NSMutableParagraphStyle *paragraphStyles = [[NSMutableParagraphStyle alloc] init];
paragraphStyles.alignment = NSTextAlignmentJustified; //justified text
paragraphStyles.firstLineHeadIndent = 10.0; //must have a value to make it work
Create NSDictionary for text attributes and create attributed string.
NSDictionary *attributes = #{NSParagraphStyleAttributeName: paragraphStyles};
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString: string attributes: attributes];
Set attributed string to a label.
existingLabel.attributedText = attributedString;
Can't be done I'm afraid - well not with UILabel.
You can use the UIWebView or a 3rd party library such as OHAttributedLabel
Happy Coding :)
Update:
This answer has been obsolete since iOS6. Please refer to Tankista's answer.
As mentionned by #martin, my class OHAttributedLabel can make this very easily.
(You will find it on my github and also find plenty of references to it on SO as well)
It can be done easily, but you need to use Core Text.
subclass a UIView, add an NSString property, create an NSAttributedString and pass kCTJustifiedTextAlignment value for the kCTParagraphStyleSpecifierAlignment key, then draw the NSAttributedString using Quartz or CoreText in your drawrect method.
edit: kCTParagraphStyleSpecifierAlignment key kCTJustifiedTextAlignment value should be used to create a CTParagraphStyleRef struct and passed in as a value for kCTParagraphStyleAttributeName key when creating the NSAttributedString.
SWIFT 4.x
version of approved answer:
Create an instance of NSMutableParagraphStyle and set its properties.
let justifiedParagraphStyles: NSMutableParagraphStyle = NSMutableParagraphStyle.init()
justifiedParagraphStyles.alignment = .justified //justified text
justifiedParagraphStyles.firstLineHeadIndent = 10.0 //must have a value to make it work
Create NSDictionary for text attributes and create attributed string.
let attributes = [NSAttributedStringKey.paragraphStyle: justifiedParagraphStyles]
let attributedString = NSAttributedString.init(string: string, attributes: attributes)
Set attributed string to a label.
existingLabel.attributedText = attributedString

IPhone: Change line spacing in UITextView?

I've been looking all over the 'net for information/examples...
I'm trying to change the line spacing of text inside a UITextView object to double spaced. I thought you could do it via Core Text, but haven't found a solution!
Any example code or information would be greatly appreciated! Thanks!
You don't have to look all over the net. A look at the documentation for UITextView is sufficient to determine that changing the line spacing is not supported by that control.
With Core Text you have complete control over the layout of the text you draw, of course. But it would be a lot of work to rewrite a UITextView-like control from scratch.
This question was asked before iOS 6. I'd like to post an updated answer for those who would like a way to do this.
This can be done now with iOS 6 and later by using an NSAttributedString. UITextView now accepts an attributed string as one of its properties. You can do all sorts of text manipulation with attributed strings, including line spacing. You can set the min and max line heights for the paragraph style attribute of the string.
Check out the NSAttributedString Class Reference for more information.
Here's a sample of what you could do:
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.minimumLineHeight = 21.0f;
paragraph.maximumLineHeight = 21.0f;
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:#"Test text line 1\nTest text line 2\nTest text line 3" attributes:#{NSParagraphStyleAttributeName: paragraph}];
textView.attributedText = attributedString;
You can use NSLayoutManagerDelegate. Add that delegate to your ViewController or UIView class (etc.) and then when you create your UITextView...
yourTextView.layoutManager.delegate = self
Add this delegate method:
func layoutManager(layoutManager: NSLayoutManager, lineSpacingAfterGlyphAtIndex glyphIndex: Int, withProposedLineFragmentRect rect: CGRect) -> CGFloat {
return 5 //Whatever you'd like...
}