【问题标题】:How to create UILabel in IOS with only code?如何仅使用代码在 IOS 中创建 UILabel?
【发布时间】:2014-09-03 00:32:03
【问题描述】:

我知道如何自定义已经在主情节提要上的 UILabel,但是有没有办法只用代码来调整 UILabel 的大小和位置?

【问题讨论】:

  • 当然可以。到目前为止你有什么尝试?你有什么问题?
  • 我还没有尝试过任何事情。我什至不知道从哪里开始。我是否以 UILabel *firstLabel; 开头
  • 您是否搜索过与您类似的其他问题?我无法想象那里已经没有一个了。
  • 我正在寻找但找不到任何适用于 ios7 的内容

标签: ios uilabel


【解决方案1】:

是的,有。

CGFloat x,y, width, height;
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
myLabel.text = @"foo";
//Now add it to your view hierarchy
[self.view addSubview:myLabel];

我希望这会有所帮助。 我强烈建议你通过这个View Programming Guide

【讨论】:

    【解决方案2】:

    在这里你可以自定义故事板UILabel

    // Create IBOutlet to get UILabel object in your view controller .m file
    @property (weak, nonatomic) IBOutlet UILabel        *storyboardLabel;
    
    // Go to storyboard and link this label to your storyboard label of this view controller
    

    然后将下面的代码放在您要重新定位或调整UILabel 大小的位置。

    // Get rect (position & size) of storyboard label
    CGRect rect = self.storyboardLabel.frame;
    rect.origin.x = 100; // Your preferable left position
    rect.origin.y = 100; // Your preferable top position
    rect.size.width = 200; // Your preferable label width
    rect.size.height = 20; // Your preferable label height
    [self.storyboardLabel setFrame:rect];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-26
      • 1970-01-01
      • 1970-01-01
      • 2020-05-09
      • 1970-01-01
      • 2011-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多