【问题标题】:AutoLayout and CATextLayer iOS7自动布局和 CATextLayer iOS7
【发布时间】:2013-10-29 14:44:03
【问题描述】:

我正在尝试使用 CATextLayer 为文本更改设置动画。

   intValueLayer = [CATextLayer layer];
   [intValueLayer setFrame:self.lblIntPart.bounds];
   [[self.lblIntPart layer] addSublayer:intValueLayer];

我在视图上使用 AutoLayout,我知道 setFrame 方法无法正常工作。在我的情况下,CATextLayer 的框架小于 self.lblIntPart 。那么,如何向我的 TextLayer 添加约束或使用与 self.llbIntPart 相同的框架显示它。

谢谢!

所有方法看起来像:

   intValueLayer = [CATextLayer layer];
    [intValueLayer setFrame:self.lblIntPart.bounds];
    [[self.lblIntPart layer] addSublayer:intValueLayer];

    [intValueLayer setAlignmentMode:@"center"];
    [intValueLayer setForegroundColor:self.category.colour.colour.CGColor];
    [intValueLayer setFont:CFBridgingRetain(@"DilleniaUPCBold")];
    [intValueLayer setFontSize:96.0f * scale];
    intValueLayer.contentsScale = [UIScreen mainScreen].scale;

    [intValueLayer setString:@"0"];

【问题讨论】:

  • NSLog的输出是什么(@"%@",NSStringFromCGRect(intValueLayer.frame)); NSLog(@"%@",NSStringFromCGRect(self.lblIntPart.frame)); NSLog(@"%@",NSStringFromCGRect(self.lblIntPart.bounds));

标签: ios objective-c ios7 interface-builder autolayout


【解决方案1】:

在尝试重现您的问题后,这是我的理解。

可能您的 Label 框架很大,并且默认 CATextLayer 字体属性不完全适合框架。

尝试调整 CATextLayer 的属性。

[intValueLayer setFont:(__bridge CFTypeRef)([UIFont systemFontOfSize:17])];
[intValueLayer setFontSize:17];

如果您按原样使用标签,它会匹配属性。

【讨论】:

  • 我使用了这种方法,但是您的代码给了我一个想法,即在通过约束编辑宽度标签后,新的字体大小 [intValueLayer setFontSize:96.0f * scale]; 是错误的。对于测试,我使用了另一个比例,现在一切看起来都很好。谢谢
猜你喜欢
  • 2014-11-23
  • 2015-02-26
  • 2013-12-17
  • 2013-12-16
  • 1970-01-01
  • 2015-02-10
  • 1970-01-01
  • 2013-10-08
  • 2014-06-06
相关资源
最近更新 更多