2013-03-26 60 views
0

我有這個字符串,我想在標籤中顯示:的UILabel使用不同的字體

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

_criticRating.text=criticsScore; 

我要爲\%%小字體和[dict objectForKey:@"critics_score"]];
大字體這可能嗎?

+0

我不能得到明確闡述PLZ你的問題:) – iPatel 2013-03-26 20:03:50

+0

例如75%。我想字體的75「系統32」和字體的%「系統15」中一個標籤。 – Machete 2013-03-26 20:06:45

+0

看看這個班可以幫你嗎http://past.is/NDe3 – AncAinu 2014-01-23 08:06:24

回答

0

閱讀本文post。它大約是NSAttributedString s。我認爲那是你需要的。

0

你將不得不做兩個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]; 
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; 

上面的代碼中,我從How to use multiple font stylings on a single string inside a label?

+0

謝謝大家:) – Machete 2013-03-26 20:24:58

相關問題