2013-04-12 37 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