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
106
votes
16 answers

Attributed string with custom fonts in storyboard does not load correctly

We are using custom fonts in our project. It works well in Xcode 5. In Xcode 6, it works in plain text, attributed string in code. But those attributed strings set in storyboard all revert to Helvetica when running on simulator or device, although…
Allen Hsu
  • 3,515
  • 3
  • 25
  • 38
104
votes
9 answers

iphone/ipad: How exactly use NSAttributedString?

Yes, many people are saying about Rich Text in iPhone/iPad and many knows about NSAttributedString. But how to use NSAttributedString? I searched for much time, no extract clues for this. I know how to set up a NSAttributedString, then what should…
Jack
  • 3,913
  • 8
  • 41
  • 66
95
votes
5 answers

Change the color of a link in an NSMutableAttributedString

I have the following code but my links are always blue. How do I cange the color of them? [_string addAttribute:NSLinkAttributeName value:tag range:NSMakeRange(position, length)]; [_string addAttribute:NSFontAttributeName value:[UIFont…
cdub
  • 24,555
  • 57
  • 174
  • 303
81
votes
4 answers

Example of NSAttributedString with two different font sizes?

NSAttributedString is just really impenetrable to me. I want to set a UILabel to have text of different sizes, and I gather NSAttributedString is the way to go, but I can't get anywhere with the documentation on this. I would love it if someone…
Le Mot Juiced
  • 3,761
  • 1
  • 26
  • 46
75
votes
4 answers

NSAttributedString background color and rounded corners

I have a question regarding rounded corners and text background color for a custom UIView. Basically, I need to achieve an effect like this (image attached - notice the rounded corners on one side) in a custom UIView: I'm thinking the approach to…
codeBearer
  • 5,114
  • 4
  • 25
  • 29
70
votes
7 answers

Is it possible to get a listing of attributes and ranges for an NSMutableAttributedString?

I've created a method that takes a NSAttributedString and I'm looking to dynamically create a subview and label to put the string into. Because attributes like font and size need to be determined to correctly determine the size of the label, I need…
propstm
  • 3,461
  • 5
  • 28
  • 41
69
votes
22 answers

UITextView: Disable selection, allow links

I have a UITextView which displays an NSAttributedString. The textView's editable and selectable properties are both set to false. The attributedString contains a URL and I'd like to allow tapping the URL to open a browser. But interaction with the…
lukas
  • 2,300
  • 6
  • 28
  • 41
69
votes
6 answers

Adding underline to UILabel attributed string from the storyboard fails

From the storyboard I select the UILabel in question Then in Attribute Inspector > Label > [I choose] Attributed Also in Attribute Inspector > Label > Text> [I select the content] Then I click on the font icon and choose underline Basically, any…
Katedral Pillon
  • 14,534
  • 25
  • 99
  • 199
68
votes
10 answers

Replace substring of NSAttributedString with another NSAttributedString

I want to replace a substring (e.g. @"replace") of an NSAttributedString with another NSAttributedString. I am looking for an equivalent method to NSString's stringByReplacingOccurrencesOfString:withString: for NSAttributedString.
Garoal
  • 2,364
  • 2
  • 19
  • 31
67
votes
4 answers

Center two fonts with different different sizes vertically in an NSAttributedString

I use NSAttributedString to generate a string with two different sizes. By default, its bottom alignment looks like this: But I want to center it vertically, like this: To be clear, this is a single attributed string, not two or more. This is a…
user2608857
  • 1,061
  • 2
  • 9
  • 11
65
votes
5 answers

how to append Attributed Text String with Attributed String in Swift

I want to append an Attributed Text with another Attributed Text in Swift. Please provide any sample code for appending operation of two attributed String in Swift.
jaydev
  • 1,629
  • 5
  • 17
  • 31
64
votes
3 answers

iOS NSAttributedString on UIButton

I'm using iOS 6, so attributed strings should be easy to use, right? Well... not so much. What I want to do: Using a custom subclass of UIButton (it doesn't do anything custom to titleLabel), I'd like to have a multi-line, attributed title that…
mbm29414
  • 11,558
  • 6
  • 56
  • 87
63
votes
10 answers

Core Text - NSAttributedString line height done right?

I'm completely in the dark with Core Text's line spacing. I'm using NSAttributedString and I specify the following attributes on it: - kCTFontAttributeName - kCTParagraphStyleAttributeName From this the CTFrameSetter gets created and drawn to…
Schoob
  • 1,688
  • 1
  • 14
  • 18
63
votes
1 answer

How to calculate the height of an NSAttributedString with given width in iOS 6

Possible Duplicate: How to get height for NSAttributedString at a fixed width Now NSAttributedString is available in iOS 6. For layout purposes, I want to know how to calculate the required height of an NSAttributedString under fixed width. I'm…
Jake
  • 1,518
  • 2
  • 14
  • 20
58
votes
8 answers

NSAttributedString, change the font overall BUT keep all other attributes?

Say I have an NSMutableAttributedString . The string has a varied mix of formatting throughout: Here is an example: This string is hell to change in iOS, it really sucks. However, the font per se is not the font you want. I want to: for each and…
Fattie
  • 27,874
  • 70
  • 431
  • 719