gpt4 book ai didi

ios - UILabel 中的尺寸和线条问题

转载 作者:行者123 更新时间:2023-11-29 02:44:45 25 4
gpt4 key购买 nike

我有一个小问题。我开发了一个自动填充从 SQLite 数据库获取的数据集的应用程序。这些数据是通过动态方式绘制的,在数据​​之间我有一个框架,我可以在其中动态插入标签。我永远不知道这些框架内标签的确切数量,因为我从数据库的不同表中获取数据。我遇到的问题是标签中的文本没有填满标签的宽度。我尝试使用 label.linebreakmode 但仍然中断。我发布代码:

我有很多从以前的代码中提取的对象,比如 widthFormatowidthImageFormato

if([tipoVino length]!=0){
UILabel *lblFormato = [[UILabel alloc] init];

labelX = ((widthFormato-widthImageFormatos) / 2)+10;

CGRect labelFrame = lblFormato.frame;
labelFrame.size.width = widthImageFormatos;
labelFrame.origin.x = labelX;
labelFrame.origin.y = labelY+25;
lblFormato.frame = labelFrame;
lblFormato.numberOfLines = 0;
lblFormato.lineBreakMode = NSLineBreakByWordWrapping;
[lblFormato setText:[NSString stringWithFormat:@"- %@",tipoVino]];

lblFormato.textColor = [UIColor whiteColor];
labelY = lblFormato.frame.origin.y;
[formatosImageView addSubview:lblFormato];

}

最佳答案

我认为您想创建具有灵活高度的标签(并非所有标签都具有相同的大小),并且必须固定 formatosImageView 的宽度。

针对 iOS7 编辑

我使用的是 iOS7 中弃用的 sizeWithFont,我将其更改为 boundingRectWithSize

试试这个:

UILabel *lblFormato = [[UILabel alloc] init];
lblFormato.numberOfLines = 0;
lblFormato.lineBreakMode = NSLineBreakByWordWrapping;
[lblFormato sizeToFit];

NSString *string = [NSString stringWithFormat:@"- %@", tipoVino];

[lblFormato setText: string];

//Calculate the size of the container of the lblFormato
CGSize size = [string boundingRectWithSize:CGSizeMake(widthImageFormatos, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont fontWithName:@"customFont" size:fontSize]} context:nil];

lblFormato.frame = CGRectMake(labelX, labelY + 25, size.width, size.height);

并且您必须使用以下内容更新 labelY:

labelY = lblFormato.frame.size.height;

也许对你有帮助。

关于ios - UILabel 中的尺寸和线条问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25285622/

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