如何在UIlabel中将上标%字符显示为字符串?我知道%unicode中不存在%作为上标,但有没有办法可以将%显示为上标而不是使用html标签?
此代码在 iOS 12 及更低版本上运行良好,并且在运行 iOS 13 时会出现问题。目标是将行高间距删除为 0,以便我的标签在文本之间减少空间量。我在集合视图单元格中有两个标签,当我将单元格从屏幕上滚动然后向下滚动时,标签文本现在被“切断”了。这不是我在以前版本的 iOS 中提到的情况。任何解决此问题的帮助都将是惊人的。提前致谢。
这是我的代码:
extension: UILabel {
func addLineSpacing(spacing: CGFloat) {
guard let text = text else { return }
let originalText = NSMutableAttributedString(string: text)
let style = NSMutableParagraphStyle()
let lineHeight = font.pointSize - font.ascender + font.capHeight
let offset = font.capHeight - font.ascender
let range = NSRange(location: 0, length: text.count)
style.maximumLineHeight = lineHeight
style.minimumLineHeight = lineHeight
style.alignment = .center
originalText.addAttribute(.paragraphStyle, value: style, range: range)
originalText.addAttribute(.baselineOffset, value: offset, range: range)
attributedText = originalText
} …Run Code Online (Sandbox Code Playgroud) 这就是我想要实现的目标。
我使用了一个,获取字符$attributedString的范围并对其应用属性,如下所示:
let str = "$4"
let r1 = str.range(of: "$")!
let n1 = NSRange(r1, in: str)
let atrStr = NSMutableAttributedString(string: str)
atrStr.addAttributes([NSAttributedStringKey.font : UIFont.systemFont(ofSize: 25)], range: n1)
atrStr.addAttributes([NSAttributedStringKey.foregroundColor : UIColor.lightGray], range: n1)
lbl.attributedText = atrStr
Run Code Online (Sandbox Code Playgroud)
但结果是
如何从下到上更改该特定字符的对齐方式?