user520300
user520300

Reputation: 1527

NSString * Discards Qualifiers

Why am i getting an 'NSString *' discards qualifiers on currentTextB on third line "CFAttributedStringSetAtribute(currentTextB..."

CFAttributedStringRef currentTextB = CFAttributedStringCreate(NULL, stringRef, NULL);

CTFontRef font = CTFontCreateWithName((CFStringRef)@"Helvetica", 10.0f, nil);
CFAttributedStringSetAttribute(currentTextB,CFRangeMake(0, strLength-1),kCTFontAttributeName,font);

Upvotes: 0

Views: 376

Answers (1)

Martin R
Martin R

Reputation: 539815

The first argument to CFAttributedStringSetAttribute() must be a mutable attributed string. Example:

CFStringRef stringRef = CFSTR("foo");

// Create mutable attributed string from CFStringRef:
CFMutableAttributedStringRef currentTextB = CFAttributedStringCreateMutable(NULL, 0);
CFAttributedStringReplaceString(currentTextB, CFRangeMake(0, 0), stringRef);

// Set an attribute:
CTFontRef font = CTFontCreateWithName(CFSTR("Helvetica"), 10.0f, NULL);
CFAttributedStringSetAttribute(currentTextB,
                               CFRangeMake(0, CFAttributedStringGetLength(currentTextB)),
                               kCTFontAttributeName,font);

Alternatively, work with the Foundation type NSMutableAttributedString which is "toll-free bridged" to CFMutableAttributedStringRef and therefore can be used interchangeably:

NSString *string = @"foo";
NSMutableAttributedString *currentTextB = [[NSMutableAttributedString alloc] initWithString:string attributes:nil];
NSFont *font = [NSFont fontWithName:@"Helvetica" size:10.0];
[currentTextB addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [string length])];

Upvotes: 2

Related Questions