I have an NSTextView object that has been added through interface builder. I am able to change the background color of the text segment that has been selected as follows.
- (void)emphasizeText {
NSColor *c = [colorWell.color colorUsingColorSpaceName:NSCalibratedRGBColorSpace]; //colorWell is an NSColorWell object that has been added through interface builder
NSRange selection = textView1.selectedRange; //textView1 is an NSTextView object that has been added through interface builder
[textView1.textStorage addAttributes:@{NSBackgroundColorAttributeName:c} range:selection];
}
Yet, I have trouble removing the added attribute (NSBackgroundColorAttributeName) from the selected text segment. In reference to this topic, I have the following code.
- (void)deemphasizeText {
NSMutableAttributedString *aStr = [textView1.attributedString mutableCopy];
NSRange selection = textView1.selectedRange;
[aStr enumerateAttribute:NSBackgroundColorAttributeName inRange:selection options:0 usingBlock:^(id value,NSRange range,BOOL *stop) {
if (value) {
[aStr removeAttribute:NSBackgroundColorAttributeName range:range];
}
}];
}
And nothing changes though the application won't crash. I have done several variations like
- (void)deemphasizeText {
NSRange selection = textView1.selectedRange;
NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc] initWithAttributedString:(NSMutableAttributedString *)[textView1.textStorage attributedSubstringFromRange:selection]];
[aStr enumerateAttribute:NSBackgroundColorAttributeName inRange:NSMakeRange(0,aStr.length) options:0 usingBlock:^(id value,NSRange range,BOOL *stop) {
if (value) {
[aStr removeAttribute:NSBackgroundColorAttributeName range:range];
}
}];
}
Again the selected segment doesn't change. What am I doing wrong?
Muchos thankos.