我正在尝试将一堆随机文本输入到文本视图中,并将字符包装起来,以便框中的符号完整。似乎文本视图试图在基于文本中的某些字符进行包装时变得聪明。我缩小了导致文本视图错误地包装到下面的字符列表:
]
}
)
.
>
/
+
可以显示的其他字符是:
[
<
{
(
#
@
=
*
~
在Playground中运行的以下代码将导致UITextView在行中的最后一个字符之前进行换行。
import UIKit
func generateRandomText() -> NSAttributedString {
let characterLimit = 9500
let segmentSize = 500
// problem characters
// ] } ) . > / +
let characterOptions = ["[","<","{","(","#","@","=","*","~","+","]","}",")",".",">","/"]
let codeString = NSMutableAttributedString.init(string: "")
while codeString.string.characters.count < segmentSize {
let addition = NSAttributedString.init(string: characterOptions[Int(arc4random_uniform(16))], attributes: [NSForegroundColorAttributeName : UIColor.whiteColor()])
codeString.appendAttributedString(addition)
}
while codeString.string.characters.count < characterLimit {
codeString.appendAttributedString(codeString)
}
return codeString
}
let textView = UITextView.init(frame: CGRectMake(0, 0, 700, 700))
textView.textContainer.lineBreakMode = .ByCharWrapping
let codeFont = UIFont.init(name: "Menlo", size: 12)
textView.font = codeFont
textView.backgroundColor = UIColor.blackColor()
textView.attributedText = generateRandomText()
但是,没有问题字符的相同代码会产生我正在寻找的结果。
import UIKit
func generateRandomText() -> NSAttributedString {
let characterLimit = 9500
let segmentSize = 500
// problem characters
// ] } ) . > / +
let characterOptions = ["[","<","{","(","#","@","=","*","~"]
let codeString = NSMutableAttributedString.init(string: "")
while codeString.string.characters.count < segmentSize {
let addition = NSAttributedString.init(string: characterOptions[Int(arc4random_uniform(9))], attributes: [NSForegroundColorAttributeName : UIColor.whiteColor()])
codeString.appendAttributedString(addition)
}
while codeString.string.characters.count < characterLimit {
codeString.appendAttributedString(codeString)
}
return codeString
}
let textView = UITextView.init(frame: CGRectMake(0, 0, 700, 700))
textView.textContainer.lineBreakMode = .ByCharWrapping
let codeFont = UIFont.init(name: "Menlo", size: 12)
textView.font = codeFont
textView.backgroundColor = UIColor.blackColor()
textView.attributedText = generateRandomText()
当字符串包含某些字符时,什么会导致文本视图换行?
答案 0 :(得分:3)
这些特定字符是系统用来定义字边界的字符,用于换行。
调用
textView.textContainer.lineBreakMode = .ByCharWrapping
由于UITextView
不支持字符换行,无效。
文档:
http://www.unicode.org/reports/tr29/#Word_Boundaries
请注意,定义单词边界有复杂的规则。例如,“1.0”被认为是一个单词,但“a.1”被认为是两个单词。
此外,可能没有完全遵循Unicode规范。