0

我正在从JSON文件中引入一些数据,并且我想将字体更改为,courier但是当我尝试执行以下操作时,出现此错误:"Property 'font' not found on object NSString". 有没有办法解决?

NSString *timePeriod = [diction objectForKey:@"Time Period"];  

UIFont *changeFont = [UIFont fontWithName:@"Courier" size:12];
timePeriod.font = changeFont;
4

3 回答 3

2

字体是属性字符串的一部分。

NSMutableAttributedString timeSringWithAttr = [[NSMutableAttributedString alloc] initWithString:timeString];
[timeStringWithAttr addAttribute:NSFontAttributeName
                           value:changeFont
                           range:NSRangeFromString(timeString)];
于 2012-11-20T15:10:11.290 回答
1

NSString 只存储字符串,它不知道字体。如果要在绘制字符串时设置字体,请使用 NSString AppKit 添加:

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSString_AppKitAdditions/Reference/Reference.html#//apple_ref/doc/uid/20000155

基本上,您想在将字符串绘制到屏幕时设置字体。

于 2012-11-20T14:57:03.527 回答
0

NSString 类没有字体属性,它不应该因为每个 String 对象都是一个字符数组。
相反,使用 UILabel 或 UITextView,取决于您对其中文本的需求,并在它们上设置字体属性。

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];
label.text = @"YOUR_TEXT";
label.font = [UIFont fontWithName:@"Courier" size:12.0f];

按照惯例,你不应该用动词名称命名变量,那些是用于函数的。
像 UIFont *changeFont..

于 2012-11-20T15:21:04.010 回答