gpt4 book ai didi

iphone - 如何将制表位添加到 NSAttributedString 并显示在 UITextView 中

转载 作者:行者123 更新时间:2023-12-03 18:38:33 25 4
gpt4 key购买 nike

我正在创建一个 iOS 应用程序,我想显示一个属性字符串UITextView 中指定的特定制表位。我也想画他们使用drawRect中的Core Text直接进入UIViews。我愿意(如果可以的话)在这两种情况下使用相同的属性字符串。

因此,在我的应用程序中,我创建一个 CFAttributedStringRef 或 NSAttributedString 并对其应用 CTParagraphStyle 属性。然后,我尝试显示UITextView 中的属性字符串,我遇到如下崩溃:

-[__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 没有选项卡停止支持,但我注意到 OS X 上的 NSMutableParagraphStyle 确实有这个能力。这让我认为 iOS NSMutableParagraphStyle 可能有一天支持这一点。

同时,有没有办法将制表位添加到 CFAttributedStringRef 或NSAttributedString 并且还有一个 UITextView 显示它?

有问题的来源是:

- (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 中你可以这样做:

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

关于iphone - 如何将制表位添加到 NSAttributedString 并显示在 UITextView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15510936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com