我有一个NSMutableParagraphStyle样式,适用于NSMutableAttributedString:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let myString = "Before a telephone call, I need to go to the bathroom..."
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = NSLineBreakMode.byWordWrapping
let myMutableString = NSMutableAttributedString(
string: myString,
attributes: [
NSFontAttributeName:UIFont(
name: "Georgia",
size: 45.0
)!,
NSParagraphStyleAttributeName: paragraphStyle
]
)
let textView = UITextView( frame: CGRect(
x: 20,
y: 25,
width: 260,
height: 400
) )
textView.attributedText = myMutableString
self.view.addSubview( textView )
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}但是,当我将可变字符串添加到UITextView中时,它会不断地破坏单词。
当我去paragraphStyle.lineBreakMode = NSLineBreakMode.byCharWrapping的时候,这个词就不再出现在同一个地方了。所以它确实有效果。但这仍然是部分原因。我有一个单词“call”,它在c之后中断,这太奇怪了。

我可以在故事板编辑器中复制:



发布于 2017-08-16 14:17:05
解决方案是尝试用崇高的文本编辑我的代码,并意识到电话和电话之间有一个不可打破的空间。
这似乎是一个愚蠢的基本功能。为什么Xcode不能告诉我..。
发布于 2017-08-16 02:01:36
这是预期的行为。根据苹果公司的文件,
byWordWrapping
包装发生在单词边界处,除非单词本身不适合于一行。有关确定单词边界的问题,请参阅字符串编程指南中的字符和字素群集。
byCharWrapping
包装发生在不适合的第一个字符之前。
有关更多信息,请参阅苹果的文档
https://stackoverflow.com/questions/45703403
复制相似问题