Swift 3 NSAttributedString multiple attributes

Related searches

just started swift 3 and I have problems with swift syntax.

i'm trying to display a simple NSAttributedString.

so 1st I set my attributes :

let attributeFontSaySomething : [String : AnyObject] = [NSFontAttributeName : UIFont.fontSaySomething()]
let attributeColorSaySomething : [String : AnyObject] = [NSForegroundColorAttributeName : UIColor.blue]

Then I create my string :

let attStringSaySomething = NSAttributedString(string: "Say something", attributes: self.attributeFontSaySomething)

What i would like to do is to create the string with my 2 attributes not only just one. But when i do :

let attStringSaySomething = NSAttributedString(string: "Say something", attributes: [self.attributeFontSaySomething, self.attributeColorSaySomething])

Xcode tells me I can't and want me to change this for a literal dictionary.

How can I create my string with the 2 attributes without using a NSMutableAttributedString ?

The main issue is that you are passing an array [attr.. , attr...] rather than one dictionary.

You need to merge the two dictionaries into one

let attributeFontSaySomething : [String : Any] = [NSFontAttributeName : UIFont.systemFont(ofSize: 12.0)]
let attributeColorSaySomething : [String : Any] = [NSForegroundColorAttributeName : UIColor.blue]

var attributes = attributeFontSaySomething
for (key, value) in attributeColorSaySomething {
    attributes(value, forKey: key)

let attStringSaySomething = NSAttributedString(string: "Say something", attributes: attributes)

However it might be easier to create the dictionary literally:

let attributes : [String : Any] = [NSFontAttributeName : UIFont.systemFont(ofSize: 12.0), NSForegroundColorAttributeName : UIColor.blue]

Is it possible to set multiple font attributes using an NSAttributedString, My 2 cents ;) Swift 3 text1, attributes: attribs1); str.append(NSAttributedString( string: text2, attributes: attribs2)) return str }. Usage: let attr1� Add the following below the Add attributes here comment: myMutableString.addAttribute(NSFontAttributeName, value: UIFont( name: "Chalkduster", size: 24.0)!, range: NSRange( location: 9, length: 5)) We use the NSFontAttributeName attribute as our attribute and 24 point Chalkduster as our font. That is the same as the initializer.

Just create a single dictionary with both sets of attributes:

let attributes: [String:AnyObject] = 
  [NSFontAttributeName : UIFont.fontSaySomething(), 
  NSForegroundColorAttributeName : UIColor.blue]

And then use the dictionary with both key/value pairs when creating your attributed string.

There's no built-in mechanism in Swift for combining dictionaries, but you could add an override of the + operator if you wanted to be able to add dictionaries together (You'd have to work out what to do if both dictionaries contained the same key however.)

NSAttributedString by example – Hacking with Swift, These are used in various places in iOS and macOS, but you're most likely to now and get three months off, exclusively for Hacking with Swift readers. If you want to add multiple attributes at the same time, you should use� If you want to translate an attributed string with XCode, it is not easy. Normal strings can be translated with NSLocalizedString(key: comment). However, there is a problem with attributed strings. The attributes must be placed in the correct place in the string and this can vary greatly depending on the language. One approach I found

Use like this:

let attStringSaySomething = NSAttributedString.init(string: "Hello", attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 16), NSForegroundColorAttributeName:UIColor.black])

Multiple Font, Style for UILable Text using NSAttributed String., Now append attributedText. attributedText.append(NSAttributedString(string: “ Attributed Text,”, attributes: [NSAttributedStringKey.font: UIFont. Upgrade to Swift 5. String class is very rich and great in Swift, but as there is a lot of requirements to have formatted string. Using NSMutableAttributedString and NSAttributedString we can achieve multiple fonts, color, underline, strikethrough, shadow and a lot more.

Thanks for @vadian's answer

Update For Swift 4

let attributes : [NSAttributedStringKey : Any] = [NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue) : UIFont.systemFont(ofSize: 12.0), NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue) : UIColor(hex:"4C0000")]

refreshControl.attributedTitle=NSAttributedString(string: "Refreshing...", attributes: attributes)

Create a Single UILabel with Multiple Styles using Attributed Text, Here's a guide for creating a single UILabel with multiple styles. Initialize an Attributed String Using Those Attributes 3. Create Dictionaries for as Many Styles as You Desire. Just as you created a Tagged: objective-c iOS uilabel attributed textPosted on July 1, 2019 by Molly Alger in Objective-C� Misspelled text that isn’t necessarily visibly marked as misspelled (NSNumber as a Boolean value).Beginning in macOS 10.9, VoiceOver no longer checks for this attribute; instead, VoiceOver uses NSAccessibility Marked Misspelled Text Attribute.

You can use this code for different attributes on different strings With Roboto font (For Roboto font use MDFRobotoFontLoader)

let yourAttributes = [NSForegroundColorAttributeName: UIColor.black, NSFontAttributeName: MDFRobotoFontLoader.sharedInstance().regularFont(ofSize: 20)]

let finalString =  NSMutableAttributedString(string: "", attributes: yourAttributes)

let attributeStr =  NSMutableAttributedString(string: "XYZGFDGii", attributes: yourAttributes)

let yourOtherAttributes = [NSForegroundColorAttributeName: UIColor.red, NSFontAttributeName: MDFRobotoFontLoader.sharedInstance().regularFont(ofSize: 24)]

let partTwo = NSMutableAttributedString(string: "hfjghlkdhkjld", attributes: yourOtherAttributes)

This example uses Roboto font

iOS Tutorials: Label with multiple attributes, NSAttributedString. For the effect of Note as illustrated above. The font size is set to 15 with semibold; The color is set to� String attributes are just a dictionary in the form of [NSAttributedStringKey: Any], where NSAttributedStringKey is the key name of the attribute and Any is the value of some Type. The value could be a font, a color, an integer, or something else. There are many standard attributes in Swift that have already been predefined. For example:

I could add multiple attributes at once with this method, but I will just add one again. myRange = NSRange(location: 3, length: 17) let anotherAttribute = [ NSBackgroundColorAttributeName: UIColor.yellow ] myString.addAttributes(anotherAttribute, range: myRange) Notice that the attributes are overlapping in some places.

NSAttributedString in Swift. Style: this is the class which represent a collection of attributes you can apply to a string. Style is created anonymously (you don’t need to assign a name like

The values of the attributes dictionary are of type Any, because NSAttributedString attributes can be all sorts of things: numbers, colors, fonts, paragraph styles, and more. If you look in the output pane of your playground, you should be able to click on the box next to where it says “This is a test string” to get a live preview of how

  • does the attributes have to be wrapped into variables?
  • No no, just to more clear.
  • Could I ask to check for Swift 4.0 version? I first tried to make [NSAttributedStringKey: AnyObject] array, but I cannot use any NSAttribute...Name.
  • You are taking an NSAttributedStringKey, converting it into a String, and then using that String to construct an NSAttributedStringKey. There is no need for that at all. Just use the NSAttributedStringKey. In fact, you can just use .font instead of NSAttributedStringKey.font.
  • Oh it crashes using AsyncDisplayKit