2

Is it possible to convert a NSString to html and set as a label?

The code below shows the NSString I want to set finalPrice as bold text and finalStr&shipping string as normal text

NSString *myText = [NSString 
     stringWithFormat:
       @"%@\nFinal price including $%.2f Shipping and all discount: <b>$%.2f</b>",
      finalStr,shipping,finalPrice];
lbl.text = myText;

I want to set multiple color and multiple text type into same dyanamic label.

mmmmmm
  • 32,227
  • 27
  • 88
  • 117
user2239835
  • 75
  • 2
  • 7
  • So basically you need a special formatting, not exactly an html, right? – FreeNickname Apr 29 '13 at 06:47
  • @FreeNickname : i need conversion for another string which i not specify in question. – user2239835 Apr 29 '13 at 06:50
  • Unfortunately, I can't tell you an exact solution, but this question might help you: http://stackoverflow.com/questions/9696097/formatted-text-in-uilabel-on-iphone – FreeNickname Apr 29 '13 at 06:52
  • @FreeNickname : But my label is dynamic and i want to set multiple color and multiple text into same label so please help me. – user2239835 Apr 29 '13 at 06:54
  • Use [NSAttributedString](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSAttributedString_Class/Reference/Reference.html) – HAS Apr 29 '13 at 06:59
  • Have a look at [this SO question and answer](http://stackoverflow.com/questions/12776741/how-to-create-a-uilabel-or-uitextview-with-bold-and-normal-text-in-it) – HAS Apr 29 '13 at 07:21
  • @HAS : it's not helpful for me because i have only one label and i want to use all label property. – user2239835 Apr 29 '13 at 07:28
  • I think I don't understand what you want. If you have one label and want to display formatted text (bold, italic, color, etc) that's exactly the case where you should use `NSAttributedString`. What do you mean by `I want to use all label property`? Which ones can't you use using `NSAttributedString`? – HAS Apr 29 '13 at 07:35
  • you can't use different font and different colours in a single `UILabel`. Try using just a simple `UIView` or a `UIWebView` – Novarg Apr 29 '13 at 09:15
  • @Novarg Of course you can, see my answer ;) – HAS Apr 29 '13 at 09:37

3 Answers3

2

use following label for bold effects. Or you can get code from that class.

DAAttributedStringUtils

and also see this

Different Label

Edit

    NSString *myText = [NSString stringWithFormat:@"%@\nFinal price including $%.2f Shipping and all discount: %%B$%.2f%%b",finalStr,shipping,finalPrice];


     DAAttributedLabel* lbl = [[DAAttributedLabel alloc] initWithFrame:CGRectMake(30.0f, 30.0f, 260.0f, 24.0f)];
     lbl.backgroundColor = [UIColor colorWithRed:0.9f green:0.9f blue:1.0f alpha:1.0f]; 
     lbl.text = (id)[formatter formatString:myText];
     [self.view addSubview:lbl];
SAMIR RATHOD
  • 3,512
  • 1
  • 20
  • 45
0

Try using NSAttributedString

There are already several questions around this here like How do you use NSAttributedString?

NSString * textString = @"Hello Bold";
NSInteger _stringLength = [textString length];
NSMutableAttributedString * attString = [[NSMutableAttributedString alloc] initWithString:textString];

[attString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica" size:14.0f]; range:NSMakeRange(0, _stringLength)];

[attString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:14.0f]; range:NSMakeRange(6, 4)];

myLabel.attributedText = attString; 

(code not tested)

Edit: label.attributedText is only available for iOS 6.0+

Community
  • 1
  • 1
joao
  • 4,067
  • 3
  • 33
  • 37
0

FYI, the answer above suggesting the use of DAAttributedStringUtils and DAAttributedLabel didn't mention that these are convenience classes for the use of NSAttributedString. They make formatting NSAttributedString instances a little easier. As an example, here's how to do the same formatting described about by HAS using DAAttributedStringUtils:

float finalPrice = 34.99, shipping = 4.99;

// Setup the formatter
DAAttributedStringFormatter* formatter = [[DAAttributedStringFormatter alloc] init];
formatter.defaultFontFamily = @"Georgia";
formatter.defaultFontSize = 12.0f;
formatter.colors = @[ [UIColor blackColor], [UIColor redColor] ];
NSAttributedString* attrStr = [formatter formatString:@"%0C%0FRed Courier Text %1C%1FBlue Arial Text %0CRed Arial Text"];

// setup base strings
NSString *finalStr = @"Some Text. ";
NSString *shippingAttributed = [NSString stringWithFormat:@"%%B%%1C$%.2f%%b%%c", shipping];
NSString *middleText0 = @"Final price including ";
NSString *middleText1 = @" Shipping and all discount: ";
NSString *finalPriceAttributed = [NSString stringWithFormat:@"%%B%%1C$%.2f%%b%%c", finalPrice];

// Format the strings
self.label.attributedText = [formatter formatString:[NSString stringWithFormat:@"%@%@%%B%%1C%@%%b%%c%@%%B%%1C%@", finalStr, shippingAttributed, middleText0, middleText1, finalPriceAttributed];

Somewhat less code, and I think easier to understand. FYI, the formatter string in the last line contains codes that are used to modify the format of portions of the string. Those codes use double percents (

Dave L
  • 171
  • 10