Poorly rendered text (NSFont) in MacRuby/Cocoa. Any advice? - cocoa

I have a small MacRuby app that displays some text inside a NSTextView. I have a method called make_label() that builds an NSTextView with some text and returns it, which I use to add to another NSView via addSubview()
make_label() looks like this:
def make_label( x, y, width, height, color, font_size, text )
label = NSTextView.alloc.initWithFrame( NSMakeRect( x, y, width, height) )
font = NSFont.systemFontOfSize(font_size)
label.setFont( font )
label.insertText( text )
label.setTextColor( color )
label.setDrawsBackground(false)
label.setRichText(true)
label.setEditable(false)
label.setSelectable(false)
label
end
My question is, how come my text looks so poorly rendered? It looks very pixelated and not antialiased at all (from what I can see).
Click here for screenshot
This screenshot shows 2 different sizes of the font, with the same phenomenon.

Cocoa can't draw sub-pixel antialiased text if it's rendering to a context that isn't opaque.
I come from the objective-C side, so I'm guessing a bit, but try setting label.setDrawsBackground(false) to true.

The culprit was setWantsLayer(true) which was called for the view I was drawing in. I deleted that line and I also needed to label.setDrawsBackground(false) as joerick described.

Related

tvOS - UITextField shows white on white when focused

I've created a UITextField on a screen using a custom font (Skia 100 point) and sitting on top of a UIImageView, but otherwise it is quite unremarkable. When it's not focused, the text shows normally, if a bit faint:
When it is focused, however, the entire area renders as a solid white rectangle:
I'd be willing to compromise on the look and feel of this screen if I could get it to render normally when focused, but nothing I've tried has changed it--I always get white-on-white. I read about a similar issue where they said this was due to custom background colors but I have not set a background color. In fact, even if I set a custom text color, it does not seem to take effect--it looks the same.

Calculate size of text (text rectangle) rendered with GDI

I'm trying to create a menu and draw it with GDI.
I have a MENU TITLE, unchangable.
I have an array of items.
If I want the title to be "MAIN MENU" how do I calculate the width and height of the Text? If I set SetBkColor() just before TextOut() to a different color than the HDC Background I see that it's rendered as a Rectangle - but how do I get the size of this rectangle and before writing out the Text on the screen?
Also, I set CreateFont() and SelectObject(HDC, Font) beforehand so I can write "bigger" text for the Menu.
Anyone with suggestions?
Haven't found a working solution for this with native GDI and not using newer Graphics objects in .NET.
The easiest way is probably DrawText() with DT_CALCRECT.
You can also measure text yourself with GetTextExtentPoint32().

Rotation on UILabel causes weird clipping problems

I have UIButtons, for which I want to rotate the title labels like this:
button.titleLabel.transform = CGAffineTransformRotate(button.titleLabel.transform, 12*M_PI/180)
The result looks like this: http://imgur.com/LD6q6 (Layer borders for clarification)
clipsToBounds is off for both the button and its label and I have tinkered with pretty much any combination of Autoresizing, Autoresize-masks, enlarging the frame, etc. but literally nothing changes about the way that label is drawn.
Any idea on how I can enlarge the label enough?

font size bug with CGContextShowTextAtPoint

I have some rather simple code drawing some text into a CGContext. Here is an excerpt (slightly edited).
CGContextSelectFont(context, "Helvetica", 1.5, kCGEncodingMacRoman);
CGContextShowTextAtPoint(context, xpos, ypos, "Hello", 5);
The text renders ok. For some unknown reason, however, the font changes to a smaller size after I click in the view containing the context. Also when I resize the window containing the view the font returns to original size. What is the reason for this?
1.5 points is mighty tiny to begin with. Assuming no other scaling is in effect, that will be one whole pixel and a blurry pixel above it on the screen.
You're probably seeing a bug that I ran into myself: On entry into drawRect:, the current context's text matrix was not the identity matrix. In my case, I saw it contain a scale by 13 on both axes, plus a translation. (Possibly left over from drawing the title bar.) I filed this in Radar as #10585106, in case you want to file your own and cite it.
The solution is to set the text matrix back to the identity transform before trying to draw text.
Once you do that, you'll find that your text will be exactly as tiny as you asked for it to be. You should change your font size to something more reasonable; Core Text contains a function to get the system fonts (from which you can get their sizes), and AppKit's NSFont class contains methods for the same purpose.

How to align text to the top of the button in Matlab GUI?

I have a GUI with big buttons and wouls like to align the text in the button to the top, all I found is "horizontal alignment" property.
Thanks...
You need to access the underlying Java Swing component (I am using FINDJOBJ function):
figure('Menubar','none', 'Position',[200 200 300 200])
h = uicontrol('Style','pushbutton', 'String','click', ...
'Units','normalized', 'Position',[0.3 0.3 0.5 0.5]);
jh = findjobj(h);
jh.setVerticalAlignment( javax.swing.AbstractButton.BOTTOM );
I'm afraid I think you can't do this - text is always vertically aligned at the middle on a uicontrol. The only hacks I can think of that might achieve something like what you want are
Add extra return characters after your main text, so that the real text ends up at the top while the whole text remains centred
(REALLY horrible) Create an image with your text right at the top, and use this with the CData property of the button.

Resources