Questions tagged [nsmutableattributedstring]

357 questions
141
votes
25 answers

Changing specific text's color using NSMutableAttributedString in Swift

The issue I am having is that I want to be able to change the textColor of certain text in a TextView. I am using a concatenated string, and just want the strings I am appending into the TextView's text. It appears that what I want to use is…
richyrich24
  • 1,557
  • 2
  • 10
  • 9
134
votes
23 answers

Making text bold using attributed string in swift

I have a string like this var str = "@text1 this is good @text1" Now replace text1 with another string, say t 1. I am able to replace the text, but i am not able to bold it. I want to bold the new string t 1, so that the final output will be: @t 1…
user2413621
  • 2,916
  • 7
  • 24
  • 28
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
70
votes
8 answers

Underline part of a string using NSMutableAttributedString in iOS 8 is not working

I try to underline part of a string, for example, a 'string' part in 'test string' string. I'm using NSMutableAttributedString and my solution was working well on iOS7. NSMutableAttributedString *attributedString = [[NSMutableAttributedString…
KlimczakM
  • 12,576
  • 11
  • 64
  • 83
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
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
39
votes
4 answers

Convert NSMutableAttributedString to NSString

Can we not convert NSMutableAttributedString to NSString? I have two NSMutableAttributedStrings and I am appending the 2nd string onto 1st as below: [string1 appendAttributedString:string2]; Since I have to display string1 on a label I…
tech_human
  • 6,592
  • 16
  • 65
  • 107
38
votes
3 answers

iOS crash report "unexpected start state" exception?

I have found several crash reports with the reason unexpected start state. My code looks like this: NSRange range = [content rangeOfString:@""]; if (range.location != NSNotFound) { NSString *htmlStr = [content…
tom lider
  • 419
  • 4
  • 4
29
votes
6 answers

How to apply bold and italics to an NSMutableAttributedString range?

I've been trying to apply combinations of NSFontAttributes to NSMutableAttributedString's lately and I simply can't find a thorough explanation on how to do it without removing other attributes. I've searched a bunch, and found this question…
Micrified
  • 3,338
  • 4
  • 33
  • 59
22
votes
8 answers

Setting NSLinkAttributeName font color

I feel like I'm missing something easy but I can't seem to find out how to do this: I set the attribute to a link like so: [myAttrString addAttribute:NSLinkAttributeName value:linkURL range:selectedRange]; That works but the link is blue and I…
Oren
  • 5,055
  • 3
  • 34
  • 52
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
6 answers

Color all occurrences of string in swift

This code var textSearch="hi" var textToShow="hi hihi hi" var rangeToColor = (textToShow as NSString).rangeOfString(textSearch) var attributedString =…
jjx
  • 257
  • 2
  • 9
17
votes
1 answer

Swift 4.2: [Swift._EmptyArrayStorage _getValue:forType:]: unrecognized selector

I'm facing a NSInvalidArgumentException exception after upgrading a project to Swift 4.2 (conversion from 4.0). 2018-09-19 15:37:33.253482+0100 -beta[3715:1010421] -[Swift._EmptyArrayStorage _getValue:forType:]: unrecognized selector sent…
ricardopereira
  • 11,118
  • 5
  • 63
  • 81
15
votes
2 answers

NSMutableAttributedString add different alignments

Is it possible to add left and right alignments to different parts of the string? I tried to add alignment attribute to the right part: NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle alloc] init]; [paragrahStyle…
Shmidt
  • 16,436
  • 18
  • 88
  • 136
1
2 3
23 24