【问题标题】:Overriding UILabel覆盖 UILabel
【发布时间】:2012-09-19 15:25:15
【问题描述】:

我必须在 UILabel 中显示货币和价格。在一个标签中,但使用不同的字体大小。现在看起来像这样:

...我做到了覆盖 drawTextInRect: 像这样:

- (void)drawTextInRect:(CGRect)rect{

    CGSize textSize = [self.text sizeWithFont:self.font];
    textSize.width -= textSize.width/12;
    CGRect analogRect = CGRectMake(0, 0, textSize.width, self.frame.size.height);
    CGPoint centrino = self.center;

    self.frame = analogRect;
    self.center = centrino;

    NSString *currency = [self.text substringWithRange:NSMakeRange(0, 3)];
    NSString *amount = [self.text substringWithRange:NSMakeRange(3, self.text.length - 3)];

    self.text = currency;
    self.textAlignment = UITextAlignmentLeft;
    self.font = [UIFont fontWithName:@"Helvetica-Bold" size:30.];
    self.baselineAdjustment = UIBaselineAdjustmentAlignBaselines;
    [super drawTextInRect:analogRect];

    self.text = amount;
    self.textAlignment = UITextAlignmentRight;
    self.font = [UIFont fontWithName:@"Helvetica-Bold" size:40.];
    [super drawTextInRect:analogRect];    
}

很好,不是吗?

但我需要在底部对齐货币和价格,如下所示:

如你所见,我需要强制“EUR”降低,因为它的尺寸更小而且居中,所以看起来更高。

请提出一种方法来做到这一点。

提前致谢!

注意:

使用 2 个不同的标签对我不利。我必须在一个标签中做到这一点。

【问题讨论】:

    标签: iphone objective-c ipad overriding uilabel


    【解决方案1】:

    两个想法:1)由于您是 UILabel 的子类,您可以在子类的实现中为货币类型添加第二个标签,您的观点仍然认为只有一个标签。 2) 由于今天在 iOS 6 上取消了 NDA,我建议查看属性字符串。哦,还有 +1 写的是“a UILabel”而不是“an UILabel”。

    【讨论】:

      【解决方案2】:

      好的,伙计们,感谢大家的快速回答,但我没有时间研究,所以我找到了一个新手解决方案。

      我感到很惭愧,但就是这样:

      - (void)setCurrencyAndPrice{
      
      //set the labels' texts
          [label_currency setText:@"EUR"];
          [label_amount setText:@"12.34"];
      
      //set the sizes to fit the content
          [label_currency sizeToFit];
          [label_amount sizeToFit];
      
      //read the new frame of the labels
          CGRect curRect = label_currency.frame;
          CGRect amoRect = label_amount.frame;
      
      //adjust the position of the price to the top-right
          [label_amount setFrame:CGRectMake(320 - amoRect.size.width - 10, 0, amoRect.size.width, amoRect.size.height)];
      
      //read again the price frame
          amoRect = label_amount.frame;
      
      //stick the currency to the price, spacing 10 pixels
          [label_currency setFrame:CGRectMake(amoRect.origin.x - curRect.size.width - 10, 11, curRect.size.width, curRect.size.height)];
      
      }
      

      如您所见,无需覆盖,只需使用 2 个不同的标签 - 这正是我不想做的,但我编码的时间过得更快。

      干杯!

      P.S.:虽然我自己找到了解决方案,但我喜欢@MichaelMangold 的想法,所以我接受他的回答。

      【讨论】:

        【解决方案3】:

        您可以使用一个带有多种文本字体样式和颜色的标签,而不是使用两个 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
        
        }
        

        【讨论】:

          【解决方案4】:

          【讨论】:

            【解决方案5】:

            我将构建一个名为 ValueUnitsLabel 的 UIView 子类。给它两个标签,让它控制框架、字体大小等。它也可以聪明地阅读 NSLocale。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2015-05-25
              • 1970-01-01
              • 1970-01-01
              • 2019-05-02
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多