【问题标题】:How to use multiple font stylings on a single string inside a label? [closed]如何在标签内的单个字符串上使用多种字体样式? [关闭]
【发布时间】:2012-12-28 11:41:04
【问题描述】:

我有一个由 - 组成的字符串

lblWithText.backgroundColor = [UIColor clearColor];
    lblWithText.textColor = [UIColor blackColor];
    lblWithText.font = [UIFont fontWithName:@"Helvetica" size:12.0];
    lblWithText.text = [NSString stringWithFormat:@"Blah1:blah-blah%d. Blah2:-%d%%", [currentCoupon.couponPrice intValue],[currentCoupon.couponDiscountPercent intValue]];

请告诉我该怎么做,“Blah2:-%d%%”是Bolden? 提前致谢

【问题讨论】:

  • 你的代码最后一行不完整,请补全。
  • 如果你想加粗字体,只需将 Helvetica 替换为 @"HelveticaBold" 或 @"Helvetica Bold" ,这两种都可以
  • 谢谢,但我只需要粗体“Blah2:-%d%%”,而不是完整的字符串..而且我不明白该怎么做。
  • 如果你想让字符串的一部分加粗,你将不得不使用两个不同的标签或 UIWebview。
  • 还有一个替代方案。您可以选择使用 NSAttributedString 代替。使用两个标签不是一个好的选择

标签: iphone objective-c uilabel


【解决方案1】:

这可能对你有帮助

Bold & Non-Bold Text In A Single UILabel?

它有适用于 iOS 6 和 iOS5 的解决方案,您必须使用带有 NSAttributedString 的 CATextLayer

或者你可以参考http://www.cocoacontrols.com/platforms/ios/controls/ohattributedlabel

https://github.com/AliSoftware/OHAttributedLabel/

https://github.com/mattt/TTTAttributedLabel/

【讨论】:

    【解决方案2】:

    只需要Helvetica字体名称加粗像

    lblWithText.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0];

    只是一个例子..

    UILable *lbl1 = [[UILable alloc]init];
    lbl1.frame = CGRectMake(10,10,100,40);
    lbl1.backgroundColor = [UIColor clearColor];
    lbl1.textColor = [UIColor blackColor];
    lbl1.font = [UIFont fontWithName:@"Helvetica" size:12.0];
    lbl1.text = @"Stack";
    
    UILable *lbl2 = [[UILable alloc]init];
    lbl2.frame = CGRectMake(110,10,150,40);
    lbl2.backgroundColor = [UIColor clearColor];
    lbl2.textColor = [UIColor blackColor];
    lbl2.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0];
    lbl2.text = @"OverFlow";
    
    [self.view addSubview:lbl1];
    [self.view addSubview:lbl2];
    

    这是一个通过你得到想法的例子..

    更新:

    另请参阅 NSMutableAttributedString 示例:

    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"Blah1:blah-blah%d. Blah2:-%d%%", [currentCoupon.couponPrice intValue],[currentCoupon.couponDiscountPercent intValue]];
    [str addAttribute:NSBackgroundColorAttributeName value:[UIColor clearColor] range:NSMakeRange(0,30)];/// Define Range here and also BackGround color which you want
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0,30)];/// Define Range here and also TextColor color which you want
    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:20.0] range:NSMakeRange(20, 10)];
    lblWithText.attributedText = str;
    

    【讨论】:

    • 谢谢,但它确实是加粗的完整标签.. 我只需要这块-"Blah2:-%d%%
    • 为此,只需使用两个标签并按顺序设置框架,例如看我将编辑答案
    • 您可以选择使用 NSAttributedString 代替。使用两个标签不是一个好的选择
    • @Atif 是的,伙计,对...
    • @Atif 嘿,看看我用例子更新了答案...... :) 谢谢
    【解决方案3】:

    为此请使用 NSMutableAttributedString 请按照此教程了解 NSMutableAttributedString

    http://soulwithmobiletechnology.blogspot.in/2012/07/how-to-use-nsattributedstring-in-ios-6.html

    【讨论】:

      【解决方案4】:

      对于全粗体,你可以试试这个

      [lblWithText setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12.f]]
      

      也可以去这个链接查看各种fonts

      正如您所说,您希望某些部分像粗体一样正常,那么您可以尝试属性字符串,但不确定。

      或者去这个linkthis

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-07
        • 1970-01-01
        相关资源
        最近更新 更多