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
52
votes
12 answers

How to get height for NSAttributedString at a fixed width

I want to do some drawing of NSAttributedStrings in fixed-width boxes, but am having trouble calculating the right height they'll take up when drawn. So far, I've tried: Calling - (NSSize) size, but the results are useless (for this purpose), as…
bonaldi
  • 992
  • 1
  • 8
  • 11
51
votes
3 answers

Converting a NSAttributedString into a NSString using Swift

I have a NSMutableAttributedString and want to convert it back into a simple String. var attributedString = NSMutableAttributedString(string: "hello w0rld") How can I get just the String out of a NSMutableAttributedString the easiest?
ixany
  • 5,433
  • 9
  • 41
  • 65
50
votes
8 answers

Can I set the `attributedText` property of `UILabel`

Can I set the attributedText property of a UILabel object? I tried the below code: UILabel *label = [[UILabel alloc] init]; label.attributedText = @"asdf"; But it gives this error: Property "attributedText" not found on object of type 'UILabel…
Shamsiddin Saidov
  • 2,281
  • 4
  • 23
  • 35
49
votes
7 answers

How to set font size on NSAttributedString

Edit - This has been marked as duplicate, but as I state below, I am looking for a Swift solution. Everything I've found is written in Objective C. I am trying to convert HTML into an NSAttributedString, but can't figure how to set the font style…
Martin Muldoon
  • 3,388
  • 4
  • 24
  • 55
49
votes
3 answers

centering text in a UILabel with an NSAttributedString

Going through some basic improvements to a application I am working on. Still new to the iOS swift development scene. I figured that the lines of text in my code would automatically be centered because I set the label to center. After a little bit…
cruelty
  • 523
  • 1
  • 5
  • 10
49
votes
5 answers

How do I create a UIColor from RGBA?

I want to use NSAttributedString in my project, but when I'm trying to set color, which isn't from the standard set (redColor, blackColor, greenColor etc.) UILabel displays these letters in white color. Here is my line of this…
agoncharov
  • 822
  • 1
  • 6
  • 15
46
votes
3 answers

NSAttributedString ignores Autoshrink and numberOfLines for UILabel (iOS 6)

I have UILabel with number of lines = 2 system font size = 15 minimum font size = 8 Line break mode - Truncate tail When I set long text which have type NSString for UILabel it works fine and shows multiline text (scaled if needed). When I am…
Sergey Pekar
  • 8,555
  • 7
  • 47
  • 54
44
votes
4 answers

Transform an NSAttributedString to plain text

I have an instance of NSData containing attributed text (NSAttributedString) originating from an NSTextView. I want to convert the attributed string to a plain string (NSString) without any formatting to do some text analysis (at the moment of…
Roger
  • 4,737
  • 4
  • 43
  • 68
44
votes
11 answers

Make link in UILabel.attributedText *not* blue and *not* underlined

I want some words within my OHAttributedLabel to be links, but I want them to be colors other than blue and I don't want the underline. This is giving me a blue link with underlined text: -(void)createLinkFromWord:(NSString*)word…
OdieO
  • 6,836
  • 7
  • 56
  • 88
41
votes
9 answers

replace entire text string in NSAttributedString without modifying other attributes

I have a reference to NSAttributedString and i want to change the text of the attributed string. I guess i have to created a new NSAttributedString and update the reference with this new string. However when i do this i lose the attributed of…
Chirag Jain
  • 2,378
  • 3
  • 21
  • 22
40
votes
4 answers

NSAttributedString superscript styling

I want to superscript all the instances of ® character in a block of text (legal disclaimer, naturally ;)) and the default way NSAttributedString is not very good. If I just let the character be and only use unmodified NSString, it is rendered the…
SaltyNuts
  • 5,068
  • 8
  • 48
  • 80
40
votes
5 answers

How can I both stroke and fill with NSAttributedString w/ UILabel

Is it possible to apply both stroke and fill with an NSAttributedString and a UILabel?
Piotr Tomasik
  • 9,074
  • 4
  • 44
  • 57
39
votes
7 answers

Type 'NSAttributedStringKey' (aka 'NSString') has no member 'font'

Just updated pods for xcode 9 and I'm getting the error below for Cosmos. Type 'NSAttributedStringKey' (aka 'NSString') has no member 'font'
Chaudhry Talha
  • 7,231
  • 11
  • 67
  • 116
38
votes
0 answers

NSRangeException Out of Bounds error

I am setting the attributed text of a label, and I am getting this strange error: Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray replaceObjectsInRange:withObject:length:: Out of bounds'. I have never seen…
37
votes
9 answers

Tap on a part of text of UILabel

I have a problem that boundingRectForGlyphRange always returns CGRect.zero "0.0, 0.0, 0.0, 0.0". For example, I am coding for touching on a part of text of UILabel feature. My text has first part is any text and second one is READ MORE. I want the…
Ashley
  • 499
  • 1
  • 6
  • 15