I'm a newbie to iOS programming and I've tried looking around for answers but somehow can't find the answer to what I assume should be a basic question.
I noticed both is able to do the same thing, I am able to change the label name, set a custom font or system font and change the font weight, font size and stuff. So the question is, if Plain is able to do those, why and when do I use Attributed?
Thanks.
You can understand difference between plain and attributed from their name.
Plain is String and as per Apple docs
String
A Unicode string value is a series of characters, such as "Swift",
that forms a collection. Strings in Swift are Unicode correct and
locale insensitive, and are designed to be efficient
And as for Attributed String allow you to format ranges of text with custom colors, fonts, underlines, shadows, and more Apple Docs
NSAttributedString
A string that has associated attributes (such as visual style,
hyperlinks, or accessibility data) for portions of its text.An
NSAttributedString object manages character strings and associated
sets of attributes (for example, font and kerning) that apply to
individual characters or ranges of characters in the string. An
association of characters and their attributes is called an attributed
string. The cluster’s two public classes, NSAttributedString and
NSMutableAttributedString, declare the programmatic interface for
read-only attributed strings and modifiable attributed strings,
respectively.
Unless you have a specific reason to use AttributedString, you can pretty much forget that it exists.
Here is a good Medium Article on attributed String
Related
I have a .txt file, and I have some words wrapped with "%%" on both sides (e.g. "My Regular text, and %%my Bold text%%"). I want it to return "My Regular text, andmy Bold text"
How would I set ALL wrapped text to be bold?
A string of text does not in itself contain any formatting information. No fonts, no weights, no styles, etc...
What you are looking for is NSAttributedString. Using this you can then add attributes to substrings to add bolder text.
I believe it now supports markdown too so you can use double asterisks like this to make text bold without having to do much yourself.
I’ll find a link.
A popular iOS framework for Swift is Down https://github.com/iwasrobbed/Down
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.
Is it possible to force a UIFont to be monospaced?
Specifically I'm using a font which does not contain monospaced numbers (tabular numerals). Creating a modified font object which is monospaced and adding that font to the numeric segments of attributed strings would work great.
Another solution may be to add custom attributes to an attributed string and modify what is handling the layout of the text to use a fixed size for glyphs in particular ranges.
Things that Haven't Worked:
There are a number of questions that propose solving similar problems by overriding -drawRect: or -drawTextInRect: on a UILabel (see: Is it possible to alter the letter-spacing/kerning of a font with Cocoa Touch?). This seems like an insane solution and would be prohibitively complex if a string mixes monospaced and not-monospaced fonts.
There are also number of questions which suggest, specifically in regards to numerals, creating a font with a font descriptor with certain font features enabled. (see Displaying proportionally spaced numbers (instead of monospace / tabular) on iOS). This seems only to work in fonts which include these features. The font I'm using does not include these features and they have no effect. The font features can be checked by using something like NSLog(#"%#", CTFontCopyFeatures ( ( __bridge CTFontRef ) myFont ));
I have an application that reads some strings and displays them in some UILabels, but these strings are not from one language so I need to be able to set the alignment of the label to right if the string is from a RTL language, and to left otherwise.
So is there a direct way to accomplish that, and if not what's the best way to get the language's direction from a string.
I'm thinking of checking the first X characters of the string and see what is the larger percentage of the LTR and RTL characters to set the alignment of the label according to it. Is this a good solution and if so where could I find a table that shows what are the unicodes that belongs to LTR characters and what those for RTL.
In programs with appropriate RTL support, you do not need to set the direction for any simple text like just words and spaces. Programs are expected to apply the properties of the characters themselves, as defined in character code standards.
If a piece of text to be rendered as a unit contains punctuation characters and/or a mix of LTR and RTL letters, the direction should be set either with control characters or at a higher protocol level. I don’t think you should do heuristics on this; rather, the overall direction should be a property of the text item, decided by the information provider (e.g., author, translator) and stored along with the text. In software localization, a set of localized values for texts normally has one language and the overall direction should be stored as part of the data set or inferred from the language.
Note that writing direction should also affect the overall layout direction (e.g., table columns should run from right to left for RTL languages) and the text alignment.
Is that possible?
If not with CATextLAyer, then how can I do that?
You can use NSMutableAttributedString in CATextLayer. You can specify different attributes like color, font to different parts of string.
The solution was to break the word to letters and combine it back with 3 different text layers.
I will try to post the function i have build for others who needs it.