问题描述
我正在创建一个iOS应用,我想在UITextView中显示一个带有
特定制表位的属性字符串。我还想在drawRect中使用Core Text将
直接绘制到UIViews中。我希望(如果可能的话)
用于在两种情况下使用相同的属性字符串。
I'm creating an iOS app, and I would like to display an attributed string withspecific tab stops specified in a UITextView. I would also like to draw themdirectly into UIViews using Core Text in drawRect. I would like (if possible)for the same attributed string to be used in both scenarios.
因此,在我的应用程序中,我创建了一个CFAttributedStringRef或者NSAttributedString和
将CTParagraphStyle属性应用于它。然后,我尝试在UITextView中显示
属性字符串,我得到如下所示的崩溃:
So, in my app, I create an CFAttributedStringRef or an NSAttributedString andapply a CTParagraphStyle attribute to it. Then, I attempt to display theattributed string in a UITextView and I get a crash like the following:
-[__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"
)
我明白发生了什么,但我想知道我是否有任何其他方式
做我想做的事情。 iOS上的NSMutableParagraphStyle没有tab
stop支持,但我注意到OS X上的NSMutableParagraphStyle确实具有
功能。这让我觉得iOS NSMutableParagraphStyle有一天可以支持
。
I understand what is going on, but I am wondering if I have any alternative wayof doing what I'd like to. NSMutableParagraphStyle on iOS does not have tabstop support, yet I notice that NSMutableParagraphStyle on OS X does have thiscapability. This leads me to think that the iOS NSMutableParagraphStyle mayone day support this.
同时,有没有办法将标签停止添加到CFAttributedStringRef或者
NSAttributedString还有UITextView显示吗?
In the meantime, is there way to add tab stops to a CFAttributedStringRef or anNSAttributedString and still have a UITextView display it?
有问题的来源是:
- (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)];
}
推荐答案
在iOS 7中你可以做它是这样的:
In iOS 7 you can do it like this:
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};
这篇关于如何将选项卡停止添加到NSAttributedString并显示在UITextView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!