Questions tagged [nsattributedstring]

NSAttributedString objects manage 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.

NSAttributedString and NSMutableAttributedString declare the programmatic interface for read-only attributed strings and modifiable attributed strings, respectively. The Foundation framework defines only the basic functionality for attributed strings; in Mac OS X, additional methods supporting RTF, graphics attributes, and drawing attributed strings are described in NSAttributedString Additions, found in the Application Kit. The Application Kit also uses a subclass of NSMutableAttributedString, called NSTextStorage, to provide the storage for the Application Kit’s extended text-handling system.

In Mac OS X, the Application Kit also uses NSParagraphStyle and its subclass NSMutableParagraphStyle to encapsulate the paragraph or ruler attributes used by the NSAttributedString classes.

An attributed string identifies attributes by name, storing a value under the name in an NSDictionary object. In Mac OS X, standard attribute keys are described in the “Constants” section of NSAttributedString Application Kit Additions Reference. You can also assign any attribute name/value pair you wish to a range of characters—it is up to your application to interpret custom attributes (see Attributed String Programming Guide). If you are using attributed strings with the Core Text framework, you can also use the attribute keys defined by that framework.

Note that the default font for NSAttributedString objects is Helvetica 12-point, which differs from the Mac OS X system font Lucida Grande, so you may wish to create the string with non-default attributes suitable for your application using, for example, initWithString:attributes:.

Resource

2249 questions
22
votes
1 answer

Why does the initial call to NSAttributedString with an HTML string take over 100 times longer than subsequent calls?

I had a need to display HTML text inside my iOS app. I have decided I will use the built-in method on NSAttributedString, initWithData:options:documentAttributes:error:. The actual parsing works excellently, however, I seem to have come across a…
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
22
votes
1 answer

Change color and font of a string using NSAttributed String

I have a string in which last word would always be of a different color compared to the other words in entire string eg: if the string color is black then last word in the statement would be of red color. I am using NSAttributed string for it as…
tech_human
  • 6,592
  • 16
  • 65
  • 107
21
votes
1 answer

iOS 15 Crash when initialize NSAttributedString with HTML string

I have an extension to convert HTML string to NSAttributedString. After iOS 15 is released, I've seen a lot of crashes from my user. All those crashes are on iOS 15 and occur on the main thread. Here is a typical crash report from…
Chen Zheng
  • 211
  • 2
  • 4
21
votes
3 answers

Cannot convert value of type '[String : AnyObject]?' to expected argument type '[NSAttributedStringKey : Any]?'

How to convert values of type '[String : AnyObject]?'to expected argument type '[NSAttributedStringKey : Any]?'? open class func drawText(context: CGContext, text: String, point: CGPoint, align: NSTextAlignment, attributes: [String :…
Fawwad Ahmed
  • 234
  • 1
  • 4
  • 12
21
votes
4 answers

Add NSUnderlineStyle.PatternDash to NSAttributedString in Swift?

I'm trying to add an underline to some text in my Swift app. This is the code I have currently: let text = NSMutableAttributedString(string: self.currentHome.name) let attrs =…
Undo
  • 25,519
  • 37
  • 106
  • 129
20
votes
2 answers

Find Range of substring in NSMutableAttributedString

I have AttributedString with emoji like this " @Mervin tester " Now I need to find a range of Mervin in this attributed String. let attributedString = NSMutableAttributedString(string: " @Mervin tester ") let range = // range for "Mervin" in above…
Ujesh
  • 1,698
  • 2
  • 23
  • 35
20
votes
4 answers

Change attributes of substrings in a NSAttributedString

This question may be a duplicate of this one. But the answers don't work for me and I want to be more specific. I have a NSString, but I need a NS(Mutable)AttributedString and some of the words in this string should be given a different color. I…
mjay
  • 231
  • 1
  • 3
  • 12
19
votes
2 answers

Displaying Lots Of RichText : Choosing the best option

What is the best way to display lots of RichText (HTML formatted) At the moment I'm working on an app for a forum. As you'd expect the posts on the site have lots of HTML formatting, lists, images bold text, colored text, etc... Now there are…
Kyle Howells
  • 3,008
  • 1
  • 25
  • 35
19
votes
2 answers

Vertically aligning NSTextAttachment in NSMutableAttributedString

I'm adding an icon to a UILabel using NSTextAttachment inside an NSMutableAttributedString like this: //Setting up icon let moneyIcon = NSTextAttachment() moneyIcon.image = UIImage(named: "MoneyIcon") let moneyIconString =…
FS.O6
  • 1,394
  • 2
  • 20
  • 42
19
votes
5 answers

Line Spacing for UILabel with a single line of text

Is it expected that a multi-line UILabel with a custom lineSpacing attribute include that line spacing even when the label's text fits on one line? Here is my label: let label = UILabel() label.numberOfLines = 4 var paragraph =…
Ben Packard
  • 26,102
  • 25
  • 102
  • 183
19
votes
5 answers

Drop cap with NSAttributedString

I would like to do a drop cap first character in a UILabel using the attributedText NSAttributedString property only. Like this: (source: interpretationbydesign.com) I have experimented with adjusting the base line for the range of the first…
PeyloW
  • 36,742
  • 12
  • 80
  • 99
19
votes
6 answers

NSAttributedString change style to bold without changing pointSize?

I am digging into NSAttributedStrings on iOS. I have a model that is returning a persons first and last name as NSAttributesString. (I don't know if it is a good idea to deal with attributed strings in models!?) I want the first name to be printed…
Michael Ochs
  • 2,846
  • 3
  • 27
  • 33
18
votes
3 answers

How is a paragraph defined in an NSAttributedString?

I see that there is an attribute name (NSParagraphStyleAttributeName) for applying paragraph styles to text in Cocoa. Text is stored in an NSAttributedString, but what defines a "paragraph" in that string -- is it the newline character \n? Other…
Rob N
  • 15,024
  • 17
  • 92
  • 165
18
votes
8 answers

Swift- Change font on an HTML string that has its own Styles

I am dinamically getting an HTML string from a Wordpress API and parsing it into an Attributed String to show it in my app. Since the string has its own styles, it shows different fonts and sizes, something that is affecting our design choices. What…
Jacobo Koenig
  • 11,728
  • 9
  • 40
  • 75
18
votes
5 answers

Are there any analogues of [NSString stringWithFormat:] for NSAttributedString

Usually I build app interface in interface builder. Sometimes design requires to use attributed strings (fonts, colors and etc.). It's easy to configure if string is static. But if string is dynamic (format with arguments) then there are no ways to…
Vlad Papko
  • 13,184
  • 4
  • 41
  • 57