Wrong alignment of label text in macOS
- Dominant language
- Python
- Stars
- 5.4k
- Forks
- 827
- Avg merge
- 15h 39m
- Merged PRs (30d)
- 40
Description
**Describe the bug**
The text in a `toga.Label` is offset by roughly 3 pixels in Cocoa when no background color is set, resulting in a misalignment with other widgets. This issue was introduced in PR #1002 and comes from the behaviour of `NSTextField` when setting `native.drawsBackground = True`: when a background is drawn, even if `backgroundColor = None`, the text is offset to the right so that the left edge of the background color is not flush with the text itself. This makes sense when the background is actually colored but results in an annoying apparent misalignment with other UI elements if `backgroundColor = None`.
I can see two possible ways to fix this:
1. Override `set_background` in `toga_cocoa.Label` to set `native.drawsBackground = False` by default, i.e., when `background_color = None`.
2. Revert PR #1002 and implement `set_background` for each widget individually to avoid possible issues with other existing or future new widgets.
3. Switch to a transparent background Color for all widgets by default. This requires careful testing of each widget and may again have unforeseen consequences.
The first two fixes have the small disadvantage that the Label is drawn with transparent background despite `background_color != TRANSPARENT`, as was the case for the previous implementation as well.
**To Reproduce**
Create a regular `toga.Label` on macOS and compare its left edge to other widgets in the same column.
Example code
```python
import toga
from toga.constants import COLUMN, TRANSPARENT, GREEN
from toga.style import Pack
class LabelTestApp(toga.App):
def startup(self):
self.main_window = toga.MainWindow(title=self.name)
self.label0 = toga.Label('Green background', style=Pack(background_color=GREEN))
self.label1 = toga.Label('No background', style=Pack(background_color=TRANSPARENT))
self.label2 = toga.Label('Default background')
box = toga.Box(
children=[
self.label0,
self.label1,
self.label2,
],
style=Pack(
flex=1,
direction=COLUMN,
padding=10
)
)
self.main_window.content = box
self.main_window.show()
def main():
return LabelTestApp('Labels', 'org.beeware.labels')
if __name__ == '__main__':
app = main()
app.main_loop()
```
**Screenshots**

**Environment:**
- Operating System: macOS Catalina and higher
- Python version: 3.8
- Software versions:
- Toga: 0.3.0.dev23
Contributor guide
Research direction
Start at the toga_cocoa.Label background handling and inspect how native.drawsBackground is set on NSTextField. Reproduce the alignment issue with the provided LabelTestApp on macOS, then verify that labels without a background align with neighboring widgets while colored backgrounds retain their expected behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- macos, python
- Domain
- desktop
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100