【问题标题】:Is it possible to set different font in One UIlabel?是否可以在一个 UIlabel 中设置不同的字体?
【发布时间】:2012-05-01 20:03:36
【问题描述】:

我有一个类似“这是一个好苹果”的字符串。显示在我的 UIlabel 上。我想用不同的字体设置“好”这个词。就像“这是一个苹果。”

【问题讨论】:

  • 我创建了一个简单的类来做到这一点:past.is/NDe3

标签: iphone fonts uilabel


【解决方案1】:

看看NSAttributedString...使用OHAttributedLabel绘制NSAttributedString,因为UILabel,UITextView不支持NSAttributedString...

PS:如果您计划分发 iOS6 或更新的应用程序,因为 UILabel 现在支持 NSAttributedString,您应该直接使用 UILabel 而不是 OHAttributedLabel,因为它现在已被操作系统原生支持。

【讨论】:

【解决方案2】:

这对于 UILabel 是不可能的。但你可以使用Core Text

另一种更简单的方法是使用非常小的 UIWebView。在此您可以使用 html 命令设置字体。

【讨论】:

    【解决方案3】:

    抱歉,UILabel 实现无法做到这一点,您有两种选择:

    • 您可以创建一个 UIWebView 并将文本 HTML 格式化
    • 您可以使用该功能制作您自己的 UILabel 实现,例如,Core Text

    【讨论】:

      【解决方案4】:

      您可以使用一个带有多种文本字体样式和颜色的标签,而不是使用两个 UILabel。这是一个例子:

      UILabel *customLbl = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 25)];
      customLbl.backgroundColor = [UIColor yellowColor];
      [self createTwoTextStyleInSingleUILabel:customLbl];// custom method calling
      [self.view addSubview:customLbl];
      

      自定义方法如下: 在应用此方法之前,在您的项目中添加 QuartzCore 框架(CALayers 需要)和 CoreText 框架(属性字符串需要。)。

        #import <QuartzCore/QuartzCore.h>
        #import <CoreText/CoreText.h>  
      
      - (void)createTwoTextStyleInSingleUILabel: (UILabel *) myLabel{
      NSString *firstText = NSLocalizedString(@"First text:", nil);
      NSString *secondText = [NSString stringWithFormat:@"%@ %@",firstText,@"Second text"];
      CATextLayer *myLabelTextLayer;
      /* Create the text layer on demand */
      if (!myLabelTextLayer) {
          myLabelTextLayer = [[CATextLayer alloc] init];
          myLabelTextLayer.backgroundColor = [UIColor clearColor].CGColor;
          myLabelTextLayer.wrapped = NO;
          CALayer *layer = myLabel.layer; //assign layer to your UILabel
          myLabelTextLayer.frame = CGRectMake((layer.bounds.size.width-180)/2 + 10, (layer.bounds.size.height-30)/2 + 10, 180, 30);
          myLabelTextLayer.contentsScale = [[UIScreen mainScreen] scale];
          myLabelTextLayer.alignmentMode = kCAAlignmentCenter;
          [layer addSublayer:myLabelTextLayer];
      }
      /* Create the attributes (for the attributed string) */
      // customizing first string
      CGFloat fontSize = 16;
      UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
      CTFontRef ctBoldFont = CTFontCreateWithName((__bridge CFStringRef)boldFont.fontName, boldFont.pointSize, NULL);
      CGColorRef cgColor = [UIColor redColor].CGColor;
      NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                  (__bridge id)ctBoldFont, (id)kCTFontAttributeName,
                                  cgColor, (id)kCTForegroundColorAttributeName, nil];
      CFRelease(ctBoldFont);
       // customizing second string
      UIFont *font = [UIFont systemFontOfSize:16];
      CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef)font.fontName, font.pointSize, NULL);
      CGColorRef cgSubColor = [UIColor blackColor].CGColor;
      NSDictionary *subAttributes = [NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)ctFont, (id)kCTFontAttributeName,cgSubColor, (id)kCTForegroundColorAttributeName, nil];
      CFRelease(ctFont);
      /* Create the attributed string (text + attributes) */
      NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:secondText attributes:attributes];
      float lengthOfSecondString = 12.0; // length of second string including blank space inbetween text, space in front , space after text.. Be careful, your  app may crash here if length is beyond the second text length (lengthOfSecondString = text length + blank spaces)
      [attrStr addAttributes:subAttributes range:NSMakeRange(firstText.length, lengthOfSecondString)];
      // you can add another subattribute in the similar way as above , if you want change the third textstring style
      /* Set the attributes string in the text layer :) */
      myLabelTextLayer.string = attrStr;
      myLabelTextLayer.opacity = 1.0; //to remove blurr effect
      
      }
      

      【讨论】:

        【解决方案5】:

        有一种方法可以使用 NSMutableAttributedString 在标签上设置不同/多种字体和其他属性。 Foll 是我的代码:

         UIFont *ArialFont = [UIFont fontWithName:@"arial" size:18.0];
         NSDictionary *arialdict = [NSDictionary dictionaryWithObject: ArialFont forKey:NSFontAttributeName];    
         NSMutableAttributedString *AattrString = [[NSMutableAttributedString alloc] initWithString:title attributes: arialdict];
        
         UIFont *VerdanaFont = [UIFont fontWithName:@"verdana" size:12.0];
         NSDictionary *veradnadict = [NSDictionary dictionaryWithObject:VerdanaFont forKey:NSFontAttributeName];
         NSMutableAttributedString *VattrString = [[NSMutableAttributedString alloc]initWithString: newsDate attributes:veradnadict];    
         [VattrString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:(NSMakeRange(0, 15))];
        
         [AattrString appendAttributedString:VattrString];
        
        
         lblText.attributedText = AattrString;
        

        请注意,lblText 是 UILabel,作为文件所有者的出口。
        可以继续添加他想要的任意数量的 NSMutableAttributedString。

        另外请注意,我在我的项目中添加了 verdana 和 arial 字体并为此添加了 plist。

        【讨论】:

        • 不工作 ..[NSDictionary dictionaryWithObject: ArialFont forKey:NSFontAttributeName];在这一行出现错误
        • @RajeshPillai 你在第一行定义了字体吗? UIFont *ArialFont = [UIFont fontWithName:@"arial" size:18.0];
        【解决方案6】:

        修改了 Akshay 的答案并用 Swift 实现:

        // First part with font size 10
        let firstPartInfo = [NSFontAttributeName: UIFont.systemFontOfSize(10.0)]
        var attributedString = NSMutableAttributedString(string: "First Part", attributes: firstPartInfo)
        
        // Second part with font size 20
        let secondPartInfo = [NSFontAttributeName: UIFont.systemFontOfSize(20.0)]
        attributedString.appendAttributedString(
            NSAttributedString(string: "Second Part", attributes: secondPartInfo))
        
        // Lastly set the attributed text
        label.attributedText = attributedString
        

        没有理由使用第三方解决方案或子类。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-01-20
          • 2010-11-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多