4

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?

1
  • can't you add two UIlabel? if so, first one with bold style and the other one with italic style. Commented Apr 21, 2011 at 12:22

7 Answers 7

11

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; 
Sign up to request clarification or add additional context in comments.

Comments

3

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

2 Comments

Thanks Jasarien.I am trying this out.
The link for Oliver Drobnik's rich text view is broken!
2

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

1 Comment

How do you expect people to use this for longer texts? A solution should be generic in nature that can cater to a larger text but at the same time should be easier to use. I am not giving a thumbs down for this answer since we should do so only if this does not answer the query but this is in no way a acceptable solution. Sorry for that but thats my opinion
1

You should have a look at Core Text.

Here are some useful resources:

Comments

0

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

Comments

0

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.

3 Comments

Oi, don't use a UIWebView for something that small. The UIWebView is a very expensive object. Depending on his requirements, he's better off creating separate labels or creating a static image. There are no clear choices here, mostly because the use case isn't really specified, but I would not use a UIWebView for this.
For a lot of (simple) cases there is no noticeable performance hit with the UIWebView but I agree it's not necessarily the right solution without knowing more about the use case.
Thanks Roger, actually I would like to go with CoreText. Seems convincing to me.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.