【问题标题】:Programmatically Creating UILabel以编程方式创建 UILabel
【发布时间】:2013-07-22 16:42:05
【问题描述】:

我知道这应该很简单,但我一直在努力以编程方式创建 UILabel 并让它按照我想要的方式运行。

我想要的只是能够创建一个标签,将最大属性设置为高度、宽度和字体大小,然后让文本变小和/或截断文本以容纳长文本字符串。

假设我希望我的标签包含最大宽度为 380、最大高度为 20、最大字体大小为 12 的文本。

这就是我试图创建这样一个标签的方法:

UILabel *fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(91, 15, 0, 0)];
fromLabel.text = [self fromSender];
fromLabel.font = [UIFont fontWithName:ProximaNovaSemibold size:12]; //custom font
fromLabel.numberOfLines = 1;
fromLabel.baselineAdjustment = YES;
fromLabel.adjustsFontSizeToFitWidth = YES;
fromLabel.adjustsLetterSpacingToFitWidth = YES;
fromLabel.size = [fromLabel.text sizeWithFont:fromLabel.font constrainedToSize:CGSizeMake(380, 20) lineBreakMode:NSLineBreakByTruncatingTail];
fromLabel.minimumScaleFactor = MIN_SCALE_FACTOR;
fromLabel.clipsToBounds = YES;
fromLabel.backgroundColor = [UIColor clearColor];
fromLabel.textColor = [UIColor blackColor];
fromLabel.textAlignment = NSTextAlignmentLeft;
[collapsedViewContainer addSubview:fromLabel];

好的,标签出现了,但是文本大于 12 并且高度总是 21!?即使我将高度和文本大小值更改为极端大小,此代码也会创建一个无法调整的固定大小的标签。我唯一能做的似乎是宽度变小了。

我一定是在看一些基本的东西,但我真的不知道如何得到我想要的结果,也不明白为什么我会得到我正在得到的行为。

【问题讨论】:

  • 您希望的最小字体大小是多少?
  • 最小字体大小为 10,并且 MIN_SCALE_FACTOR = 10/12
  • 大小不是只读属性吗?您可能想在这里使用框架。

标签: ios objective-c uilabel


【解决方案1】:

以下是否有效?

UIFont * customFont = [UIFont fontWithName:ProximaNovaSemibold size:12]; //custom font
NSString * text = [self fromSender];

CGSize labelSize = [text sizeWithFont:customFont constrainedToSize:CGSizeMake(380, 20) lineBreakMode:NSLineBreakByTruncatingTail];

UILabel *fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(91, 15, labelSize.width, labelSize.height)];
fromLabel.text = text;
fromLabel.font = customFont;
fromLabel.numberOfLines = 1;
fromLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; // or UIBaselineAdjustmentAlignCenters, or UIBaselineAdjustmentNone
fromLabel.adjustsFontSizeToFitWidth = YES;
fromLabel.adjustsLetterSpacingToFitWidth = YES;
fromLabel.minimumScaleFactor = 10.0f/12.0f;
fromLabel.clipsToBounds = YES;
fromLabel.backgroundColor = [UIColor clearColor];
fromLabel.textColor = [UIColor blackColor];
fromLabel.textAlignment = NSTextAlignmentLeft;
[collapsedViewContainer addSubview:fromLabel];

编辑:我相信您在同时使用 adjustsFontSizeToFitWidthminimumScaleFactor 时可能会遇到问题。前者声明您还需要设置一个minimumFontWidth(否则根据我的测试它可能会缩小到非常不可读的东西),但这已被弃用并被后者取代。

编辑 2:没关系,过时的文档。 adjustsFontSizeToFitWidth 需要 minimumScaleFactor,只要确保不将其 0 作为 minimumScaleFactor 传递(整数除法,10/12 返回 0)。 baselineAdjustment 的值也有细微的变化。

【讨论】:

  • 我认为它会,但使用上面的,标签甚至没有出现。
  • 尝试注释掉adjutsFontSizeToFitWidth
  • 知道了。将 MIN_SCALE_FACTOR 更改为 10.0f/12.0f
  • sizeWithFont:constrainedToSize:lineBreakMode 在 IOS 7.0 中已弃用
  • @Nerkatel 您好,我在您的回答中添加了文档链接。如果你认为我这里越界了,你可以点击回滚。干杯。
【解决方案2】:
UILabel *lbl1 = [[UILabel alloc] init];
/*important--------- */lbl1.textColor = [UIColor blackColor];
[lbl1 setFrame:position];
lbl1.backgroundColor=[UIColor clearColor];
lbl1.textColor=[UIColor whiteColor];
lbl1.userInteractionEnabled=NO;
lbl1.text= @"TEST";
[self.view addSubview:lbl1];

【讨论】:

    【解决方案3】:

    这里是如何以编程方式创建 UILabel..

    1) 将其写入项目的 .h 文件中。

    UILabel *label;
    

    2) 将其写入项目的 .m 文件中。

    label=[[UILabel alloc]initWithFrame:CGRectMake(10, 70, 50, 50)];//Set frame of label in your viewcontroller.
    [label setBackgroundColor:[UIColor lightGrayColor]];//Set background color of label.
    [label setText:@"Label"];//Set text in label.
    [label setTextColor:[UIColor blackColor]];//Set text color in label.
    [label setTextAlignment:NSTextAlignmentCenter];//Set text alignment in label.
    [label setBaselineAdjustment:UIBaselineAdjustmentAlignBaselines];//Set line adjustment.
    [label setLineBreakMode:NSLineBreakByCharWrapping];//Set linebreaking mode..
    [label setNumberOfLines:1];//Set number of lines in label.
    [label.layer setCornerRadius:25.0];//Set corner radius of label to change the shape.
    [label.layer setBorderWidth:2.0f];//Set border width of label.
    [label setClipsToBounds:YES];//Set its to YES for Corner radius to work.
    [label.layer setBorderColor:[UIColor blackColor].CGColor];//Set Border color.
    [self.view addSubview:label];//Add it to the view of your choice. 
    

    【讨论】:

    • 我知道这是旧的,但是如何将框架放置在视图的底部,就在 UIToolBar 的正上方?我喜欢根据字符串的长度自动扩展宽度,或者如果太长则调整字体大小?
    【解决方案4】:
    UILabel *mycoollabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 70, 50, 50)];
    mycoollabel.text=@"I am cool";
    
    // for multiple lines,if text lenght is long use next line
    mycoollabel.numberOfLines=0;
    [self.View addSubView:mycoollabel];
    

    【讨论】:

    • 这可能会解决问题,但您确实可以添加一些关于它如何/为什么工作的解释。
    【解决方案5】:

    对于迅速

    var label = UILabel(frame: CGRect(x: 0, y: 0, width: 250, height: 50))
    label.textAlignment = .left
    label.text = "This is a Label"
    self.view.addSubview(label)
    

    【讨论】:

      【解决方案6】:
      UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 300, 50)];
      label.backgroundColor = [UIColor clearColor];
      label.textAlignment = NSTextAlignmentCenter;
      label.textColor = [UIColor whiteColor];
      label.numberOfLines = 0;
      label.lineBreakMode = UILineBreakModeWordWrap;
      label.text = @"Your Text";
      [self.view addSubview:label];
      

      【讨论】:

        【解决方案7】:
        In Swift -
        
        var label:UILabel = UILabel(frame: CGRectMake(0, 0, 70, 20))
        label.center = CGPointMake(50, 70)
        label.textAlignment = NSTextAlignment.Center
        label.text = "message"
        label.textColor = UIColor.blackColor()
        self.view.addSubview(label)
        

        【讨论】:

          【解决方案8】:

          斯威夫特 3:

          let label = UILabel(frame: CGRect(x:0,y: 0,width: 250,height: 50))
          label.textAlignment = .center
          label.textColor = .white
          label.font = UIFont(name: "Avenir-Light", size: 15.0)
          label.text = "This is a Label"
          self.view.addSubview(label)
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-07-27
            • 2012-08-15
            • 2012-07-03
            • 2015-06-09
            相关资源
            最近更新 更多