【问题标题】:How to add padding to a NSMutableAttributedString?如何向 NSMutableAttributedString 添加填充?
【发布时间】:2019-02-19 12:13:10
【问题描述】:

我有一个标签,它使用 NSMutableAttributedString 将文本写为:

我想要做的是降低星号的顶部填充,以便它的 midY 与下面的“美食”一词相同:

如何使用 NSMutableAttributedString 添加填充?

我知道我可以单独使用星号创建一个单独的标签,并使用带有常量的锚点将其居中,但我想看看如何使用 NSMutableAttributedString 实现这一点

let cuisineLabel: UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false

    let attributedText = NSMutableAttributedString(string: "Cuisine ", attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 17), NSAttributedStringKey.foregroundColor: UIColor.lightGray])

    attributedText.append(NSAttributedString(string: "*", attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 24), NSAttributedStringKey.foregroundColor: UIColor.red]))

    label.attributedText = attributedText

    return label
}()

【问题讨论】:

标签: ios swift uilabel padding nsmutableattributedstring


【解决方案1】:

正如 Code Different 指出的那样,您可以使用 baselineOffset 属性来执行此操作。 -8 的值应该适用于您的情况:

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {
    override func loadView() {
        let view = UIView()
        view.backgroundColor = .white

        self.view = view

        let cuisineLabel: UILabel = {
            let label = UILabel()
            label.translatesAutoresizingMaskIntoConstraints = false
            label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
            let attributedText = NSMutableAttributedString(string: "Cuisine ", attributes: [
                NSAttributedStringKey.font: UIFont.systemFont(ofSize: 17),
                NSAttributedStringKey.foregroundColor: UIColor.lightGray])

            attributedText.append(NSAttributedString(string: "*", attributes: [
                NSAttributedStringKey.font: UIFont.systemFont(ofSize: 24),
                NSAttributedStringKey.baselineOffset: -8,
                NSAttributedStringKey.foregroundColor: UIColor.red]))

            label.attributedText = attributedText

            return label
        }()

        view.addSubview(cuisineLabel)

    }
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

如果由于新基线而导致行高偏移混乱,并且您正在使用多行标签,请尝试使用lineHeightMultiple

let lineStyle = NSParagraphStyle()
lineStyle.lineHeightMultiple = 0.8

...

NSAttributedStringKey.paragraphStyle = style

如果不是(并且您使用的是多个标签堆叠在一起),那么您可能只需要调整系列中每个标签的框架以进行补偿。

【讨论】:

  • 我使用 Sweeper 和 rmaddy 建议使用不同大小的星号和 unicode 字符来保持大小为 17。感谢您的帮助。顺便说一句,我支持你。
  • 我遇到的唯一问题是尝试设置lineHeightMultiple 时。 let lineStyle = NSParagraphStyle() 应该是 let lineStyle = NSMutableParagraphStyle()
  • 很好,@NickKohrn
【解决方案2】:

baselineOffset 属性键用于此目的。

let cuisine = NSMutableAttributedString(string: "Cuisine")
let asterisk = NSAttributedString(string: "*", attributes: [.baselineOffset: -3])
cuisine.append(asterisk)

显然,您必须使用其余文本的字体大小来计算偏移量。这就是为什么我认为使用全角星号 (*) 更容易。

带有全角星号的结果(您可能希望其字体大小与字符串其余部分的字体大小成比例):

【讨论】:

  • 什么是全角星号?
  • @LanceSamaria 这是代码点 U+FF0A:fileformat.info/info/unicode/char/ff0a/index.htm
  • @Sweeper 有没有办法使用字体大小计算偏移量?
  • 尝试分解问题。要计算偏移量,您需要知道星号的高度以及其余文本的高度,您可以通过size(withAttributes:) 获得。 @MiteshDobareeya
  • @Sweeper 我正在使用 $ sing。有没有其他方法可以将 $ 设置为中心?我找到了 $ 的高点和文本的其余部分,但设置到中心无济于事。我划分了剩余空间,但 $ 上涨太多。
猜你喜欢
  • 2012-02-06
  • 2012-04-07
  • 1970-01-01
  • 1970-01-01
  • 2013-01-29
  • 2011-12-15
  • 2019-07-31
  • 1970-01-01
  • 2012-12-24
相关资源
最近更新 更多