【问题标题】:Font Size and Type not applying with CATextLayer字体大小和类型不适用于 CATextLayer
【发布时间】:2012-12-06 21:01:16
【问题描述】:

我正在尝试修改图层中文本的字体属性,但没有发生。有人可以帮忙吗?请在下面找到代码:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

if (self)
{

    // All HypnosisViews start with a clear background color

    [self setBackgroundColor:[UIColor clearColor]];
    [self setCircleColor:[UIColor lightGrayColor]];


    // Create the new layer object
    boxLayer = [[CATextLayer alloc] init];

    // Give it a size
    [boxLayer setBounds:CGRectMake(0.0, 0.0, 300.0, 85.0)];

    // Give it a location
    [boxLayer setPosition:CGPointMake(160.0, 350.0)];

    // Make half-transparent red the background color for the layer
    UIColor *reddish = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.75];

    // Get CGColor object with the same color values
    CGColorRef cgReddish = [reddish CGColor];
    [boxLayer setBackgroundColor:cgReddish];

    // Make it a sublayer on the view's layer
    [[self layer] addSublayer:boxLayer];

    NSString *text2 = @"You are me.";
    UIFont *font2 = [UIFont fontWithName:@"Times New Roman" size:10.0];
    [text2 sizeWithFont:font2];


    [boxLayer setString:text2];

}
return self;
}

【问题讨论】:

    标签: ios text fonts layer


    【解决方案1】:

    要更改 CATextLayer 的字体/字体大小,您必须为图层的“font”和“fontSize”属性赋值。

    或者您必须使用 NSAttributedString,在这种情况下使用该字符串对象的值。

    您使用的“sizeWithFont”调用是一个 NSString 添加,它只计算并返回一个 CSSize,其中包含您给它的字体的宽度和高度。由于您没有在代码中使用返回的 CGSize,因此它完全没有任何作用。

    参考Apple docs

    【讨论】:

    • 完美 - 如果这是您要查找的内容,请将其标记为正确答案,以便其他人可以看到此问题已解决!
    • 我当然会,但是怎么做呢?我是这个网站的新手。还有关于 CATextLayer:有没有办法使图层角变圆?以及如何为图层内的文本换行?
    • 我还没有问过问题 :-) 但我认为答案下方有一个复选标记,您可以单击它,以便验证答案是正确的。
    • 你看过CALayer的属性了吗?这将向您展示如何拥有圆角(通过cornerRadius 属性)。如果我查看 CATextLayer 的文档,我会说“包装”属性允许您指定需要在需要时断开文本行。
    • 是的,我知道“Wrapped”,但它决定了文本是否被包裹以适应接收者的边界。就像它到达边缘时会破裂一样。这很好。我也期待在文本到达边缘之前随时中断文本。我会继续探索。但已经感谢您的有用意见。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-30
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多