【发布时间】:2017-12-16 10:34:34
【问题描述】:
我在NSMutableAttributedString 中使用NSTextAttachment 向UILabel 添加图标,如下所示:
//Setting up icon
let moneyIcon = NSTextAttachment()
moneyIcon.image = UIImage(named: "MoneyIcon")
let moneyIconString = NSAttributedString(attachment: moneyIcon)
//Setting up text
let balanceString = NSMutableAttributedString(string: " 1,702,200")
balanceString.insert(moneyIconString, at: 0)
//Adding string to label
self.attributedText = balanceString
self.sizeToFit()
但由于某种原因,图标没有垂直对齐
有人知道如何对齐吗?
谢谢!
【问题讨论】:
-
Check out this question,大约是两种不同大小的字体,但应该能给你一个想法。
-
@the4kman 不幸的是它没有帮助。您发送的内容是关于字体大小的,而图标当然没有字体大小……还有其他想法吗?
-
你到底想做什么?放上“文字”?然后将
NSBaselineOffsetAttributeName用于balanceString。还是放下图片?然后把moneyIcon的bounds改成:stackoverflow.com/questions/26105803/…
标签: ios swift uilabel nsattributedstring