对于具有行间距和多种颜色的单行文本,UILabel 大小不正确

2024-03-26

我很确定这实际上是一个 UIKit 错误,但想获得一些输入来看看我是否在这里遗漏了一些愚蠢的东西。

这是我的代码:

// single line with modified line spacing and 2 colors - broken, line spacing is added to the bottom!
UILabel *brokenLabel = [[UILabel alloc] init];
brokenLabel.backgroundColor = [UIColor greenColor];

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Just some text"];

[attributedString addAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} range:[attributedString.string rangeOfString:@"text"]];

attributedString = attributedStringFromAttributedStringWithLineSpacing(attributedString, 20, NSTextAlignmentCenter);

brokenLabel.attributedText = attributedString;
[brokenLabel sizeToFit];
brokenLabel.frame = CGRectOffset(brokenLabel.frame, 50, 100);
[self.view addSubview:brokenLabel];
// end

// single line with modified line spacing and 1 color - correct
UILabel *workingLabel = [[UILabel alloc] init];
workingLabel.backgroundColor = [UIColor greenColor];

attributedString = [[NSMutableAttributedString alloc] initWithString:@"Just some text"];

attributedString = attributedStringFromAttributedStringWithLineSpacing(attributedString, 20, NSTextAlignmentCenter);

workingLabel.attributedText = attributedString;
[workingLabel sizeToFit];
workingLabel.frame = CGRectOffset(workingLabel.frame, 200, 100);
[self.view addSubview:workingLabel];
//end

// multiple lines with modified line spacing and 1 color - correct
UILabel *workingLabel2 = [[UILabel alloc] init];
workingLabel2.frame = CGRectMake(0, 0, 100, 0);
workingLabel2.numberOfLines = 0;
workingLabel2.backgroundColor = [UIColor greenColor];

attributedString = [[NSMutableAttributedString alloc] initWithString:@"Just some text"];

attributedString = attributedStringFromAttributedStringWithLineSpacing(attributedString, 20, NSTextAlignmentCenter);

workingLabel2.attributedText = attributedString;
[workingLabel2 sizeToFit];
workingLabel2.frame = CGRectOffset(workingLabel2.frame, 50, 300);
[self.view addSubview:workingLabel2];
//end

// multiple lines with modified line spacing and 2 color - correct
UILabel *workingLabel3 = [[UILabel alloc] init];
workingLabel3.frame = CGRectMake(0, 0, 100, 0);
workingLabel3.numberOfLines = 0;
workingLabel3.backgroundColor = [UIColor greenColor];

attributedString = [[NSMutableAttributedString alloc] initWithString:@"Just some text"];

[attributedString addAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} range:[attributedString.string rangeOfString:@"text"]];

attributedString = attributedStringFromAttributedStringWithLineSpacing(attributedString, 20, NSTextAlignmentCenter);

workingLabel3.attributedText = attributedString;
[workingLabel3 sizeToFit];
workingLabel3.frame = CGRectOffset(workingLabel3.frame, 200, 300);
[self.view addSubview:workingLabel3];

以及一个简单的便利功能来改变lineSpacing属性字符串的:

NSMutableAttributedString *attributedStringFromAttributedStringWithLineSpacing(NSAttributedString *string, CGFloat lineSpacing, NSTextAlignment textAlignment)
{
    NSMutableAttributedString *mutable = string.mutableCopy;
    
    NSMutableParagraphStyle *par = [NSMutableParagraphStyle new];
    par.alignment = textAlignment;
    par.lineSpacing = lineSpacing;
    [mutable addAttributes:@{NSParagraphStyleAttributeName : par} range:NSMakeRange(0, mutable.length)];
    return mutable;
}

然而,这就是它的样子

正如您所看到的,第一个标签的高度太大(或者准确地说,它应该是的高度+我的自定义行间距)。当简单地向第一个属性字符串添加另一种颜色时,它会导致sizeToFit通过添加来增加尺寸lineSpacing在它下面。我也尝试使用boundingRectWithSize:直接在字符串上使用方法,同样的问题是可见的。因此,这不是特定于标签大小调整代码的,而是字符串本身的问题。我看不出有任何可能的原因会导致这种情况发生。有人有任何见解吗?


在你的属性字典中添加

 [attrDic setObject:@0 forKey:NSBaselineOffsetAttributeName];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

对于具有行间距和多种颜色的单行文本,UILabel 大小不正确 的相关文章

  • 用强/弱自我打破保留周期

    我读过关于强 弱的帖子self打破保留周期 但我仍然对它们如何工作感到困惑 我理解使用 weak typeof self weakSelf self创建对自我的弱引用 但我对强引用感到困惑 据我了解 强引用是指对self这样它就不会在块结束
  • textFieldDidChangeSelection:在视图更新期间修改状态,这将导致未定义的行为

    这是我的代码 struct CustomTextField UIViewRepresentable var placeholder String Binding var text String func makeUIView context
  • AFNetworking 无法编译

    我完全无法尝试使用 AFNetworking 在模拟器上运行项目 我之前在其他项目中使用过这种依赖关系 所以我不明白这里出了什么问题 首先 我尝试运行项目时出现错误 Undefined symbols for architecture x8
  • UITableView 显示的行数多于 numberOfRowsInSection 中指定的行数:

    我希望我的 tableView 显示 6 行 其中包含文本 在本例中为 示例 据我所知 我有我的numberOfSectionsInTableView and numberOfRowsInSection 设置正确 请参阅下面的示例代码 NS
  • 应用程序因使用私有 API“commentText”而被拒绝

    我的应用程序更新刚刚被拒绝 并显示以下消息 您的应用程序使用或引用以下非公共 API 评论文本 我搜索了 StackOverflow 以了解如何使用 nm 和 otool 来检查我的库中的私有 API 但我无法让它工作 另外 我有一种轻微的
  • iOS UITest:如何找到UITableViewCell的AccessoryView?

    你好我正在学习UITests now 我有个问题 如何检测accessoryView的点击tableViewCell 在UI测试中 下面是我的tableViewCell 我想要检测细节闭合配件视图水龙头 像这样 app tables cel
  • Swift 2.0 中的协议扩展方法调度

    我面临有关协议方法调度的问题 我有一个类层次结构 如下所示 protocol E func test extension E func test print jello class A E class B A func test print
  • iOS 绘制圆圈

    我正在尝试在我的 iOS 应用程序中创建下面的圆圈 我知道如何制作圆圈 但不完全确定如何沿着弧线获取点 它必须是代码而不是图像 下面也是我目前拥有的代码 void drawRect CGRect rect CGPoint point poi
  • 使用 GCD 异步 UITableViewCell 图像加载

    我目前正在尝试加载 Flickr 照片的 UITableView 列表 cs193p iOS 斯坦福大学 作业 5 为了避免 UI 阻塞事件 我将每个单元格的缩略图下载推迟到不同的队列中 但确实将 UI 更新回主队列中 此代码不会异步加载图
  • 如何在 Swift ios 中获取国家/地区列表?

    我已经看到了两个与我类似的问题 但这些问题的答案对我不起作用 我有一个旧项目 其中在一组方括号内手动输入了国家 地区列表 我可以轻松地在我的 pickerView 中使用它 但我想知道是否有更有效的方法来做到这一点 我将在 UIPicker
  • AFNetworking 的 setImageWithURLRequest 在滚动后在错误的单元格中设置图像(iOS、Swift)

    我使用表dequeueReusableCellWithIdentifier and afnetworking uiimageview 我的一些细胞有图像 有些则没有 如果我在加载图像之前滚动表格 成功块会将图像放入重复使用的错误单元格中 例
  • Parse.com 因超出突发限制而拒绝服务

    我使用 Parse 创建了一个 iOS 应用程序 其中使用的是从 Parse com 网站下载的 iOS SDK 为了创建此类应用程序 ApplicationID 和 ClientID 密钥都嵌入在 iOS 应用程序中 并在使用应用程序时从
  • 如何在 Flutter 中为 Button 添加渐变?

    有没有办法改变ElevatedButton背景颜色渐变 如果没有一些小瑕疵或问题 例如缺少涟漪效应 不需要的边框 不尊重主题的内容 上述所有解决方案都无法真正发挥作用minWidth对于按钮 The 下面的解决方案没有上述问题 关键部分是使
  • 如何通过情节提要向导航栏添加后退按钮?

    我现在很困惑 我在网上到处都看到添加自定义后退按钮的教程 但我什至似乎无法启动默认按钮 在我的 MainViewController 中 我有performSegueWithIdentifier 然后在另一端 我希望导航栏在左侧有后退按钮
  • 从 UIImagePickerControllerReferenceURL 加载 UIImage

    我正在使用 UIImagePickerController 来允许用户从图像库中选择图像 然后我想在 sqlite 数据库中启动该文件的位置 以便稍后可以参考它 我一直在谷歌上搜索如何做到这一点 但我的结果相当简短 我知道我可以通过调用委托
  • 如何在 iPhone 应用程序中播放来自服务器的视频 URL

    实际上 我通过使用从服务器获取一个网址XMLParser 我想在我的应用程序中播放这个视频网址 现在我使用了 MPMoviePlayerController 但它不起作用 请帮忙 XML解析器 m id loadXMLByURL NSStr
  • .net MVC 将 MP4 流式传输到 iDevice 问题

    我一直在编写用于提供视频服务的一段代码 但遇到了一些问题 代码如下 public ResumingFileStreamResult GetMP4Video string videoID if User Identity IsAuthenti
  • stringFromDate 始终为 NIL

    我知道这是一个重复的问题 但是在 stackoverflow 和 google 上搜索了许多类似的问题后 没有一个解决方案对我有用 我正在尝试将从数据库收到的日期转换为字符串格式以在 iPhone 应用程序中显示 我正在按以下方式将日期转换
  • VNFaceObservation BoundingBox 在纵向模式下不缩放

    作为参考 这源于一个问题视觉API 我正在努力使用Vision通过a检测图像中的人脸VNDetectFaceRectanglesRequest 它在确定图像中正确的人脸数量并提供boundingBox对于每张脸 我的麻烦是由于我UIImag
  • 是否可以开发一个可以跟踪其他应用程序使用情况的应用程序?

    我很想知道是否有可能为 iOS 开发一款类似的应用程序 就像本周刚刚发布的 Android 版应用程序一样 https play google com store apps details id com appuccino Frequenc

随机推荐