2013-04-12 39 views
3

我想的UILabel的第一個字母有從別人不同的字體大小,幾個字體大小。需要一些指導如何做到這一點。這個標籤有很多單詞。增加第一個字母的字體大小UILabel.text

這是我曾嘗試:

NSArray * words = [Label.text componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 
NSString *firstLetter = [[words objectAtIndex:0] substringToIndex:1]; 

但被困在增加的NSString的大小。有沒有更好的辦法?我歡迎建議和指導..謝謝..

編輯:

[Label setFont:[UIFont fontWithName:@"Arial" size:12.f]]; 
NSArray * words = [Label.text componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 
UIFont *fontFirst=[UIFont fontWithName:@"Arial" size:15.f]; 
NSDictionary *attrsDictFirst=[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName]; 
NSAttributedString *finalString=[[NSAttributedString alloc] initWithString:[[words objectAtIndex:0] substringToIndex:1] attributes:attrsDictFirst]; 
+0

如果您只需要iOS 6或更高版本,請使用'UILabel attributedText'。如果您需要支持iOS 5或更早版本,那麼您不能在UILabel中使用多個字體。 – rmaddy

+0

我懷疑你必須使用屬性文本。 (或將其分成兩個標籤。) –

回答

5

得到的,例如,這樣的:

enter image description here

這樣說:

NSString* s2 = @"Fourscore and seven years ago, our fathers brought forth " 
    @"upon this continent a new nation, conceived in liberty and dedicated " 
    @"to the proposition that all men are created equal."; 
NSMutableAttributedString* content2 = 
    [[NSMutableAttributedString alloc] 
    initWithString:s2 
    attributes: 
     @{ 
      NSFontAttributeName: 
       [UIFont fontWithName:@"HoeflerText-Black" size:16] 
     }]; 
[content2 setAttributes: 
    @{ 
     NSFontAttributeName:[UIFont fontWithName:@"HoeflerText-Black" size:24] 
    } range:NSMakeRange(0,1)]; 
[content2 addAttributes: 
    @{ 
     NSKernAttributeName:@-4 
    } range:NSMakeRange(0,1)]; 

NSMutableParagraphStyle* para2 = [NSMutableParagraphStyle new]; 
para2.headIndent = 10; 
para2.firstLineHeadIndent = 10; 
para2.tailIndent = -10; 
para2.lineBreakMode = NSLineBreakByWordWrapping; 
para2.alignment = NSTextAlignmentJustified; 
para2.lineHeightMultiple = 1.2; 
para2.hyphenationFactor = 1.0; 
[content2 addAttribute:NSParagraphStyleAttributeName 
       value:para2 range:NSMakeRange(0,1)]; 

然後分配給content2標籤的attributedText

+0

首先感謝您的幫助。可以解釋一下這一行:NSKernAttributeName:@ - 4呢? – lakesh

+0

否則,大寫字母與小寫字母相比很接近。這使得「F」的條與「o」重疊。 – matt

+0

你的回答是正確的。方面的問題:我將一個UILabel添加到滾動視圖,並添加了您的代碼。但它不允許我滾動。這些詞水平出現,並且不會垂直出現... – lakesh

2

需要使用NSAttributedStringiOS6.0以上

得到第一個字母后,把它放到屬性字符串中,改變它的大小。字符串的

中休息設置其他尺寸。

UIFont *font=[UIFont fontWithName:@"Arial" size:12.f]; 
NSDictionary *attrsDict=[NSDictionary dictionaryWithObject:font 
           forKey:NSFontAttributeName]; 
NSAttributedString *attribString=[[NSAttributedString alloc] initWithString:[words[0] substringFromIndex:1] attributes:attrsDict]; 


UIFont *fontFirst=[UIFont fontWithName:@"Arial" size:15.f]; 
NSDictionary *attrsDictFirst=[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName]; 
NSAttributedString *firstString=[[NSAttributedString alloc] initWithString:[attribString subStringToIndex:1] attributes:attrsDictFirst]; 

[attribString replaceCharactersInRange:NSMakeRange(0, 1) withString:firstString]; 
+0

請記住,UILabel只支持iOS 6.0或更高版本中的歸屬文本。 – rmaddy

+0

如何取下字符串的其餘部分? – lakesh

+0

一旦你得到attribString,需要把它賦給標籤吧? – lakesh