我有这个字符串,我想在标签中显示:的UILabel使用不同的字体
NSString *criticsScore=[NSString stringWithFormat:@"%@\%%",[dict objectForKey:@"critics_score"]];
_criticRating.text=criticsScore;
我要为\%%
小字体和[dict objectForKey:@"critics_score"]];
大字体这可能吗?
我有这个字符串,我想在标签中显示:的UILabel使用不同的字体
NSString *criticsScore=[NSString stringWithFormat:@"%@\%%",[dict objectForKey:@"critics_score"]];
_criticRating.text=criticsScore;
我要为\%%
小字体和[dict objectForKey:@"critics_score"]];
大字体这可能吗?
阅读本文post。它大约是NSAttributedString
s。我认为那是你需要的。
你将不得不做两个UILabels。您可以将第一个标签设置为全部文本,并使用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];
你需要使用自己的控制绘制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;
上面的代码中,我从How to use multiple font stylings on a single string inside a label?
谢谢大家:) – Machete 2013-03-26 20:24:58
我不能得到明确阐述PLZ你的问题:) – iPatel 2013-03-26 20:03:50
例如75%。我想字体的75“系统32”和字体的%“系统15”中一个标签。 – Machete 2013-03-26 20:06:45
看看这个班可以帮你吗http://past.is/NDe3 – AncAinu 2014-01-23 08:06:24