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
14
votes
3 answers

Displaying NSMutableAttributedString on iOS 8

Seems on iOS 8.0 (12A365) NSMutableAttributedString sometimes will not be displayed correctly. The problem obviously occurs when the range of the attribute does not start at the beginning of the text (and if there is no other attribute starting at…
user3845832
14
votes
1 answer

Handling UIContentSizeCategoryDidChangeNotification for NSAttributedString in UITextView

I have an NSAttributedString in a UITextView and would like to handle the UIContentSizeCategoryDidChangeNotification when working with Dynamic Type and specifically the text styles. All the examples I've seen (IntroToTextKitDemo) address the case…
byamabe
  • 1,421
  • 1
  • 12
  • 19
14
votes
4 answers

UITextField attributedPlaceholder has no effect

I'm trying to make the placeholders in my textfields italic, and since my app is targeting iOS 6.0 or newer, decided to use attributedPlaceholder property instead of rolling something more custom. The code goes as follows: NSString *plString =…
SaltyNuts
  • 5,068
  • 8
  • 48
  • 80
13
votes
6 answers

Adding strikethrough to NSAttributedString in iOS 11 with Swift

Having some issues getting strikethrough to work. Currently I'm doing the following: theString.addAttributes([ NSAttributedStringKey.strikethroughStyle: NSUnderlineStyle.styleSingle.rawValue, …
eskimo
  • 304
  • 1
  • 3
  • 15
13
votes
2 answers

How to make NSMutableAttributedString responsive with dynamic type text from settings app

let dictTitleColor = [NSAttributedStringKey.foregroundColor: UIColor.LTColor()] let titleAttributedString = NSMutableAttributedString(string: title, attributes: dictTitleColor) alert.setValue(titleAttributedString, forKey: "attributedTitle") When I…
13
votes
2 answers

NSKernAttributeName space at end of line in an NSAttributedString

When using NSKernAttributeName it puts a space at the end of each line, is there any way to fix this? I can set the attributed to be in the range of: NSRange(location: 0, length: self.text!.characters.count-1) But I don't want to set this for every…
richy
  • 2,716
  • 1
  • 33
  • 42
13
votes
1 answer

Localising a UILabel with attributed string from a Storyboard

I have a UILabel with text set as "attributed" in a storyboard. When I generate the Main.strings file for translating into a different language the text for this label does not appear. I tried to add manually an entry into the Main.strings file by…
Felipe Ferri
  • 3,488
  • 2
  • 33
  • 48
13
votes
3 answers

How to remove attributes from the NSAttributedString swift?

I have added some attributes to my buttons attributedTitle let attr = NSMutableAttributedString(string: currTitle) attr.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attr.length)) …
13
votes
1 answer

How do I properly render nested/multiple superscripts in DTCoreText?

I want to have multiple/nested superscipts from HTML to NSAttributedString using DTCoreText. If I take the following HTML: Some text with a superscript Which renders on Stackoverflow correctly as: Some text with a…
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
13
votes
3 answers

Attributed Title attributes set to UIButton in Interface Builder are ignored in app

I have a UIButton that I've given an attributed title in Interface Builder: it has a specific font, and one part of the string is a different color. When I load my app, the color is maintained, but the app reverts the font back to system default...…
Adama
  • 1,101
  • 8
  • 26
13
votes
2 answers

NSAttributedString justified text (without stretching words)

I'm using justified text for my NSAttributedString in my UITextView: var paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = NSTextAlignment.Justified; normalAttributes.setObject(paragraphStyle, forKey:…
Casey Perkins
  • 1,853
  • 2
  • 23
  • 41
13
votes
6 answers

How do you clear attributes from NSMutableAttributedString?

How do you set all the attributes in an NSMutableAttributedString to nothing? Do you have to enumerate through them and remove them? I don't want to create a new one. I am working on the textStorage in NSTextView. Setting a new string resets the…
James Alvarez
  • 7,159
  • 6
  • 31
  • 46
13
votes
1 answer

Text Rendering iOS - Most performant way to render fast changing text

What is the most performant way to render fast changing text (10-15 numbers that change every single frame) in iOS? Those are the tries that I did so far: 1) CATextLayer + CTFont and NSString: Quite fast but no access to kerning, which I need. (If…
Max
  • 2,699
  • 2
  • 27
  • 50
13
votes
3 answers

How do you get the image data from NSAttributedString

I have an NSTextView. I paste an image into it and see it. When I get the NSTextAttachment for the NSAttributedString of the text view, it's file wrapper is nil. How do I get the image data that was pasted into the text view? I'm using a category on…
joels
  • 7,249
  • 11
  • 53
  • 94
13
votes
5 answers

Translate attributed string

I have a UILabel with an attributed string. Here is a printscreen of it: Now, I have to translate this attributed string to english and italian. I am searching for a way to do this. Can I build this attributed string in code part by part? I have…
BennoDual
  • 5,865
  • 15
  • 67
  • 153