0

我有这个字符串,我想在标签中显示:

 NSString *criticsScore=[NSString stringWithFormat:@"%@\%%",[dict objectForKey:@"critics_score"]];

 _criticRating.text=criticsScore;

我想设置一个小字体\%%和一个大字体[dict objectForKey:@"critics_score"]];
这可能吗?

4

3 回答 3

8

您需要使用自己的控件来绘制一个NSAttributedString,例如TTTAttributedLabel.

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;

以上代码我来自如何在标签内的单个字符串上使用多种字体样式?

于 2013-03-26T20:13:55.903 回答
0

你必须用两个 UILabel 来做。您可以将第一个标签设置为除 \%% 之外的所有文本,并使用 sizeWithFont: 在该标签中的文本上获取该标签的大小。然后将第二个标签设置为从该标签帧的末尾开始。

因此,它看起来像这样,根据您想要标签的位置更改坐标:

NSString *criticsScore = [NSString stringWithFormat:@"%@",[dict objectForKey:@"critics_score"]];
NSString *str2 = @"\%%";

UIFont *criticsFont = [UIFont systemFontOfSize:[UIFont systemFontSize]];
UIFont *font2 = [UIFont systemFontOfSize:12.0];

//Get the sizes of each text string based on their font sizes.
CGSize criticsSize = [criticsScore sizeWithFont:criticsFont];
CGSize size2 = [str2 sizeWithFont:font2];


int x = 0;
int y = 0;
//The first label will start at whatever x and y are.
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(x,y,criticsSize.width,criticsSize.height)];
[label1 setFont:criticsFont];
//Create a second label with the x starting at x+criticsSize.width;
//The y will start at y+criticsSize.height-size2.height, so that it will be aligned with the bottom.
//Change these to align it differently.
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(x+criticsSize.width,y+criticsSize.height-size2.height,size2.width,size2.height)];
[label2 setFont:font2];
[self.view addSubview:label1];
[self.view addSubview:label2];
于 2013-03-26T20:06:26.587 回答
0

阅读这篇文章。大约是NSAttributedStrings。我认为这就是你所需要的。

于 2013-03-26T20:04:28.063 回答