我有一个包含字体样式和sumbolic特征的CTFont。
我想创建一个新的字体,它继承了第一个字体的符号特征。我如何才能做到这一点?
CTFontRef newFontWithoutTraits = CTFontCreateWithName((CFString)newFontName, CTFontGetSize(font), NULL);
CTFontRef newFont = CTFontCreateCopyWithSymbolicTraits(newFontWithoutTraits, CTFontGetSize(font), NULL, 0, CTFontGetSymbolicTraits(font));这里的新字体是空的,我不知道应该向CTFontCreateCopyWithSymbolicTraits中的4th参数传递什么。
发布于 2011-09-21 06:01:24
我这样做是为了从非粗体字体生成粗体字体:
CTFontRef newFont = CTFontCreateCopyWithSymbolicTraits(currentFont, 0.0, NULL, (wantBold?kCTFontBoldTrait:0), kCTFontBoldTrait);currentFont是我想要添加的符号特征towantBold是一个布尔值,用来告诉我是要在字体中添加还是删除粗体特征fontkCTFontBoldTrait是我要在CTFontRef上修改的符号特征。第四个参数是要应用的值。第五个是选择符号特征的掩码。
您可以将其视为位掩码以应用于符号特征,其中CTFontCreateCopyWithSymbolicTraits的第4个参数是值,第5个参数是掩码:
newTrait = oldTrait | (value&mask)一样应用sthg,将与mask对应的位设置为你想取消设置符号特征并将其从字体中移除的value.newTrait = oldTrait & ~mask这样的sthg来取消设置该位。value,其中1表示要设置的位,0表示要取消设置(或忽略)的位,并使用右侧mask,其中1表示需要修改的位,0表示不需要更改的位。EDIT2
我终于找到了针对你的特殊情况的解决方案:你需要像你已经做的那样获得font的符号掩码…和位-或者它与您的newFontWithoutTraits字体的符号。
这是因为newFontWithoutTraits实际上do有默认的symtrait(与我想的相反,它有一个非零的CTFontSymbolicTraits值),因为symtrait值也包含字体类的信息和诸如此类的东西(所以即使是一个非粗体、非斜体的字体也可以有一个非零的symtrait值,记录你的字体的符号的十六进制值,以便更好地理解)。
这就是你需要的代码,,
CTFontRef font = CTFontCreateWithName((CFStringRef)@"Courier Bold", 12, NULL);
CGFloat fontSize = CTFontGetSize(font);
CTFontSymbolicTraits fontTraits = CTFontGetSymbolicTraits(font);
CTFontRef newFontWithoutTraits = CTFontCreateWithName((CFStringRef)@"Arial", fontSize, NULL);
fontTraits |= CTFontGetSymbolicTraits(newFontWithoutTraits);
CTFontRef newFont = CTFontCreateCopyWithSymbolicTraits(newFontWithoutTraits, fontSize, NULL, fontTraits, fontTraits);
// Check the results (yes, this NSLog create leaks as I don't release the CFStrings, but this is just for debugging)
NSLog(@"font:%@, newFontWithoutTraits:%@, newFont:%@", CTFontCopyFullName(font), CTFontCopyFullName(newFontWithoutTraits), CTFontCopyFullName(newFont));
// Clear memory (CoreFoundation "Create Rule", objects needs to be CFRelease'd)
CFRelease(newFont);
CFRelease(newFontWithoutTraits);
CFRelease(font);https://stackoverflow.com/questions/7492237
复制相似问题