遇到CTFontRef问题

时间:2012-03-19 08:47:27

标签: iphone objective-c ios xcode sdk

您好我需要使用CTFontRef两种字体作为iPad应用程序,我需要使用此方法,因为我的字体不是英文。所以我从这个site

中找到了这段代码

但是编译器给了我一些错误:

CTFontRef font = CTFontCreateWithName(CFSTR("myfont"), 12,NULL);

NSMutableAttributedStringRef attrString = [[NSMutableAttributedString alloc] initWithString:textView.text];
[attrString addAttribute:(NSString*)kCTFontAttributeName
                   value:(id)font
                   range: NSMakeRange(0, textView.text.length)];

enter image description here

如果你能帮助我解决问题,我将不胜感激

2 个答案:

答案 0 :(得分:1)

看看这个。可能对你有用:

CTFontRef font = CTFontCreateWithName(CFSTR("GujaratiSangamMN-Bold"),12.0f, NULL);
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
                       (id)font, kCTFontAttributeName,
                       nil];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:textView.text
                                                                 attributes:attrs];

答案 1 :(得分:0)

代码的问题在于NSMutableAttributedStringRef。改为使用NSMutableAttributedString

NSString *myString = @"Hello World!!";
CTFontRef futura = CTFontCreateWithName( CFSTR("Futura"), 24.0, NULL);

NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] 
        initWithString:myString];

[attString addAttribute:(NSString *)kCTFontAttributeName
                  value:(id)futura
                  range:NSMakeRange(0, 4)];