gpt4 book ai didi

ios - 向UILabel添加额外的空间以插入图像

转载 作者:行者123 更新时间:2023-12-01 18:23:45 29 4
gpt4 key购买 nike

我在UILabel文本的末尾添加图像时遇到问题。如何检测标签中文本的大小,然后添加额外的空间并在其中插入图像?

最佳答案

我为此写了一些代码:

-(void) labelSizeGetter
{
UILabel * mylabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];

float heigth=25.0;//max height value of label.

NSString *yourString = @"My great text";

mylabel.font =[UIFont fontWithName:@"Helvetica-Bold" size:12];

[mylabel setText:yourString];

CGSize s = [mylabel.text sizeWithFont:[UIFont systemFontOfSize:12]
constrainedToSize:CGSizeMake(MAXFLOAT,heigth)
lineBreakMode:NSLineBreakByWordWrapping];

// s is a size for text of label. just add 10 pix to width to make it more beautiful.

NSLog(@"%@", NSStringFromCGSize(s));

mylabel.frame=CGRectMake(0, 0, s.width+10, s.height);

[self.view addSubview:mylabel];

UIView * myimage=[[UIView alloc] initWithFrame:CGRectMake(mylabel.frame.origin.x+mylabel.frame.size.width , mylabel.frame.origin.y, 30, 30)];//create your images frame according to frame of your label

myimage.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"arti.png"]];

[self.view addSubview:myimage];

}

我希望它会有所帮助

关于ios - 向UILabel添加额外的空间以插入图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15502871/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com