【问题标题】:Increasing the font size of first letter in UILabel.text增加 UILabel.text 中第一个字母的字体大小
【发布时间】:2013-04-05 06:57:22
【问题描述】:

我希望 UILabel 的第一个字母的字体大小与其他字母不同,大一些。需要一些关于如何做到这一点的指导。标签有很多字。

这是我尝试过的:

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];

【问题讨论】:

  • 如果您只需要 iOS 6 或更高版本,请使用UILabel attributedText。如果您需要支持 iOS 5 或更早版本,那么 UILabel 中的字体不能超过一种。
  • 我怀疑你必须使用属性文本。 (或者把它分成两个标签。)

标签: ios uilabel


【解决方案1】:

例如,得到这个:

这样说:

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

【讨论】:

  • 首先感谢您的帮助。可以解释一下这一行:NSKernAttributeName:@-4 的作用吗?
  • 否则大写字母与小写字母的距离远大于其后。这使得“F”的小节与“o”重叠。
  • 你的答案是正确的。附带问题:我正在向 Scrollview 添加 UILabel,并且添加了您的代码。但它不允许我滚动。单词水平出现,而不是垂直出现...
  • 你有没有把UILabel的行数设置为0?如果你不这样做,它就不会换行。
  • 我的答案中的代码来自我书中的这里:apeth.com/iOSBook/ch23.html#_attributed_strings
【解决方案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];

【讨论】:

  • 请记住,UILabel 仅支持 iOS 6.0 或更高版本中的属性文本。
  • 如何取其余字符串?
  • 一旦你得到attribString,需要把它分配回label吗?
  • @lakesh:我试过没有编译检查。请检查和评论。
  • myLabel.attributedText = finalString
猜你喜欢
  • 2018-07-08
  • 2018-07-16
  • 2015-02-01
  • 1970-01-01
  • 2014-09-17
  • 1970-01-01
  • 1970-01-01
  • 2012-09-12
  • 2013-12-26
相关资源
最近更新 更多