2

我必须在 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 个不同的标签对我不利。我必须在一个标签中做到这一点。

4

5 回答 5

2

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

于 2012-09-19T15:43:08.253 回答
1

Ok, guys, thanks to everybody for your fast answers, but I have no time for researches, so I have found a newbie solution.

I feel shame but here is it:

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

}

As you can see, nothing to override, just using 2 different labels - exactly what I did NOT want to do, but the time runs faster that I am coding.

Cheers!

P.S.: Although I have found the solution for myself, I like @MichaelMangold' idea, so I accept his answer.

于 2012-09-19T16:15:53.567 回答
1

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

}
于 2012-09-20T10:21:03.673 回答
0

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

于 2012-09-19T15:47:06.207 回答
0

该库将为您工作https://github.com/AliSoftware/OHAttributedLabel

于 2012-09-19T15:43:06.500 回答