为什么我会得到一个' NSString *'丢弃第三行上的currentTextB上的限定符" CFAttributedStringSetAtribute(currentTextB ..."
CFAttributedStringRef currentTextB = CFAttributedStringCreate(NULL, stringRef, NULL);
CTFontRef font = CTFontCreateWithName((CFStringRef)@"Helvetica", 10.0f, nil);
CFAttributedStringSetAttribute(currentTextB,CFRangeMake(0, strLength-1),kCTFontAttributeName,font);
答案 0 :(得分:2)
CFAttributedStringSetAttribute()
的第一个参数必须是 mutable
属性字符串。例如:
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);
或者,使用基础类型NSMutableAttributedString
是CFMutableAttributedStringRef
的“免费桥接”,因此可以使用
可互换:
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])];