Vertically align text in Label

Hi! In Xojo, a single line text in a Label is vertically aligned to the top of the Label. In RS it used to be vertically centered. How can I change it back?

I think this is a bug of the Cocoa target and should be reported.
On Windows still aligns at center and the same on Carbon.
Perhaps this is an intentional change, but in this case they should change other targets too.

Reported. <https://xojo.com/issue/27197>

I have the same issue for Cocoa, but only for the combination:

  • TextFont = “SmallSystem”
  • TextSize = 0
  • Height = 20 // same height as for TextFont = “System”

If this is also your setup, then it is not a bug. Interface Builder in XCode behaves the same: if you drag a label onto a window and switch the font of the label from “System” to “SmallSystem”, the text moves to the top. I can post a work-around if necessary - just let me know.

I can post a work-around if necessary - just let me know.

Why not just post it? What’s the point of just writing “hey, I have a workaround for your problem”?

It always puzzles me that people don’t treat the forum and language reference as a source of information that they can contribute to and expand. If you have some useful information then by all means: share it with others.

I wrote: “If this is also your setup, then it is not a bug.” If Tuomo has a different setup, my work-around is not a possible solution for his question.

I get the same behavior for every font/size.
But IMHO the fact Xcode behave like this doesn’t mean this is not a bug.
Added an example project on report.

No need to jump on people, just let them know a work around is always nice to have… as for the language reference, we can contribute to it? How?

The LR is a Wiki, so just ask edit permissions to Xojo, Inc. if you wish to contribute.

OK. I saw login, but no create account and at least looking at the recent history it seems Paul is the person doing the work. So, it isn’t really an open wiki, it is a wiki by permission.

On Cocoa the text is in a NSCell (NSButtonCell, NSTextFieldCell, etc.) and the text is never vertically centered. Of course it is a bug, if Xojo doesn’t behave the same on all platforms. With “it’s not a bug”, I was thinking Cocoa-ish.

Here is my work-around for Labels:

Declare Function frame Lib AppKitLib Selector "frame" (NSView As Ptr) As NSRect Declare Sub setFrame Lib AppKitLib Selector "setFrame:" (NSView As Ptr, rect As NSRect) Dim rect As NSRect = frame(Ptr(Handle)) rect.origin.y = rect.origin.y - 1 // The value -1 is working for TextFont = "SmallSystem", TextSize = 0, Height = 20 setFrame(Ptr(Handle), rect)

and here for Checkboxes:

[code]Declare Function CFBundleGetBundleWithIdentifier Lib CoreFoundationLib (bundleID As CFStringRef) As Ptr
Declare Function CFBundleGetDataPointerForName Lib CoreFoundationLib (bundle As Ptr, symbolName As CFStringRef) As Ptr

Declare Function alloc Lib FoundationLib Selector “alloc” (NSClass As Ptr) As Ptr
Declare Function autorelease Lib FoundationLib Selector “autorelease” (NSObject As Ptr) As Ptr
Declare Function dictionary Lib FoundationLib Selector “dictionary” (NSDictionaryClass As Ptr) As Ptr
Declare Function initWithFloat Lib FoundationLib Selector “initWithFloat:” (NSNumber As Ptr, value As Single) As Ptr
Declare Function initWithString Lib FoundationLib Selector “initWithString:attributes:” (NSAttributedString As Ptr, aString As CFStringRef, NSDictionary As Ptr) As Ptr
Declare Function mutableCopy Lib FoundationLib Selector “mutableCopy” (NSObject As Ptr) As Ptr
Declare Sub setObject Lib FoundationLib Selector “setObject:forKey:” (NSMutableDictionary As Ptr, anObject As Ptr, aKey As Ptr)

Declare Function defaultParagraphStyle Lib AppKitLib Selector “defaultParagraphStyle” (NSParagraphStyleClass As Ptr) As Ptr
Declare Function smallSystemFontSize Lib AppKitLib Selector “smallSystemFontSize” (NSFont As Ptr) As Single
Declare Function systemFontOfSize Lib AppKitLib Selector “systemFontOfSize:” (NSFont As Ptr, fontSize As Single) As Ptr
Declare Sub setAlignment Lib AppKitLib Selector “setAlignment:” (NSMutableParagraphStyle As Ptr, NSTextAlignment As Int32)
Declare Sub setAttributedTitle Lib AppKitLib Selector “setAttributedTitle:” (NSCell As Ptr, NSAttributedString As Ptr)

Const NSLeftTextAlignment = 0

Static attributedStringClass As Ptr = NSClassFromString(“NSAttributedString”)
Static dictionaryClass As Ptr = NSClassFromString(“NSMutableDictionary”)
Static fontClass As Ptr = NSClassFromString(“NSFont”)
Static numberClass As Ptr = NSClassFromString(“NSNumber”)
Static stringClass As Ptr = NSClassFromString(“NSString”)
Static paragraphStyleClass As Ptr = NSClassFromString(“NSParagraphStyle”)

Static bundle As Ptr = CFBundleGetBundleWithIdentifier(“com.apple.AppKit”)
Static baselineOffsetAttributeNamePtr As Ptr = CFBundleGetDataPointerForName(bundle, “NSBaselineOffsetAttributeName”).Ptr
Static fontAttributeNamePtr As Ptr = CFBundleGetDataPointerForName(bundle, “NSFontAttributeName”).Ptr
Static paragraphStyleAttributeNamePtr As Ptr = CFBundleGetDataPointerForName(bundle, “NSParagraphStyleAttributeName”).Ptr

Static defaultParagraphStylePtr As Ptr = defaultParagraphStyle(paragraphStyleClass)

Dim attributesPtr As Ptr = dictionary(dictionaryClass)

Dim fontPtr As Ptr = autorelease(systemFontOfSize(fontClass, smallSystemFontSize(fontClass)))
setObject(attributesPtr, fontPtr, fontAttributeNamePtr)

// The value -1 is working for TextFont = “SmallSystem”, TextSize = 0, Height = 20
Dim baselineOffsetPtr As Ptr = autorelease(initWithFloat(alloc(numberClass), -1))
setObject(attributesPtr, baselineOffsetPtr, baselineOffsetAttributeNamePtr)

Dim paragraphStylePtr As Ptr = autorelease(mutableCopy(defaultParagraphStylePtr))
setAlignment(paragraphStylePtr, NSLeftTextAlignment)
setObject(attributesPtr, paragraphStylePtr, paragraphStyleAttributeNamePtr)

Dim attributedObj As Ptr = autorelease(initWithString(alloc(attributedStringClass), Caption, attributesPtr))
setAttributedTitle(Ptr(Handle), attributedObj)

Top = Top - 2 // Align the whole checkbox to match labels and text fields on the same line[/code]

I tried the label workaround but it doesn’t work here. For whatever size or font.
Also, I really don’t understand how changing the frame by 1 pixel vertically could fix things…
Can you please explain Lukas?

It’s not a fix, it’s a work-around for the specific combination TextFont = “SmallSystem”, TextSize = 0, Height = 20. It works on different iMacs and Macbooks in an in-house application. I just wanted the text of a label to align vertically with the text of its corresponding textfield (and for some reasons Top = Top - 1 didn’t work). Moving the labels around in the IDE was not an option because the Win version would then not show properly aligned anymore.

A better is - since there is no possibility of vertical alignment in an NSCell - to draw the text as NSAttributedString and therein set a different baseline (see the checkbox code above). You could apply this to labels and other controls too. Instead of using -1 for the baseline you could do a calculation based on the height of the frame of the NSControl owning the NSCell (NSTextField owns an NSTextFieldCell, NSButton owns an NSButtonCell, etc.).

[quote=14759:@Markus Winter]> I can post a work-around if necessary - just let me know.

Why not just post it? What’s the point of just writing “hey, I have a workaround for your problem”?

It always puzzles me that people don’t treat the forum and language reference as a source of information that they can contribute to and expand. If you have some useful information then by all means: share it with others.[/quote]

Can we contribute to the LR?

I mean, I’d love to see that. In the past I have extolled the virtues of commented manuals like MySQL’s and PHP’s but I was always under the impression the LR is untouchable.

I’d LOVE for it to be expandable this way.

While I might be wrong, I really don’t think we’re supposed to be able to edit the Wiki, as users. It being a wiki doesn’t mean anyone can go in and write, it means Xojo can go in and edit it.

maybe we can start a conversation inn the forum with a title that are referring to the WIKI for people who might want to include something in the wiki and paul leferve can update the wiki.

yes it is a controlled access wiki so the data within stays at an excellent level. if the wiki was open for anyone, there would be lots of SPAM in there that has nothing to do with Xojo.

And Paul is not the only one with edit rights (I don’t - dont want them either).

[quote=14856:@scott boss]yes it is a controlled access wiki so the data within stays at an excellent level. if the wiki was open for anyone, there would be lots of SPAM in there that has nothing to do with Xojo.

And Paul is not the only one with edit rights (I don’t - dont want them either).[/quote]

They could add a review-before-public option…:wink: