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?

halfer
  • 19,824
  • 17
  • 99
  • 186
TechBee
  • 1,897
  • 4
  • 22
  • 46

7 Answers7

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;
sanjana
  • 3,034
  • 2
  • 25
  • 31
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

Jasarien
  • 58,279
  • 31
  • 157
  • 188
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];
Community
  • 1
  • 1
Tom
  • 21
  • 2
  • 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 – Deepak G M Feb 13 '13 at 07:07
1

You should have a look at Core Text.

Here are some useful resources:

Nick Weaver
  • 47,228
  • 12
  • 98
  • 108
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.

ararog
  • 1,092
  • 10
  • 18
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.

Roger
  • 15,793
  • 4
  • 51
  • 73
  • 1
    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. – Wayne Hartman Apr 21 '11 at 12:27
  • 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. – Roger Apr 21 '11 at 12:44
  • Thanks Roger, actually I would like to go with CoreText. Seems convincing to me. – TechBee Apr 25 '11 at 03:42
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];
edc1591
  • 10,146
  • 6
  • 40
  • 63