When you want to compose a string of text that is attributed to a single person, you have to use the “&” character. The problem is that it is hard to type it, and it is even harder to remember. Wouldn’t it be nice if you could simply insert the “&” character and the “&” character would combine? You could simply type “&and” and the “&” character would be replaced by the “and” character.
If you write a lot of code and go back to read your past code, you may have noticed that the code you wrote a few days ago looks really different to the code you wrote a week ago. This is because the syntax you use to write the code has changed, often due to new features or changes in the style guide.
In my previous post, I showed how to write a string based on the attribute of another string in the same way as how we write a regular string. The problem is that we have to pre-compute all the integers and apply the ReplacingCharacter to each to make the attribute string. However, we could use a different approach to make the attribute string more efficiently.
Text feature
With TextAttributes, you can easily compile assigned character strings.
let attrs = TextAttributes()
.font(name : HelveticaNeue, size : 16)
.foregroundColor(white : 0.2, alpha : 1)
.lineHeightMultiple(1.5)
NSAttributedString(string : The brown and quick fox jumps over the lazy dog, attributes : attrs)
Characteristics
- x] Strongly typed features
- x] Chain adjustment methods
- x] Direct access to NSParagraphStyle properties
- x] Improved auto-fill function
Use
- Obtain or set TextAttributes properties:
attrs.font = UIFont(name : HelveticaNew, size : 16)
attrs.backgroundColor = .white
View all articles
The TextAttributes methods return Self to enable method chaining:
attrs
.lineHeightMultiple(1.5)
.underlineStyle(.styleSingle)
Show all methods
- Methods are also constructors:
The following are equivalent:
attrs
.font(name: HelveticaNew, size: 16)
.foregroundColor(white: 0.2, alpha: 1)
font = UIFont(name : HelveticaNeue, size : 16)
lets color = UIColor(white : 0.2, alpha : 1)
attrs
.font(font)
.foregroundColor(color)
- Access to basic vocabulary :
attrs.dictionary // Returns an attribute dictionary of type [NSAttributedString.Key : Elk]
Third party libraries:
Demo
Live updates with Injection for Xcode
References
Attribute dictionary
Dictionary test | TextAttributes property | TextAttribute method |
---|---|---|
NSFontAttributeName | Police | Font(Name:Size:) |
Writing (_ 🙂 | ||
NSParagraphStyleAttributeName | paragraphStyle | paragraphStyle(_ 🙂 |
NSForegroundColorAttributeName | foreground color | foreground color(_ 🙂 |
foregroundcolor(white:alpha 🙂 | ||
foregroundcolor(hue:saturation:brightness:alpha:) | ||
foregroundcolor(red:green:blue:alpha 🙂 | ||
Foreground color(patternImage 🙂 | ||
NSBackgroundColorAttributeName | Background color | background color(_ 🙂 |
backgroundcolor(white:alpha 🙂 | ||
backgroundcolor(hue:saturation:brightness:alpha:) | ||
backgroundcolor(red:green:blue:alpha 🙂 | ||
background color(pattern image 🙂 | ||
NSLigatureAttributeName | Binding | Binding(_ 🙂 |
NSKernAttributeName | Core | core(_) |
NSStrikethroughStyleAttributeName | Style type | Crossed-out style (_ 🙂 |
NSStrikethroughColorAttributeName | strikethroughColor | color blocked(_ 🙂 |
strike throughColor(white:alpha 🙂 | ||
strike throughColor(hue:saturation:brightness:alpha 🙂 | ||
strike throughColor(red:green:blue:alpha 🙂 | ||
bar color(patternImage 🙂 | ||
NSUnderlineStyleAttributeName | style of underlining | underscore type(_ 🙂 |
NSUnderlineColorAttributeName | underlineColor | underscore color(_ 🙂 |
underscore color(white:alpha 🙂 | ||
underscoreColor(hue:saturation:brightness:alpha 🙂 | ||
underscore color(red:green:blue:alpha 🙂 | ||
underscore color(pattern image 🙂 | ||
NSStrokeWidthAttributeName | stroke width | line width(_ 🙂 |
NSStrokeColorAttributeName | strokeColor | strokeColor(_ 🙂 |
strokeColor(white:alpha 🙂 | ||
strokeColor(Hue:Saturation:Lightness:Alpha 🙂 | ||
strokeColor(red:green:blue:alpha 🙂 | ||
strokeColor(patternImage 🙂 | ||
NSShadowAttributeName | Shadow | Shadow(_) |
shadow(color:offset:blurRadius 🙂 | ||
NSTextEffectAttributeName | textEffect | textEffect(_ 🙂 |
NSAttachmentAttributeName | Appendix | Appendix (_:) |
NSLinkAttributeName | Link | Reference(_:) |
Reference(line:) | ||
link(string:relativeToURL 🙂 | ||
NSBaselineOffsetAttributeName | baselineOffset | baselineOffset(_ 🙂 |
NSObliquenessAttributeName | Skewed | Slant(_) |
NSExpansionAttributeName | Expansion | Expansion(_ 🙂 |
NSVerticalGlyphFormAttributeName | verticalGlyphForm | verticalGlyphShape(_ 🙂 |
Form of paragraph
NSMutableParagraphStyle | TextAttributes property | TextAttribute method |
---|---|---|
Alignment | Alignment | Alignment(_ 🙂 |
firstLineHeadIndent | firstLineHeadIndent | firstLineHeadIndent(_ 🙂 |
headIndent | headIndent | mainIndent(_ 🙂 |
tailIndent | tailIndent | tailIndent(_ 🙂 |
lineBreakMode | lineBreakMode | lineBreakMode(_ 🙂 |
maximum line height | maximum line height | Maximum Line Height(_ 🙂 |
Line spacing | Line spacing | line spacing(_ 🙂 |
paragraph spacing | paragraph spacing | ParagraphDistance(_ 🙂 |
paragraphSpacingBefore | paragraphSpacingBefore | paragraph space before(_:) |
baseWritingDirection | baseWritingDirection | baseWritingDirection(_ 🙂 |
lineHeightMultiple | lineHeightMultiple | lineHeightMultiple(_ 🙂 |
Setting
Carthago
Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate TextAttributes into your Xcode project with Carthage, specify it in your cartfile:
github delba/TextAttributes
Coconut palms
CocoaPods is a dependency manager for Cocoa projects.
You can install it with the following command:
$ gem install cocoapods
To include TextAttributes in your Xcode project with CocoaPods, specify it in your podfile:
use_frameworks !
pod Text attribute
GitHub
https://github.com/delba/TextAttributesToday’s blog post will be about my favourite topic — composing attributed strings. And I am happy to announce that I have come up with an easier way to do this.. Read more about nsattributedstring get attributes and let us know what you think.
Frequently Asked Questions
What is attributed string?
The idea of an attributed string is to have one string containing the hidden message for the outside message. The basic idea is that this string is encrypted with a normal string that contains the encrypted string. In a nutshell, the hidden message will be decrypted using the private key and will yield the decrypted message. However, the problem is that the private key should be kept secret, so how do we do it? The HTML5 tag allows authors to place text alongside hyperlinks, and also to direct readers to a new web page. The tag is the simplest and most necessary tag in the HTML5 family; however, its form is not straightforward.
How do you make an attributed string in Swift?
When you are composing an attributed string in your Swift app, it gets a bit frustrating sometimes, as you have to write the attribute string by hand, before creating the string, and then you have to add a hardcoded string at the end of the attributed string, which is kind of frustrating. So I found a simple method to compose the attributed string by adding a newline after the “c” in “c = myString” and then you can use the “greeting” method to create the string. In Swift, you can turn the following string: “My name is Mr. Smith – I teach at RHS – I work at a school – I live in a town – I’m 53 years old” into a string with a few lines of code. You just need to add a very simple attribute to the string and then you can call it an attributed string. The downside of this is that the string cannot have any whitespace inside, which makes it harder to read.
How do you make a string bold in Swift?
Bolding an attribute in a String in swift is done like this: let boldString = “Hello, bold attribute!” // * is the character to be bolded let boldString = “Hello, bold attribute!” // ‘>’ is the character to be bolded let boldString = “Hello, bold attribute!” // ‘<' is the character to be bolded let boldString = "Hello, bold attribute!" // '=' is the character to be bolded let boldString = "Hello, bold attribute!" // '+' is the character to be bolded let boldString = "Hello, bold attribute!" // '+' is NOT the character to be bolded let boldString = "Hello, bold attribute!" // ' Strings are one of the most common and powerful types of data in a program. Most apps and websites interact with strings in some way, but they’re not always easy to read. In this post, we’ll show you how to make strings bold in Swift.
Related Tags:
nsattributedstringattributed string objective-cnsattributedstring objective-c examplensattributedstring get attributesappend attributed string swiftnsattributedstring font size,People also search for,Feedback,Privacy settings,How Search works,attributed string with different fonts swift,nsattributedstring,attributed string objective-c,nsattributedstring objective-c example,nsattributedstring get attributes,append attributed string swift,nsattributedstring font size,nsattributedstring underline