【问题标题】:Rotate a UIlabel to make it vertical旋转 UIlabel 使其垂直
【发布时间】:2014-08-26 10:20:36
【问题描述】:

我有一个 UILabel。我需要以编程方式旋转它。

我有我的水平 UILabel,例如带有框架:x:0, y:0, w: 200, h:80

现在我想旋转标签使其垂直: 我试试这段代码:

 [self setTransform:CGAffineTransformMakeRotation(M_PI_2 / 2)];

我可以看到旋转的包含文本。但我想旋转整个框架:使用我的代码,UILabel 继续具有相同的框架。

【问题讨论】:

  • Warning: If this property is not the identity transform, the value of the frame property is undefined and therefore should be ignored. 来自UIViewtransform 属性的文档

标签: ios objective-c uilabel cgaffinetransform


【解决方案1】:

试试这个工作代码:

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 200, 80)];
//set background color to see if the frame is rotated
[label setBackgroundColor:[UIColor redColor]];
[label setText:@"Text Here"];
label.transform=CGAffineTransformMakeRotation( ( 90 * M_PI ) / 180 );
[self.view addSubview:label];

希望对你有帮助

【讨论】:

    【解决方案2】:

    如果您更喜欢在 xib 或情节提要中直观地设置标签位置和大小,请执行以下操作:

    1. 在界面构建器中设置标签的位置和大小,就像您希望它们在旋转后保留一样。

    2. 旋转标签并重新设置框架:

      -(void)rotateLabel:(UILabel*) label
      {
          CGRect orig = label.frame;
          label.transform=CGAffineTransformMakeRotation(M_PI * 3/2);//270º
          label.frame = orig;
      }
      

    【讨论】:

      【解决方案3】:

      你的标签是一个正方形(w:100,h:100)。所以你的标签已经变形了,但是你看不到变化,因为宽度等于高度。

      【讨论】:

      • ok sorry...我报错了 H 和 W:我们可以假设 UILabel 是 200x80
      猜你喜欢
      • 2021-07-03
      • 1970-01-01
      • 1970-01-01
      • 2012-12-28
      • 1970-01-01
      • 1970-01-01
      • 2015-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多