【问题标题】:Change the Font Style - (Set Lable text in All-Caps format ) in Objective C在Objective C中更改字体样式 - (设置全大写格式的标签文本)
【发布时间】:2012-02-11 06:24:03
【问题描述】:

我想将 UILable 文本字体样式设置为 Small-Caps 格式,如下图所示。

如果有人知道,请给我解决方案,

谢谢。 :)

【问题讨论】:

  • 我想要图片中显示的字体样式,如标题“完成日期”、“所用时间”和“分数”。
  • 在你的 xcode 中添加字体文件
  • 字体是 Helvetica Neue Bold,我可以完美地获得字体,但问题是我无法获得相同的样式 - 意味着问题是我无法将文本样式设置为小型大写字母 :(

标签: iphone objective-c uilabel uifont capitalization


【解决方案1】:

对于 iOS 7 自定义字体,该方法由 Anthony Mattox 描述。系统字体我不知道怎么弄。

Typesetting a font in small caps on iOS

【讨论】:

    【解决方案2】:

    要使 UILabel 呈现为大写字符串,其中每个单词的第一个字母较大,您可以执行以下操作:

    @implementation CapitalicTextLabel
    
    - (void)drawRect:(CGRect)rect
    {
    
        // Drawing code
        NSArray* words = [self.text componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetCharacterSpacing(context, 1);
        CGContextSetFillColorWithColor(context, [self.textColor CGColor]);
        CGAffineTransform myTextTransform = CGAffineTransformScale(CGAffineTransformIdentity, 1.f, -1.f );
        CGContextSetTextMatrix (context, myTextTransform);
    
        CGFloat x = 0;
        float centeredY = (self.font.pointSize + (self.frame.size.height - self.font.pointSize) / 2) - 2;
        CGFloat firstLetterSize = self.font.pointSize * 1.4;
    
        for (NSString* word in words)
        {
            NSString* letter = [[word substringToIndex:1] uppercaseString];
            CGContextSelectFont(context, [self.font.fontName cStringUsingEncoding:NSASCIIStringEncoding], firstLetterSize, kCGEncodingMacRoman);
            CGContextShowTextAtPoint(context, x, centeredY, [letter cStringUsingEncoding:NSASCIIStringEncoding], [letter length]);
            x = CGContextGetTextPosition(context).x;
    
            NSString* restOfWord = [[[word substringFromIndex:1] uppercaseString] stringByAppendingString:@" "];
            CGContextSelectFont(context, [self.font.fontName cStringUsingEncoding:NSASCIIStringEncoding], self.font.pointSize,
                kCGEncodingMacRoman);
            CGContextShowTextAtPoint(context, x, centeredY, [restOfWord cStringUsingEncoding:NSASCIIStringEncoding], [restOfWord length]);
            CGPoint v = CGContextGetTextPosition(context);
            x = CGContextGetTextPosition(context).x;
        }
    
    }
    
    @end
    

    此实现不处理跨多行的拆分或遵守 TextAlignment 设置,这应该足够简单,以便以后添加。

    例子:

    【讨论】:

      【解决方案3】:

      无法更改UILabel 中单个字母的字体大小或类型。

      你想要做的是有 2 个标签,一个在乞求第一个更大的字母,一个在后面的剩余单词。

      为了访问您可以使用的第一个字母和单词的其余部分:

      NSString * word = @"COMPLETED";
      NSString * firstLetter = [word substringToIndex:1];
      NSString * remainingWord = [word substringFromIndex:1];
      

      你在图片中看到的可能是文字图片,而不是UILabels。

      【讨论】:

        【解决方案4】:

        iOS 中可用的字体没有“大写风格”。您应该添加自己的字体或尝试使用函数 CTFontDescriptorCreateCopyWithFeature 创建字体。
        我认为最简单的方法是构建具有混合字体大小的属性字符串(NSAttributedString)。

        【讨论】:

        • 实际上,这是不正确的,至少对于 iOS 6 而言。iOS 附带的字体与 OS X 附带的相同变体,其中很多包含许多 OpenType 功能,例如 small-caps .
        • 当然。在 iOS 上的 Safari 中打开以下页面:w3schools.com/cssref/… iOS 具有与其老大哥 OS X 相同的排版功能;然而,在公共 API 中使用这些功能要困难得多。
        【解决方案5】:

        你可以试试这个

        NSString* str=@"mudit"; label.text=[str 大写字符串];

        它会给你这样的输出:MUDIT

        【讨论】:

          【解决方案6】:

          试试这个

              NSString *string = @"askdfjgjksdfgh";
          NSString *upString = [string uppercaseString];
          NSLog(@"U %@", upString);
          

          【讨论】:

          • 这是一种字体样式,您必须根据您的文字设置字体名称。
          【解决方案7】:

          如果我没有误会你,这就是你想要的吗?

          NSString *uppercaseString = [yourString uppercaseString];
          

          【讨论】:

          • 然后搜索字体网站。如果你要提交到苹果商店,那么你还需要包含字体的许可证,不要忘记。
          • 这不是不同的字体。它是 halvtica-bold 但只是它的样式在 Photo-shop 中更改为 All-Caps 格式,我只想知道它可以在 Objective-C 中更改这种字体样式。 :)
          • 据我所知,在代码中不可能做到这一点。您最好尝试以下一些答案中所述的一些解决方法。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-12-19
          • 2018-11-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多