【问题标题】:Convert NSString to HTML and set HTML text into UILabel [duplicate]将 NSString 转换为 HTML 并将 HTML 文本设置为 UILabel [重复]
【发布时间】:2013-04-29 06:45:32
【问题描述】:

是否可以将 NSString 转换为 html 并设置为标签?

下面的代码显示了我想将 finalPrice 设置为粗体文本并将 finalStr&shipping 字符串设置为普通文本的 NSString

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

我想在同一个动态标签中设置多种颜色和多种文本类型。

【问题讨论】:

  • 所以基本上你需要一个特殊的格式,而不是一个 html,对吧?
  • @FreeNickname :我需要转换另一个我没有指定的字符串。
  • 很遗憾,我无法告诉您确切的解决方案,但这个问题可能会对您有所帮助:stackoverflow.com/questions/9696097/…
  • @FreeNickname :但我的标签是动态的,我想在同一个标​​签中设置多种颜色和多种文字,所以请帮助我。

标签: objective-c nsstring uilabel


【解决方案1】:

使用以下标签以获得粗体效果。或者您可以从该类获取代码。

DAAttributedStringUtils

也看到了这个

Different Label

编辑

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

【讨论】:

  • DAAttributedLabel *label1;但我不能像这样设置它 lbl.text = label1;
  • 查看我的更新答案,无需使用您的 lbl。
  • 但是我怎样才能设置你的标签动态?????
  • 如何设置所有标签属性,如 numberOfLines 等?
  • 看这个方法 [lbl setPreferredHeight];您可以设置高度,无需设置行数。
【解决方案2】:

尝试使用NSAttributedString

这里已经有几个问题,比如 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; 

(代码未测试)

编辑: label.attributedText 仅适用于 iOS 6.0+

【讨论】:

  • @joao :这里我的字符串 len 没有修复。
  • 您可以即时计算字符串大小。如果你的问题是 "numbers" NSString * priceString = [NSString stringWithFormat:@"%.2f", 123.1234566]; NSInteger priceLength = [priceString 长度]; @HAS 实际上发布了您案例的完整代码
  • 我忘了说 label.attributedText 只适用于 6.0+
  • 但是these statistics 你可以忽略这个恕我直言;)
  • @joao : 应用程序崩溃了,它不工作了。
【解决方案3】:

仅供参考,上面建议使用 DAAttributedStringUtils 和 DAAttributedLabel 的答案没有提到这些是使用 NSAttributedString 的便利类。它们使格式化 NSAttributedString 实例更容易一些。例如,下面是如何使用 DAAttributedStringUtils 执行 HAS 所描述的相同格式:

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

代码少了一些,我觉得更容易理解。仅供参考,最后一行中的格式化程序字符串包含用于修改字符串部分格式的代码。这些代码使用双百分号(

【讨论】:

    猜你喜欢
    • 2011-12-31
    • 2011-02-06
    • 2014-01-30
    • 1970-01-01
    • 2014-09-24
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    • 2019-08-10
    相关资源
    最近更新 更多