2013-03-19 37 views
6

Tôi đang tạo một ứng dụng iOS và tôi muốn hiển thị chuỗi được phân bổ với các điểm dừng tab cụ thể trong một UITextView với các điểm dừng tab cụ thể. Tôi cũng muốn vẽ chúng trực tiếp vào UIViews bằng cách sử dụng Core Text trong drawRect. Tôi muốn (nếu có thể) cho cùng một chuỗi được phân bổ sẽ được sử dụng trong cả hai trường hợp.Làm cách nào để thêm các điểm dừng tab vào NSAttributedString và hiển thị trong UITextView

Vì vậy, trong ứng dụng của mình, tôi tạo CFAttributedStringRef hoặc NSAttributedString và áp dụng thuộc tính CTParagraphStyle cho nó. Sau đó, tôi cố gắng để hiển thị chuỗi do trong một UITextView và tôi nhận được một vụ tai nạn như sau:

-[__NSCFType headIndent]: unrecognized selector sent to instance 0x7545080 

po 0x7545080 
$0 = 122966144 CTParagraphStyle: 
base writing direction = -1, alignment = 4, line break mode = 0, 
    default tab interval = 0 
first line head indent = 0, head indent = 0, tail indent = 0 
line height multiple = 0, maximum line height = 0, minimum line height = 0 
line spacing adjustment = 0, paragraph spacing = 0, 
    paragraph spacing before = 0 
tabs: 
(
    "CTTextTab: location = 20, alignment = 0, options = (none)\n", 
    "CTTextTab: location = 40, alignment = 0, options = (none)\n", 
    "CTTextTab: location = 60, alignment = 0, options = (none)\n", 
    "CTTextTab: location = 80, alignment = 0, options = (none)\n", 
    "CTTextTab: location = 100, alignment = 0, options = (none)\n", 
    "CTTextTab: location = 120, alignment = 0, options = (none)\n" 
) 

tôi hiểu những gì đang xảy ra, nhưng tôi tự hỏi nếu tôi có bất kỳ cách nào thay thế làm những gì Tôi muôn. NSMutableParagraphStyle trên iOS không có tab dừng hỗ trợ, nhưng tôi nhận thấy rằng NSMutableParagraphStyle trên OS X có khả năng này . Điều này khiến tôi nghĩ rằng NSMutableParagraphStyle iOS có thể hỗ trợ một ngày này.

Trong thời gian chờ đợi, có cách nào để thêm các điểm dừng tab vào CFAttributedStringRef hoặc NSAttributedString và vẫn hiển thị UITextView không?

Các nguồn tin trong câu hỏi là:

- (void)viewWillAppear:(BOOL)animated 
{ 
    CFDictionaryRef attrs = (__bridge CFDictionaryRef) @{}; 
    CFAttributedStringRef a = CFAttributedStringCreate(
     kCFAllocatorDefault, CFSTR("a\tb\tc\td"), attrs); 

    CFMutableAttributedStringRef attrStr; 
    attrStr = CFAttributedStringCreateMutableCopy(
     kCFAllocatorDefault, CFAttributedStringGetLength(a), a); 

    CFArrayRef tabStops = (__bridge CFArrayRef) @[ 
     (__bridge id) CTTextTabCreate(0, 20, NULL), 
     (__bridge id) CTTextTabCreate(0, 40, NULL), 
     (__bridge id) CTTextTabCreate(0, 60, NULL), 
     (__bridge id) CTTextTabCreate(0, 80, NULL), 
     (__bridge id) CTTextTabCreate(0, 100, NULL), 
     (__bridge id) CTTextTabCreate(0, 120, NULL)]; 

    const CTParagraphStyleSetting paraSettings[] = { 
     {kCTParagraphStyleSpecifierTabStops, sizeof(CFArrayRef), &tabStops}, 
    }; 

    CTParagraphStyleRef paraStyle = CTParagraphStyleCreate(
     paraSettings, 
     sizeof(paraSettings)/sizeof(CTParagraphStyleSetting)); 

    CFRange range = CFRangeMake(0, CFAttributedStringGetLength(attrStr)); 
    CFAttributedStringSetAttribute(
     attrStr, range, kCTParagraphStyleAttributeName, paraStyle); 

    CFRelease(paraStyle); 
    CFIndex i, count = CFArrayGetCount(tabStops); 
    for (i = 0; i < count; i++) { 
     CFRelease(CFArrayGetValueAtIndex(tabStops, i)); 
    } 

    [[self textView] setAttributedText: 
     (__bridge NSAttributedString *)(attrStr)]; 
} 

Trả lời

8

Trong iOS 7, bạn có thể làm điều đó như thế này:

UIFont *font = [UIFont systemFontOfSize:18.0]; 
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; 
NSInteger cnt; 
CGFloat tabInterval = 72.0; 
paragraphStyle.defaultTabInterval = tabInterval; 
NSMutableArray *tabs = [NSMutableArray array]; 
for (cnt = 1; cnt < 13; cnt++) { // Add 12 tab stops, at desired intervals... 
    [tabs addObject:[[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentLeft location:tabInterval * cnt options:nil]]; 
} 
paragraphStyle.tabStops = tabs; 
NSDictionary *attributes = @{ NSFontAttributeName: font, NSParagraphStyleAttributeName: paragraphStyle}; 
+1

paragraphStyle.defaultTabInterval không làm bất cứ điều gì:/ –

-1

iOS 6.1 thêm NSParagraphStyle nhưng nó dường như các CTParagraphStyleRef và NSParagraphStyle không thu phí cầu nối miễn phí.

Kết quả nếu bạn có CTParagraphStyleRef trong NSAttributeString nó gây ra:

[__NSCFType headIndent]: chọn không được công nhận gửi đến dụ xxxx

và phương pháp khác để NSParagraphStyle như sự liên kết.

3

Đây là phiên bản Swift mà làm việc cho tôi:

let tablInterval: CGFloat = 85.0 
    let paragraphStyle = NSMutableParagraphStyle() 
    let terms = NSTextTab.columnTerminatorsForLocale(NSLocale.currentLocale()) 
    let tabStop0 = NSTextTab(textAlignment: .Right, location: 0, options: [NSTabColumnTerminatorsAttributeName:terms]) 
    let tabStop1 = NSTextTab(textAlignment: .Right, location: tablInterval, options: [NSTabColumnTerminatorsAttributeName:terms]) 
    let tabStop2 = NSTextTab(textAlignment: .Right, location: tablInterval*2, options: [NSTabColumnTerminatorsAttributeName:terms]) 
    let tabStop3 = NSTextTab(textAlignment: .Right, location: tablInterval*3, options: [NSTabColumnTerminatorsAttributeName:terms]) 
    paragraphStyle.addTabStop(tabStop0) 
    paragraphStyle.addTabStop(tabStop1) 
    paragraphStyle.addTabStop(tabStop2) 
    paragraphStyle.addTabStop(tabStop3) 

    let attributedString = NSMutableAttributedString(string:text) 
    attributedString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:rangeAll) 
+0

gì 'tùy chọn' bạn đã bao gồm? – ma11hew28

Các vấn đề liên quan