swift - 在 UILabel/UITextView Swift 中的一个单词下设置一个单词
问题描述
我试图了解如何在 UILabel 或 UITextView 中的单词下实现单词的放置。我知道字符串的属性中y轴上有偏移,但x轴上没有。因此,我没有找到解决方案。
以防万一,更改 Y 轴位置的代码。它的缺点还在于它将相邻的单词向右移动。
extension String {
func setSuperscript(text: String) -> NSMutableAttributedString {
let attributedString = NSMutableAttributedString(string: self)
let foundRange = attributedString.mutableString.range(of: text)
let font = UIFont.systemFont(ofSize: 12)
if foundRange.location != NSNotFound {
attributedString.addAttribute(.font, value: font, range: foundRange)
attributedString.addAttribute(.baselineOffset, value: -10, range: foundRange)
attributedString.addAttribute(.foregroundColor, value: UIColor.red, range: foundRange)
}
return attributedString
}
}
它看起来像这样:
有没有人有任何想法?
解决方案
推荐阅读
- google-data-studio - 数据洞察社区连接器错误:数据中的列数与架构中的数字不匹配
- android - 在 Android Oreo 及更高版本中在准确时间安排任务
- php - 如何通过 laravel 中的路由和控制器显示模式?
- android - 应用程序模块化后引用的方法计数增加
- c - fopen 没有返回地址,程序崩溃
- kendo-grid - Kendo MVC Grid Filtering - 考虑多个字段的过滤器
- javascript - 从 Java 脚本/类型脚本中的对象数组中获取属性数组
- java - 为什么我在解析 xml 文件时遇到致命错误?
- java - 在 BBOX 中提取内容流(图像、文本和图形)。并在不丢失任何样式的情况下将其放回新的 PDF 中?
- c - 在重新分配期间重定位中间指针