在 Objective C 中更改字体样式 -(将标签文本设置为全部大写格式)

发布于 2024-12-27 11:09:54 字数 151 浏览 2 评论 0原文

我想将 UILable 文本字体样式设置为小型大写字母格式,如下图所示。

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

在此处输入图像描述

谢谢。 :)

I want to set the UILable text font style in Small-Caps format like below image.

Please give me the solution for this if anyone know,

enter image description here

Thanks.
:)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

一曲爱恨情仇 2025-01-03 11:09:54

如果我没理解错的话,这就是你想要的吗?

NSString *uppercaseString = [yourString uppercaseString];

If I didn't get you wrong, is this what you want?

NSString *uppercaseString = [yourString uppercaseString];
稍尽春風 2025-01-03 11:09:54

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

Fonts available in iOS don't have "capitalic style". You should add own font or try to create font using function CTFontDescriptorCreateCopyWithFeature.
I think that the simplest way will be to build attributed string (NSAttributedString) with mixed font sizes.

安稳善良 2025-01-03 11:09:54

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

在 iOS 上排版小型大写字母

For iOS 7 custom fonts, the method is described by Anthony Mattox. For system font I do not know a way.

Typesetting a font in small caps on iOS

猫卆 2025-01-03 11:09:54

你可以尝试使用这个

NSString* str=@"mudit";
label.text=[str uppercaseString];

它会给你这样的输出:MUDIT

you can try with this

NSString* str=@"mudit";
label.text=[str uppercaseString];

it will give you the output like this:MUDIT

ι不睡觉的鱼゛ 2025-01-03 11:09:54

要使 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 设置,这应该是足够简单,可以在之后添加。

示例:

在此处输入图像描述

To make the UILabel render as an upper-case string, where the first letter of each word is larger, you can do something like this:

@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

This implementation doesn't handle splitting across multiple-lines or honor the TextAlignment setting, This would be should be simple enough to add afterwards.

Example:

enter image description here

夜光 2025-01-03 11:09:54

试试这个

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

try with this one

    NSString *string = @"askdfjgjksdfgh";
NSString *upString = [string uppercaseString];
NSLog(@"U %@", upString);
一场信仰旅途 2025-01-03 11:09:54

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

你想要做的是有两个标签,一个在第一个较大的字母的开头,一个在后面的剩余单词的标签。

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

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

您在图片中看到的可能是单词的图片,而不是 UILabel。

It is not possible to change the font size or type of a individual letter within a UILabel.

What you want to do is to have 2 labels, one in the begging for the first bigger letter and one right after that for the remaining word.

In order to access the first letter and the rest of the word you may use:

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

What you see in the picture are probably pictures of words and not UILabels.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文