Inserting regular space between characters in Cocoa text drawing - cocoa

Which is the font style used in NSFont to create alignment/space between characters?
I tried doing with this code, but not able to align the characters.
NSFont *font=[NSFont fontWithName:#"TimesNewRomanPSMT" size:15.0 ];
[textview setFont: font];
Please specify the font style name which suits the above mentioned problem.

What about trying fontspace and pick the font you want.
And if you are looking to adjust the space between characters in a UILabel you can use:
#property(nonatomic) BOOL adjustsLetterSpacingToFitWidth;
Edit: Above code for ios.

Related

How to shrink text to fit when single words are too long for a UILabel width in Xcode

I have a square UILabel that will show one word only. However, when that word is too long, it cuts off the word, instead of shrinking it down to fit.
How do I get the UILabel to shrink to fit a single word?
Thanks.
You can used the AutoShrink property of UILabel. Please find the below screenshot for the same.
Select the AutoShrink property.
Click on the Minimum Font Size option
Adjust any value depend on your requirement once the text will shrink.
Please refer the below GIF represenation.
Hope it works for you!!!

How can I determine the minimum NSRect for drawing an attributed string?

I cannot seem to find this and hope it is a FAQ somewhere.
How can I get the minimum NSRect needed to draw an NSAttributed string on OS X?
I am doing an overlay borderless window, but I want to constrain the size based on what is needed to display the string.
use NSTextContainer and NSLayoutManager.
More from Apple here:
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/TextLayout/Tasks/StringHeight.html

Hit-testing text in Cocoa (Mac)?

Is there a way for hit-testing text? That is for example if I draw text "Hello graph!"" on the screen, within a bounding box, how can I tell the dimension and position of these blank rectangles (blank spaces):
The blank rectangle above the letter "e"
The blank rectangle that spans from above the letter "o" up to above the letter "p"
The blank rectangle below the letter "H" up to the space before "g"
etc, you get the idea.
In other words how to find out which areas occupied by actual text and which areas that are really blank.
The platform (graphics library) is Cocoa on Mac OS X, the character set is Unicode (which probably rules out raw CoreGraphics API?), and the font can be anything that Cocoa can use.
Thanks in advance
You can get the rect of a character in a NSTextView like this:
//Get the range of characters, which may be different than the range of glyphs
NSRange range = [[textView layoutManager]glyphRangeForCharacterRange:NSMakeRange(charIndex, 0) actualCharacterRange:NULL]
//Get the rect of the character
NSRect rect = [[textView layoutManager]boundingRectForGlyphRange:range inTextContainer:[textView textContainer]];
Then, get the NSGlyph that you want:
NSGlyph glyph = [[textView layoutManager]glyphAtIndex:range.location];
And draw it in an NSBezierPath:
NSBezierPath *path = [NSBezierPath bezierPath];
[path appendBezierPathWithGlyph:glyph inFont:myFavoriteFont];
Next, query the path for its bounds:
NSRect actualRect = [path bounds];
You can then compare those two rectangles.
Have a look at the NSLayoutManager Class Reference, the Text System Overview, and the Text Layout Programming Guide.
You could draw the text over fully-transparent bitmap (via CGBitmapContext), then check that bitmap if a particular point is transparent or not.
If you need a list of rectangles (that is called region in 2d graphics), you'll have to implement that yourself or google for a library, as CoreGraphics and Cocoa don't have regions (at least documented).

How do I create an NSFont that is both Bold and Italic?

This is a beginner's question about font handling in Cocoa. I have a font family, e.g. Verdana, that has the following typefaces: Regular, Bold, Italic, and Bold Italic. I know those typefaces exist since they are available in the Fonts panel.
This works:
NSFont *regular = [NSFont fontWithName:#"Verdana" size:75];
NSFont *bold = [NSFont fontWithName:#"Verdana-Bold" size:75];
NSFont *italic = [NSFont fontWithName:#"Verdana-Italic" size:75];
This does not work:
NSFont *boldItalic = [NSFont fontWithName:#"Verdana-Bold Italic" size:75];
What is the simplest way to get the Bold Italic version of a given font family?
This works:
NSFontManager *fontManager = [NSFontManager sharedFontManager];
NSFont *boldItalic = [fontManager fontWithFamily:#"Verdana"
traits:NSBoldFontMask|NSItalicFontMask
weight:0
size:75];
See NSFontManager and -convertFont:toHaveTrait:
For more detailed information, I would suggest reading the Font Handling Guide and specifically the section titled Converting Fonts Manually.
Note, that the font you are using needs to have some version of it with the traits you are asking for, otherwise, you will get back a font, but without the requested trait.
If, eventually, you are seeking to add an italic trait to a font that doesn't have one, check out:
How do I get Lucida Grande italic into my application?
Verdana-BoldItalic.
(The actual name of the bold-italic variant of the font depends on the family, and some font doesn't have bold-italic, Use a NSFontDescriptor with -fontDescriptorWithSymbolicTraits: to get the exact bold italic font.)

Height of NSTextView with one line?

I want to programatically create an NSTextView. How can I determine the correct frame height so that the view displays one line of text in the current default font?
The NSFont class has a method that can give you the size of a rectangle that would enclose a specific attributed string. Get the font used by your text view, create a string that serves as a reasonable example of what will be in the text view, and use that to inform your frame height. (The frame height will need to be some number of points larger than the actual rectangle the string would be displayed in.)
Alternately, you can get the various metrics from the font and attempt to calculate a reasonable frame from that. That might or might not work; for example, a font like Apple Chancery has a huge amount of variation depending on the glyphs that are being rendered, where they are in a word, and so on; I don't know that you can calculate what the needed size would be in advance without knowing exactly what you were going to render.
It would be more normal to be using an NSTextField than an NSTextView for a single line of text.
With NSTextField, just do the following:
[textField setFont:myFont];
[textField sizeToFit];
Oh, and there is no built-in 'current default font'. If an application has such a concept, it needs to track it itself. The font panel doesn't read or write to anything global, it's used to operate on specific text objects.

Resources