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
2,261
questions
377
votes
31
answers
432k
views
How do I make an attributed string using Swift?
I am trying to make a simple Coffee Calculator. I need to display the amount of coffee in grams. The "g" symbol for grams needs to be attached to my UILabel that I am using to display the amount. The ...
318
votes
15
answers
248k
views
How do you use NSAttributedString?
Multiple colours in an NSString or NSMutableStrings are not possible. So I've heard a little about the NSAttributedString which was introduced with the iPad SDK 3.2 (or around 3.2) and is available on ...
300
votes
35
answers
308k
views
Create tap-able "links" in the NSAttributedString of a UILabel?
Many applications have text and in this text are web hyperlinks in rounded rect. When I click them UIWebView opens. What puzzles me is that they often have custom links, for example if words starts ...
262
votes
15
answers
161k
views
Bold & Non-Bold Text In A Single UILabel?
How would it be possible to include both bold and non-bold text in a uiLabel?
I'd rather not use a UIWebView.. I've also read this may be possible using NSAttributedString but I have no idea how to ...
234
votes
23
answers
287k
views
How can I make a clickable link in an NSAttributedString?
It's trivial to make hyperlinks clickable in a UITextView. You just set the "detect links" checkbox on the view in IB, and it detects HTTP links and turns them into hyperlinks.
However, that still ...
188
votes
9
answers
115k
views
How can I concatenate NSAttributedStrings?
I need to search some strings and set some attributes prior to merging the strings, so having NSStrings -> Concatenate them -> Make NSAttributedString is not an option, is there any way to concatenate ...
168
votes
9
answers
213k
views
Change string color with NSAttributedString?
I have a slider for a survey that display the following strings based on the value of the slider: "Very Bad, Bad, Okay, Good, Very Good".
Here is the code for the slider:
- (IBAction) ...
165
votes
18
answers
130k
views
Convert HTML to NSAttributedString in iOS
I am using a instance of UIWebView to process some text and color it correctly, it gives the result as HTML but rather than displaying it in the UIWebView I want to display it using Core Text with a ...
163
votes
25
answers
137k
views
boundingRectWithSize for NSAttributedString returning wrong size
I am trying to get the rect for an attributed string, but the boundingRectWithSize call is not respecting the size I pass in and is returning a rect with a single line height as opposed to a large ...
157
votes
17
answers
96k
views
Parsing HTML into NSAttributedText - how to set font?
I am trying to get a snippet of text that is formatted in html to display nicely on an iPhone in a UITableViewCell.
So far I have this:
NSError* error;
NSString* source = @"<strong>Nice</...
144
votes
16
answers
177k
views
How to underline a UILabel in swift?
How to underline a UILabel in Swift? I searched the Objective-C ones but couldn't quite get them to work in Swift.
134
votes
9
answers
125k
views
NSAttributedString add text alignment
How can I add text alignment attribute to an NSAttributedString to center the text?
Edit:
Am I doing anything wrong? It doesn't seem to change the alignment.
CTParagraphStyleSetting setting;
setting....
120
votes
16
answers
168k
views
Swift: Display HTML data in a label or textView [duplicate]
I have some HTML data, which contains headings, paragraphs , images and lists tags.
Is there a way to display this data in one UITextView or UILabel?
113
votes
17
answers
128k
views
Use multiple font colors in a single label
Is there a way to use two, or even three font colors in a single label in iOS?
If the text "hello, how are you" were used as an example, the "hello," would be blue, and the "how are you" would be ...
113
votes
12
answers
102k
views
Attributed Text Center Alignment
I have tried everything but cannot seem to center this text. Can someone please tell me where the error is.
NSMutableParagraphStyle *paragraphStyle = NSMutableParagraphStyle.new;
paragraphStyle....
106
votes
16
answers
55k
views
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 ...
104
votes
9
answers
94k
views
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 ...
101
votes
5
answers
66k
views
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 ...
82
votes
4
answers
84k
views
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 ...
79
votes
4
answers
34k
views
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) ...
73
votes
24
answers
32k
views
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 ...
71
votes
6
answers
54k
views
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 ...
70
votes
7
answers
47k
views
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 ...
69
votes
10
answers
47k
views
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:...
68
votes
4
answers
26k
views
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 ...
66
votes
5
answers
76k
views
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.
65
votes
10
answers
75k
views
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 ...
64
votes
3
answers
65k
views
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 ...
63
votes
1
answer
83k
views
How to calculate the height of an NSAttributedString with given width in iOS 6 [duplicate]
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 ...
59
votes
8
answers
22k
views
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 ...
52
votes
12
answers
33k
views
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 ...
51
votes
3
answers
57k
views
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 ...
50
votes
8
answers
98k
views
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 "...
50
votes
7
answers
81k
views
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 ...
49
votes
5
answers
72k
views
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 ...
49
votes
3
answers
48k
views
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 ...
46
votes
3
answers
26k
views
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 ...
46
votes
2
answers
3k
views
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:...
45
votes
4
answers
37k
views
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 ...
44
votes
11
answers
36k
views
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)...
42
votes
9
answers
38k
views
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. ...
40
votes
5
answers
23k
views
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?
40
votes
4
answers
19k
views
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 ...
39
votes
7
answers
32k
views
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'
38
votes
9
answers
81k
views
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 ...
37
votes
3
answers
12k
views
Wrapping Text in a UITextView Around a UIImage WITHOUT CoreText
Is there a way to wrap text from a UITextView around a UIImage without using CoreText?
I have been playing around with attributed strings without much luck, and CoreText just seems extremely ...
36
votes
6
answers
28k
views
how to resize an image or done as a NSAttributedString NSTextAttachment (or set its initital size)
I have a NSAttributedString to which I am adding a NSTextAttachment. The image is 50w by 50h but I'd like it to scale down to reflect the line height of the attributed string. I thought this would be ...
35
votes
8
answers
14k
views
How to set color of templated image in NSTextAttachment
How can I set the color of a templated image that is an attachment on an attributed string?
Background:
I've got a UILabel and I'm setting its attributedText to an NSAttributedString. The ...
34
votes
7
answers
28k
views
NSAttributedString inserting a bullet point?
So I have an NSAttributedString I want to insert a bullet point at the beginning of a portion of text. How can I do this? How do I create a CTPAragraphStyle that creates this bullet point when I ...
34
votes
1
answer
48k
views
How can I set the color and alignment of attributed text in a UITextView in iOS 7?
The formatting of my textViews worked fine in iOS 6, but no longer in iOS 7. I understand with Text Kit much of the under the hood stuff has changed. It's become really quite confusing, and I'm hoping ...