【问题标题】:Can I use NSAttributedString in Core Text in iOS?我可以在 iOS 的核心文本中使用 NSAttributedString 吗?
【发布时间】:2011-04-15 16:34:53
【问题描述】:

我正在尝试研究如何获取 NSAttributedString 并在 iPad 上的 Core Text 中使用它。我观看了一个有幻灯片(但没有源代码)的 WWDC 视频 (110),它描述了如何创建 NSAttributedString,然后将其放入 CTFramesetterRef:

CTFontRef helveticaBold = CTFontCreateWithName( CFSTR("Helvetica-Bold"), 24.0, NULL);

NSString* unicodeString = NSLocalizedString(@"TitleString", @"Window Title");
CGColorRef color = [UIColor blueColor].CGColor; NSNumber* underline = [NSNumber numberWithInt:kCTUnderlineStyleSingle|kCTUnderlinePatternDot];
NSDictionary* attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:helveticaBold, (NSString*)kCTFontAttributeName, color, (NSString*)kCTForegroundColorAttributeName, underline, (NSString*)kCTUnderlineStyleAttributeName, nil];
NSAttributedString* stringToDraw = [[NSAttributedString alloc] initWithString:unicodeString attributes:attributesDict];

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(stringToDraw);

CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
CFRelease(framesetter);
CTFrameDraw(frame, context);
CFRelease(frame);

但是当我尝试这个时,它会抛出错误:

从不兼容的指针类型传递“CTFramesetterCreateWithAttributedString”的参数 1

CFAttributedString 和 NSAttributedString 是否“免费桥接”?我在某处读到这仅在 OSX 上是正确的,但这就是 Apple 在他们的演示中这样做的方式......

谢谢!

:-乔

【问题讨论】:

  • 您是否能够显示虚线下划线?我只在文本下得到一个简单的单下划线。

标签: iphone objective-c ipad core-text


【解决方案1】:

您可以下载WWDC10源代码here。此外,使用免费桥接并将您的 stringToDraw 明确转换为 CFAttributedStringRef

【讨论】:

  • 谢谢,但您提供的链接一直显示我的会话已过期(即使我再次尝试登录)。我之前下载过WWDC10示例代码,但是里面没有110 :(
  • 只需将NSAttributedString* stringToDraw = [[NSAttributedString alloc] initWithString:unicodeString attributes:attributesDict]; 更改为CFAttributedStringRef stringToDraw = (CFAttributedStringRef)[[NSAttributedString alloc] initWithString:unicodeString attributes:attributesDict];
【解决方案2】:

你快到了。将 CTFontRef 添加到属性字典时,只需将其转换为 id 类型即可。

CTFontRef helveticaBold = CTFontCreateWithName( CFSTR("Helvetica-Bold"), 24.0, NULL);

NSString* unicodeString = NSLocalizedString(@"TitleString", @"Window Title");

CGColorRef color = [UIColor blueColor].CGColor; 
NSNumber* underline = [NSNumber numberWithInt:
    kCTUnderlineStyleSingle|kCTUnderlinePatternDot];

NSDictionary* attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:
                    (id)helveticaBold, (NSString*)kCTFontAttributeName, 
                    color, (NSString*)kCTForegroundColorAttributeName, 
                    underline, (NSString*)kCTUnderlineStyleAttributeName, nil];

NSAttributedString* stringToDraw = [[NSAttributedString alloc] 
                            initWithString:unicodeString
                            attributes:attributesDict];

【讨论】:

    【解决方案3】:

    WWDC Session 110 CTPageViewer 的代码现在是苹果官方示例代码的一部分:

    http://developer.apple.com/library/ios/#samplecode/CoreTextPageViewer/Listings/ReadMe_txt.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-29
      • 2013-12-26
      • 1970-01-01
      • 1970-01-01
      • 2017-12-05
      • 1970-01-01
      • 1970-01-01
      • 2015-06-16
      相关资源
      最近更新 更多