Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 动态增加uilabel的宽度_Iphone - Fatal编程技术网

Iphone 动态增加uilabel的宽度

Iphone 动态增加uilabel的宽度,iphone,Iphone,我想根据要显示的文本长度动态地为标签指定宽度。标签是在uiview上添加的。我使用以下代码,但我仍然得到较短的宽度标签 - (id)initWithFrame:(CGRect)frame OrangeText:(NSString*)orange WhiteText:(NSString*)white { if ((self = [super initWithFrame:frame])) { CGSize textSize = [orange sizeWithFont:[UIFont sys

我想根据要显示的文本长度动态地为标签指定宽度。标签是在uiview上添加的。我使用以下代码,但我仍然得到较短的宽度标签

- (id)initWithFrame:(CGRect)frame OrangeText:(NSString*)orange WhiteText:(NSString*)white {
if ((self = [super initWithFrame:frame])) {
    CGSize textSize = [orange sizeWithFont:[UIFont systemFontOfSize:14]];
    OrangeLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, textSize.width, textSize.height+2)];
    OrangeLabel.text = orange;
    OrangeLabel.backgroundColor = [UIColor clearColor];
    OrangeLabel.textColor = [UIColor orangeColor];
    [self addSubview:OrangeLabel];

    WhiteLabel = [[UILabel alloc] init];
    CGSize whiteTextSize = [white sizeWithFont:[UIFont systemFontOfSize:14]];
    WhiteLabel.frame = CGRectMake(OrangeLabel.frame.size.width+35, 5, whiteTextSize.width, whiteTextSize.height);
    WhiteLabel.text = white;
    WhiteLabel.backgroundColor = [UIColor clearColor];
    WhiteLabel.textColor = [UIColor whiteColor];
    [self addSubview:WhiteLabel];        // Initialization code
}
return self;

}

我想您正在寻找这种方法


[myLabel sizeToFit];


这将调整标签框的大小以适应其内容。

我想您正在寻找这种方法


[myLabel sizeToFit];

这将调整标签框的大小以适应其内容