【问题标题】:Align text before a tab stop to the right, and after it to the left将制表位之前的文本向右对齐,之后向左对齐
【发布时间】:2014-05-05 18:15:09
【问题描述】:

我正在使用 Text Kit 在我的应用程序中布局一些自定义文本,并且我想创建漂亮的列表,并使用正确对齐的枚举数。我需要文本看起来像这样:

粉色线不是想要的效果的一部分,而是强调枚举数应该如何对齐(即它们需要右对齐,但左边的文本仍然应该左对齐)。

我已经实现了这个要求的列表样式(枚举数目前是左对齐的)。为此,我将枚举数添加为字符串,在其后添加制表符 (\t),并在所需位置将自定义制表位 (NSTextTab) 添加到 NSParagraphStyle。通过将headIndent 设置为与NSTextTab 相同的位置来缩进后续行。

关于如何根据需要对齐枚举器有什么想法吗?

【问题讨论】:

    标签: ios uitextview textkit


    【解决方案1】:

    想通了。原来我可以用NSTextTabs 做到这一点,如下所示:

    NSString *enumerator = @"\t\u2022\t"; // Bullet symbol
    
    NSMutableParagraphStyle *enumeratorParagraphStyle = [paragraphStyle mutableCopy];
    NSTextTab *enumeratorTabStop = [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentRight
                                                                   location:eMarkdownRendererIndentationPoints
                                                                    options:nil];
    NSTextTab *contentTabStop = [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentLeft
                                                                location:contentLocation
                                                                 options:nil];
    
    enumeratorParagraphStyle.tabStops = @[enumeratorTabStop, contentTabStop];
    [textStorage appendAttributedString:[[NSAttributedString alloc] initWithString:enumerator
                                                                        attributes:@{NSParagraphStyleAttributeName: enumeratorParagraphStyle}]];
    

    【讨论】:

    • 如果你能提供一个更完整的例子,我将不胜感激。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 2019-11-17
    • 2019-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多