Xcode how to Autoshrink a UITextView? - xcode

I have a Text View box and I want that rectangle to stay the same size while the text inside autoshrinks to fit the box. The reason I need this is because the text is pulled from a plist and the strings are all different lengths. Labels have an Autoshrink feature which is exactly what I want, but I can't figure out a way even through code to get a Text View to do the same thing. Please help!

UILabels can have more than one line of text. You just need to set the following:
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0; // 0 = unlimited number of lines, set to another number to have that as maximum
I'm not entirely sure if this works, but you can then combine this with label.adjustsFontSizeToFitWidth = YES; to have a multi-line label that autoshrinks text.

Related

AMCharts bullets converted to gibberish on small charts

I've searched through the entire AMCharts 4 bullet documentation as well as numerous Google pages, but I am yet to find the answer. When chart is squeezed to a mobile screen, bullet labels above bars get updated to some complete gibberish (as in the example below left). Those are suppose to be various numeric values (example on the right), including some with a decimal point. No percentage or other symbols.
The text actually gets swapped from normal numbers to the 'processed food' as the screen is being resized. Logic dictates that I must be missing some setting that prevents this sort of undesirable behaviour.
Any help is highly appreciated!
The "gibberish" is supposed to be an ellipsis. It's likely that your page isn't encoded to UTF-8 or the font you're using does not have the Unicode character for an ellipsis.
You can either double-check your encoding, or, if you don't want the ellipsis as at all, disable truncate on your labels. Assuming you're using a LabelBullet for your column labels:
var valueLabel = series.bullets.push(new am4charts.LabelBullet());
// ...
valueLabel.label.truncate = false;
For me the only way to get it show properly was setting value as workingValue
var bullet = series.bullets.push(new am4charts.LabelBullet());
bullet.label.text = "{valueY.workingValue.formatNumber('#')}";
bullet.label.truncate = false;

How to scale NSTextView to show rich text in actual size?

The rich text in an NSTextView displays on my screen (27" Mac) a lot smaller than the font size would imply, although it prints correctly, and is the correct size if pasted in to another app (e.g. OpenOffice). TextEdit shows the same behaviour.
The following lines in awakeFromNib fixes this more or less exactly.
[myTextView scaleUnitSquareToSize:NSMakeSize(96.0/72, 96.0/72)];
myTextView.layoutManager.usesScreenFonts = NO;
So it looks as if the screen is using 96 points per inch. If I don't have the 2nd line, the text is slightly squashed up, and monotext mangled. Obviously I shouldn't hard code the scale factor, but where can I find the factor to put there? From [NSScreen mainScreen].deviceDescription ( a dictionary) I get NSDeviceResolution = {72, 72}", so it seems that's not what's being used.
I'm not sure this is wise, but you can get the "real" DPI from the display mode:
CGDirectDisplayID displayID = [window.screen.deviceDescription[#"NSScreenNumber"] unsignedIntValue];
CGSize size = CGDisplayScreenSize(displayID);
CGDisplayModeRef mode = CGDisplayCopyDisplayMode(displayID);
NSSize dpi;
dpi.width = CGDisplayModeGetWidth(mode) * 25.4 / size.width;
dpi.height = CGDisplayModeGetHeight(mode) * 25.4 / size.height;
CGDisplayModeRelease(mode);
[myTextView scaleUnitSquareToSize:NSMakeSize(dpi.width/72, dpi.height/72)];
myTextView.layoutManager.usesScreenFonts = NO;
Some display modes are letterboxed. These will presumably never be used as the general mode for the Mac GUI; they'd only be used by full-screen games. However, if you want your app to handle such a mode, it should account for the fact that the mode size does not correspond to the full screen size in one dimension and the DPI calculation above will be off. I don't think there's a direct way to figure that out. You have to examine all available display modes to see if there's one that's the same in relevant properties (size, pixel encoding, etc.) as the current mode but whose IOKit flags indicate that it's stretched (CGDisplayModeGetIOFlags(mode) & kDisplayModeStretchedFlag != 0) while the current mode is not. In that case, you probably want to assume the pixels are square and pick the smaller of dpi.width and dpi.height to use for both.

How do I adjust the font size of a tetxblock control depending on the length of the text string that it contains?

I have a textblock and I pull items out of a database and I want to make the text fit on one line. How do I programmatically adjust the font size such that the entire line fits on the phone screen?
Thank you.
I ended up using what Jared Bienz linked to.

Adobe Flex 4 text height

I remember in Flex 3 text width/height could be calculated (not the text UI component but the text itself):
new Text().textHeight;
or
new Text().getTextField().measuredHeight;
Does anyone know how can that be done in Flex 4 with its Text Layout Framework now?
Thanks.
I've found an answer myself. If someone has any better ideas - you're welcome.
I use to assume that width of text content is not so important cause each time the size changes the value used below are updated. So I check the height of text (assuming width is fixed) as:
var textHeight: Number = (_text.textLines.length-1) * _text.textFlow.lineHeight + _text.textFlow.fontSize
where _text is s:RichText.
I needed to know whether the text inside did not exceeded the space available, that's why the only thing I need now is to check whether textHeight < _text.height
That's it. Be sure the text is rendered correctly before checking the height (I use to listen to UDPATE_COMPLETE event).

How can I resize an NSTextField to fit the text that it holds?

I'm using an NSTextField to display lines of text that will be printed on a printer with a variable paper size (the printer prints to a spool, which is cut to the appropriate length when the job is done).
I can generate the text no problem, but I need to know how big to make the NSTextField (vertically) so that it exactly contains all of the lines of text. Is there an easy way of going about this?
The text is prepared as an NSAttributedString, and the lines are all of a fixed width (no wrapping), if that helps.
There is a great category on NSString/NSAttributedString by Jerry Krinock which allows you to calculate the height of text based on its width and vice versa:
http://www.sheepsystems.com/sourceCode/sourceStringGeometrics.html
I've used this often and it works very well indeed.

Resources