2010-09-22 18 views
9

Tôi đang cố gắng tìm ra cách lấy NSAttributedString và sử dụng nó trong Core Text trên iPad. Tôi đã xem một trong những video WWDC (110) trong đó có các slide (nhưng không có mã nguồn) và nó mô tả làm thế nào để tạo ra một NSAttributedString, sau đó chỉ cần đặt nó vào một CTFramesetterRef:Tôi có thể sử dụng NSAttributedString trong Core Text trong iOS không?

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); 

Nhưng khi tôi cố gắng này, nó ném lỗi:

Passing argument 1 of 'CTFramesetterCreateWithAttributedString' from incompatible pointer type

CFAttributedString và NSAttributedString 'cầu nối miễn phí'? Tôi đã đọc ở đâu đó rằng điều này chỉ đúng trên OSX, nhưng đây là cách Apple làm điều đó trong bản demo của họ ...

Cảm ơn!

: -Joe

+0

là bạn có thể nhận được gạch dưới rải rác xuất hiện? Tôi chỉ nhận được gạch chân đơn đơn giản dưới văn bản. – learner2010

Trả lời

7

Bạn có thể tải xuống mã nguồn WWDC10 here. Ngoài ra, hãy sử dụng cầu nối miễn phí và sử dụng số stringToDraw của bạn một cách rõ ràng đến CFAttributedStringRef.

+0

Cảm ơn bạn, nhưng liên kết bạn đã nói tiếp tục nói rằng phiên của tôi đã hết hạn (ngay cả khi tôi cố gắng đăng nhập lại). Tôi đã tải xuống mã mẫu WWDC10 trước đây, nhưng 110 không có trong đó: ( – jowie

+1

Ngoài ra, giải pháp của bạn đã hoạt động, cảm ơn !:-) – jowie

+1

bạn có thể vui lòng đăng mã làm việc cuối cùng không. – user836026

3

Bạn sắp hoàn tất. Chỉ cần nhập CTFontRef vào loại id khi bạn thêm nó vào từ điển thuộc tính.

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]; 
Các vấn đề liên quan