0

I have an NSMutableAttributedString with a + in the first character that I want to lift 2 pts.

I can kern with this: attributedAmountText.addAttribute(NSKernAttributeName, value: 2.0, range: NSRange(location: 0, length: 1))

Is there a way to lift that first character? Sort of like a vertical kern

Edit:

Trying attributedAmountText.addAttribute(NSBaselineOffsetAttributeName, value: 2.0, range: NSRange(location: 0, length: 1)) actually moves the baseline itself rather than the text vertically off the baseline which seems odd since the docs say otherwise:

The value of this attribute is an NSNumber object containing a floating point value indicating the character’s offset from the baseline, in points. The default value is 0.

codejockie
  • 9,020
  • 4
  • 40
  • 46
Zack Shapiro
  • 6,648
  • 17
  • 83
  • 151

1 Answers1

0

Hmmm... A quick test of this code:

class ScalingViewController: UIViewController {

    @IBOutlet weak var theLabel: UILabel!

    @IBOutlet weak var theOtherLabel: UILabel!

    let now = Date()

    let labelA: UILabel = {
        let v = UILabel()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.backgroundColor = UIColor.cyan
        return v
    }()

    let labelB: UILabel = {
        let v = UILabel()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.backgroundColor = UIColor.yellow
        return v
    }()

    let labelC: UILabel = {
        let v = UILabel()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.backgroundColor = UIColor.green
        return v
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(labelA)
        view.addSubview(labelB)
        view.addSubview(labelC)

        labelA.topAnchor.constraint(equalTo: view.topAnchor, constant: 40.0).isActive = true
        labelA.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 8.0).isActive = true

        labelB.topAnchor.constraint(equalTo: labelA.topAnchor, constant: 0.0).isActive = true
        labelB.leadingAnchor.constraint(equalTo: labelA.trailingAnchor, constant: 8.0).isActive = true

        labelC.topAnchor.constraint(equalTo: labelA.topAnchor, constant: 0.0).isActive = true
        labelC.leadingAnchor.constraint(equalTo: labelB.trailingAnchor, constant: 8.0).isActive = true

        var aText = NSMutableAttributedString()
        let baseFont = UIFont.systemFont(ofSize: 30.0, weight: UIFontWeightLight)


        let testString = "Example"
        let tsLength = testString.characters.count


        // Font only
        aText = NSMutableAttributedString()
        aText.append(NSMutableAttributedString(string: testString, attributes: [NSFontAttributeName: baseFont]))

        labelA.attributedText = aText


        // Font + positive Baseline Offset of 1st character
        aText = NSMutableAttributedString()
        aText.append(NSMutableAttributedString(string: testString, attributes: [NSFontAttributeName: baseFont]))
        aText.addAttribute(NSBaselineOffsetAttributeName, value: 10.0, range: NSRange(location: 0, length: 1))

        labelB.attributedText = aText


        // Font + negative Baseline Offset of all but 1st character
        aText = NSMutableAttributedString()
        aText.append(NSMutableAttributedString(string: testString, attributes: [NSFontAttributeName: baseFont]))
        aText.addAttribute(NSBaselineOffsetAttributeName, value: -10.0, range: NSRange(location: 1, length: tsLength - 1))

        labelC.attributedText = aText

    }

}

Gave me this result:

enter image description here

I suppose if I knew enough about all the typography aspects of String attributes this would make sense?

DonMag
  • 69,424
  • 5
  • 50
  • 86