首页 > 解决方案 > 在 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
}

}

它看起来像这样:
在此处输入图像描述

有没有人有任何想法?

标签: swiftstringuilabel

解决方案


推荐阅读