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)];
}
paragraphStyle.defaultTabInterval không làm bất cứ điều gì:/ –