I am stuck with a small issue.
I need to use a sentence which will have first two words bold and last two words italic. Like:
I am using an Objective C Developer.
How to do that. Is this possible in Objective C?
I am stuck with a small issue.
I need to use a sentence which will have first two words bold and last two words italic. Like:
I am using an Objective C Developer.
How to do that. Is this possible in Objective C?
For iOS7 you can use this:
NSString * htmlString = @"<html><body> Some html string </body></html>";
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
UILabel * myLabel = [UILabel alloc] init];
myLabel.attributedText = attrStr;
Apple recommends that for small amounts of styled text, you should use a web view, and display the text marked up in HTML and formatted with CSS, etc.
Personally I've never taken that advice, as I would consider a UI full of web views to be a bit over kill.
There is the Core Text framework, if you want a little more control over your text and want to use attributed strings.
It isn't a one-liner though. Using core text requires quite a lot of code.
I have written a core text view that will display tappable URL links inline with text, but I have not given it arbitrary formatting support. If you're interested in showing links within text, then check it out: https://github.com/jasarien/CoreTextHyperlinkView
You may be interested in Oliver Drobnik's rich text view, which is based on Core Text too. As far as I know, you can feed it HTML and it'll produce a native view containing your formatted text. Very useful. It can be found here: https://github.com/Cocoanetics/DTCoreText
I've written a very small class called THMultiPartLabel to help me accomplish this sort of thing - you can find it on GitHub here. It's based heavily on Jason's answer to a similar question here. Using this class, you'd implement your example like so:
UIFont *normal = [UIFont systemFontOfSize:20];
UIFont *bold = [UIFont boldSystemFontOfSize:20];
UIFont *italic = [UIFont italicSystemFontOfSize:20];
NSArray *fonts = [NSArray arrayWithObjects: normal, bold, normal, italic, normal nil];
THMultiPartLabel *mpLabel = [[THMultiPartLabel alloc] initWithOffsetX:0 Y:0 defaultFonts:fonts];
[mpLabel updateText:@"I ", @"am using ", @"an ", @"Objective C ", "Developer", nil];
You should have a look at Core Text.
Here are some useful resources:
Since iOS 6 you can use NSAttributedString with an UILabel, a NSAttributedString takes a NSString as an argument in one of its contructors, after that you can specify which ranges of this string has a particular text style.
You can find a good example on how to do it on iOS 6 here: http://weblog.invasivecode.com/post/31931916393/introduction-to-nsattributedstring-for-ios-6
If you what something backwards compatible, I'm strongly recommend the OHAttributedLabel which can be found here: https://github.com/AliSoftware/OHAttributedLabel
Note: OHAttributedLabel also allows the usage of html markup to style your label text.
Your best option is probably to replace the UILabel with a UIWebView and use HTML to do the formatting.
[EDIT]
If you have lots of them and think there is too much of a performance hit, you can (from iOS 3.2 onwards) also consider NSAttributedString, although that will be a lot more coding.
You can't do both with the standard UILabel implementation. But you can do one or the other.
myLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:18];