9

enter image description here

Left one on the picture is the date and time from Apple and one is my application one. As you may see the text of my application appears lower than the Apple one. Which doesn't look pretty. How can this be resolved?

self.statusBarItem.image                    = nil
self.statusBarItem.button?.imagePosition    = .NoImage
self.statusBarItem.button?.title            = "Sun 5 Jun  13:35"
Tarvo Mäesepp
  • 4,477
  • 3
  • 44
  • 92
Mark
  • 16,906
  • 20
  • 84
  • 117
  • I am not using constraints. The code you see is the code I used except for this at the top 'let statusBarItem = NSStatusBar.systemStatusBar().statusItemWithLength(NSVariableStatusItemLength)' – Mark Jun 19 '16 at 13:38
  • I was able to align with the following code: "button.frame = CGRectMake(0.0, 0.5, button.frame.width, button.frame.height)" to manually correct the offset, but perhaps someone has a better explanation. – Mark Jun 19 '16 at 13:46
  • @Mark can you please share the code over GitHub or anything similar so that i can have a look at it? – Jeet Jun 19 '16 at 13:58
  • Or Why not embed in a navigation controller? – Mtoklitz113 Jun 19 '16 at 20:06
  • All don't work because it needs to be done on the status item button. The offset actually is a good enough work a round for me. I am sorry to have troubled you all. I am trying to delete the question but it has not been succesfull yet. – Mark Jun 20 '16 at 21:46
  • @Mark You can add flag. – Tarvo Mäesepp Jun 23 '16 at 13:30
  • Thanks I flagged it. – Mark Jun 24 '16 at 11:44

2 Answers2

2

You could do it in offset. You cannot do it like this since it needs to be done on the status item button bar.

button.frame = CGRectMake(0.0, 0.5, button.frame.width, button.frame.height)
Mark
  • 16,906
  • 20
  • 84
  • 117
Tarvo Mäesepp
  • 4,477
  • 3
  • 44
  • 92
0

I wanted to make a quick addition to this. While the accepted answer does indeed work based on the context of the question. It will not work if you have an image, as this moves everything (image and text). I have found for some reason, the image is vertically centered, but the text is not.

What you actually want to do in that case is set NSAttributedString.Key.baselineOffset, where you derive the baseline value as follows

    var displayScale: CGFloat = 1.0
    let pstyle = NSMutableParagraphStyle()
    pstyle.alignment = .center;
    let aStr = NSAttributedString(string: str, attributes: [NSAttributedString.Key.paragraphStyle: pstyle, NSAttributedString.Key.font: font])
    if let main = NSScreen.main {
        displayScale = main.backingScaleFactor
        if displayScale <= 0.0 {
            displayScale = 1.0
        }
    }
    let textHeight = aStr.size().height
    baselineOffset = ((font.ascender - font.descender) - textHeight) / displayScale
    button.attributedTitle =  = NSAttributedString(string: str, attributes: [NSAttributedString.Key.paragraphStyle: pstyle, NSAttributedString.Key.baselineOffset: baselineOffset, NSAttributedString.Key.font: font])

Disclaimers here are that I'm not completely sure about the displayScale part. I theorize this is needed to correct for pixels/points.

I've only added this answer because I was struggling to find a good answer for this. I got my inspiration to do this based on this: Center two fonts with different different sizes vertically in an NSAttributedString

If this technique is wrong, I'd be interested to know so I can fix it.

Mobile Ben
  • 7,121
  • 1
  • 27
  • 43