【问题标题】:NSAttributedString justified text (without stretching words)NSAttributedString 对齐文本(不拉伸单词)
【发布时间】:2021-10-12 23:38:21
【问题描述】:

我在 UITextView 中为我的 NSAttributedString 使用了对齐文本:

var paragraphStyle = NSMutableParagraphStyle()        
paragraphStyle.alignment = NSTextAlignment.Justified;
normalAttributes.setObject(paragraphStyle, forKey: NSParagraphStyleAttributeName)

问题在于它会拉伸一些单词以实现合理对齐,而不是仅仅拉伸单词之间的空格。我觉得这很让人分心,并且知道有一种方法可以使单词不会被拉伸,而只是间距。下面是我的应用程序的屏幕截图,然后是一个应用程序的屏幕截图,该应用程序具有一些相同的功能,也使用对齐的文本和完全相同的字体。

如何达到同样的效果?

任何帮助将不胜感激。

【问题讨论】:

  • 嗯,这只是一个猜测。 :( 抱歉,它没有用。如果你真的想深入了解这个,你可以深入研究 Text Kit 并覆盖布局(这实际上可能是其他应用程序所做的)。

标签: ios uitextview nsattributedstring right-justified


【解决方案1】:

你可以使用一个讨厌的技巧。

您可以使用 UIWebView 和 css 以您想要的方式进行对齐。 这是一个示例:

NSString *css = [NSString stringWithFormat:
                 @"<html><head><style>body { background-color: white; text-align: %@; font-size: %ipx; color: black;} a { color: #172983; } </style></head><body>",
                 @"justify",
                 11];

NSMutableString *desc = [NSMutableString stringWithFormat:@"%@%@%@",
                         css,
                         @"15 Προσέχετε ἀπὸ τῶν ψευδοπροφητῶν͵ οἵτινες ἔρχονται πρὸς ὑμᾶς ἐν ἐνδύμασι προβάτων͵ ἔσωθεν δέ εἰσι λύκοι ἅρπαγες. 16 ἀπὸ τῶν καρπῶν αὐτῶν",
                         @"</body></html>"];

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(20, 80, 150, 100)];
[self.view addSubview:webView];
[webView loadHTMLString:desc baseURL:nil];

结果如下:

【讨论】:

【解决方案2】:

您只需将 hyphenationFactor 更改为 1:

textView.layoutManager.hyphenationFactor = 1

【讨论】:

    猜你喜欢
    • 2011-10-11
    • 2010-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    相关资源
    最近更新 更多