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.
Related
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
I'm new to coding and am stuck on formatting what I want to write. I need it formatted with bullet points and spaces. The easiest would be to just screenshot the page but I'd rather be able to change the words if necessary in case of changes later. Below is an example of what a protocol page looks like. That's what I need. Not a big run-on sentence.
How might I be able to accomplish this in Swift?
Here's an example
You have a few options:
• You can use a font symbol, like this: • (or an Emoji) inline with text.
• Use a customized webview to show stylized content.
• Render each line and custom bullet in Quartz.
You could also use the Unicode code point for the bullet character in your string.
myLabel.text = #"\u2022 This is a list item!";
Format UILabel with bullet points?
For long, complicated, pieces of text, you can use HTML and a WKWebView or instead use NSLayoutManager (which may be a bit complicated if you're just getting started), as described in the Text Layout Programming Guide.
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.
I have an iOS app which uses fixed width font label extensively.
After changing to the iOS 7 sdk and build target 6.1, all the label automagically replace occurences of three punctuation marks with an ellipsis character. This breaks a lot of stuff and looks weird, since the ellipsis character is not present in the font I use, and iOS sees fit to use one from a different font.
How do I stop this behaviour?
This is a ligature, and iOS seems to replace them automatically (like fl becomes fl). Seems like there are some options to disable them, see this question: Adjoining "f" and "l" characters
Alternative number three: insert a zero-width space (U+200B) between the dots.
(Posted as an answer per request of the OP)
One way around this is to replace the ASCII periods with a unicode 2024 character ("ONE DOT LEADER"). It looks exactly like a period but should not get converted automatically.
What you could do if this is widespread is to change all your UILabels to a subclass, MyLabel, and intercept messages to set the text, look for three dots, and if found change them to the unicode character above.
Yeah, this is a big PITA but I know of no other workaround.
EDIT
Another idea - find an open source UILabel (there must be at least one) and use it.
Another alternative : the ellipsis is a true character of its own. Why don't you try to add it yourself in your font (with Fontlab, FontForge or Glyphs) at the same width than the other characters?
I'd like to slightly modify the y position of a character in an NSAttributedString. I know I can subscript (or superscript) using the attribute dictionary:
#{(id)kCTSuperscriptAttributeName: #(-1)}
Unfortunately the shift caused by this is too large in my case. I'm looking for an option to adjust the baseline in 1-point steps.
The attributed string will be displayed in a UIButton using -setAttributedTitle:forState:.
As of iOS 7 the NSBaselineOffsetAttributeName attribute has been added.
Yes you can... but only if you draw the text yourself like I am doing it in my DTCoreText open source project.
See line 924ff in DTCoreTextLayoutFrame.m here: https://github.com/Cocoanetics/DTCoreText/blob/master/Core/Source/DTCoreTextLayoutFrame.m
Unfortunately that's the ONLY way I know how to. UIKit does not give you this option.