Means to get detailed font metrics? - ios

I'm puzzling how to divine more font metrics from iOS. I can accomplish my very long single line of text by paging UIViews while scrolling. I can determine where to break the strings between views by iteratively calculating it with sizeToFont, but I need the inter-character spacing (i.e. advance width) to space them accurately.
Any ideas short of full fixed or monospaced fonts? Thanks.

The Core Text class CTFont gives you some access to glyph metrics. But really, you should try to just use Core Text to do your text rendering instead of asking it for measurements and calculating positions yourself.
CTFont Reference
Core Text Programming Guide

Related

Add custom space to UILabel

I've a weird problem that faces me. I've a UILabel contains text of multiple lines and I'm using bullet in the first line "•".
But my problem is that the second line is not aligned with first character after the bullet.
I measured it and found that width of the bullet is a little bit bigger tan width of normal space.
Is there any trick for that?
the issue is concerning font attributes. Get into the difference of monospaced fonts and proportional fonts, then you will know why there's a difference between space and bullet in your font
https://en.wikipedia.org/wiki/Monospaced_font
easy solution and maybe really an option for you is to use a monospaced font.
Okay, I found a powerful answer by using NSParagraphStyle.
https://wingoodharry.wordpress.com/2016/04/10/bullet-point-list-ios-swift/

Change the height of individual characters using core text

I have a font where unfortunately the numbers and letters are different heights. I need to display a reference code which is a mix of letters and numbers and the uneven heights of the characters looks jarring. Is it possible with core text (or another technology on iOS) to render certain characters with a slightly stretched height so that it looks even numbers and letters are displayed together.
E.g i have the string '23Rt59RQ' I need the 2,3,5,9 to be rendered with a larger height.
AFAICT, there's nothing in the CGContext API (which is what you'd want to use for laying out sets of glyphs) which would directly, easily facilitate this.
If it's really very important to use the font you are using, you could make separate calls to CGContextShowGlyphsAtPositions for alphabetic and numeral characters, calling CGContextSetFontSize each time so that the end result ends up matching, but this is a lot of overhead for just drawing text, and will probably result in undesirable performance.
My real advice would be to pick a better font so that this isn't even an issue :)
In the end of used regex to identify the character groups and then created an attributed string varying the font size in the font given in the NSFontAttributeName attribute according to which characters were to be displayed.
Kinda hacky but it had the desired effect.

How to adjust Font Size according text viewable in a TButton.Text using Firemonkey?

I am using buttons to display product names in a matrix using TGridLayout.
The problem is that commonly Items contains 3 or 4 words and in my language (Portuguese) some words tend to be long.
I would like that somehow I could calculate the size of the font, decreasing it, in order to make all the text show up automatically (of course there is also a decrease limit, anything below 9 or 8 point for the font turn to be difficult to read).
The wordwrap property is turned on to have many lines, and use the most possible space for the text.
I don't know if you're programming for an Android/iOS app, but you can't change the fontsize of a button. I've had the same problem, my solution was to make an abbreviation of the words. And then i put labels above it to explain the abbreviations.
Of course you can adjust the font size of a button:
TButton.TextSettings.Font.Size

How to draw a non-rectangle UILabel with paragraph truncation at the end?

I need to show a body of text similar to the one shown below and limited to the red non-rectangular area. Rob's answer to this question pretty much answers my question as well, but I also need to truncate at tail of the paragraph when the text is too long.
Extra question: is it also possible to set minimum font size similar to UILabel?
Try using NSString::sizeWithFont:constrainedToSize method which is documented here. You can specify the fontsize and maximum size for the text.
You can also calculate the minimum font size using NSString::sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode: method.

How to put large text into multiple UITextViews or UILabels?

I am facing a problem placing a large text into multiple UITextView/UILabel.
Example: Please see the attached picture ,it explains my problem.
assume three boxes are UITextViews or UILabels. Text will get from server as a complete one NSString, Now challenge is to place the text in the boxes (UITextView/UILabel).
could anyone please help me in solving this problem.
A UIWebView might be more suitable for the task of laying out a page. You could then use html with CSS floats to achieve the effect you're looking for.
If you do not want to use UIWebView, I suggest looking at Core Text for layout.
If you have bounding box for those elements, than you can easily implement this logic by iterating usage of method - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode and removing by one word from string till returned size will fit into bounding box for corresponding section, than truncate text to place where you stop removing words and complete with other sections.

Resources