【问题标题】:Vertically aligning NSTextAttachment in NSMutableAttributedString在 NSMutableAttributedString 中垂直对齐 NSTextAttachment
【发布时间】:2017-12-16 10:34:34
【问题描述】:

我在NSMutableAttributedString 中使用NSTextAttachmentUILabel 添加图标,如下所示:

//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。还是放下图片?然后把moneyIconbounds改成:stackoverflow.com/questions/26105803/…

标签: ios swift uilabel nsattributedstring


【解决方案1】:

使用NSTextAttachmentbounds 属性。

//Setting up icon
let moneyIcon = NSTextAttachment()
moneyIcon.image = UIImage(named: "MoneyIcon")

let imageSize = moneyIcon.image!.size
moneyIcon.bounds = CGRect(x: CGFloat(0), y: (font.capHeight - imageSize.height) / 2, width: imageSize.width, height: imageSize.height)

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()

【讨论】:

  • 这个工作完美。我在正确设置偏移值时遇到了问题。
  • @FS.O6 这应该是公认的答案。它非常简单,而且很有效。
【解决方案2】:

This answer,即在单个NSAttributedString 中将两种不同大小的字体垂直居中,提到使用基线偏移量来计算字符串的中心。

您可以在使用图像时使用相同的方法:

  1. 从图像的高度中减去字体大小,然后除以 2。

  2. 从值中减去字体的下降(因为字体大小与字体的上升不同)。您特别使用的字体 (Baloo-Regular) 的下降值与标准不同,应除以 2。其他字体(包括 San Fransisco)不需要该修复或需要不同的除数。

此代码涵盖了大多数情况,如果您的字体表现不同,您应该查看the guide for managing texts in Text Kit

// *Setting up icon*

let moneyIcon = NSTextAttachment()

// If you're sure a value is not and will never be nil, you can use "!".
// Otherwise, avoid it.

let moneyImage = UIImage(named: "MoneyIcon")!

moneyIcon.image = moneyImage
let moneyIconString = NSAttributedString(attachment: moneyIcon)

// *Setting up NSAttributedString attributes*

let balanceFontSize: CGFloat = 16

let balanceFont = UIFont(name: "Baloo", size: balanceFontSize)!

let balanceBaselineOffset: CGFloat = {
    let dividend =  moneyImage.size.height - balanceFontSize

    return dividend / 2 - balanceFont.descender / 2
}()

let balanceAttr: [NSAttributedString.Key: Any] = [
    .font: balanceFont,
    .baselineOffset: balanceBaselineOffset
]

// *Setting up text*

let balanceString = NSMutableAttributedString(
    string: " 1,702,200",
    attributes: balanceAttr
)

balanceString.insert(moneyIconString, at: 0)

【讨论】:

  • 尝试使用不同的基线偏移量来获得所需的结果。
  • 请在答案中包含您尝试过的代码。
  • 我已经尝试了您添加的内容。我还尝试使用self.frame.size.height 更改字体大小并将-+ 交换。没有任何效果
  • @FS.O6 你说得对,我的方法并不完美。我稍微调整了代码,还添加了一个 GitHub 项目,您可以在其中试用。
  • 是的。这是“Baloo-Regular”。尺寸:16
猜你喜欢
  • 1970-01-01
  • 2023-01-09
  • 1970-01-01
  • 2016-03-02
  • 2013-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多