Swift UITextView在每行开始之前添加换行符

Swift UITextView在每行开始之前添加换行符,swift,uitextview,Swift,Uitextview,如何在UITextView中的新行开始之前插入换行符?下面的图片应该会更清晰。 最初的 最终结果 不确定这是否是最好的解决方案,但目前它仍然有效。(对其他答案持开放态度)。谢谢你的主意 let outputString = ""; let splitInput = [String]() let input = incomingText outPut = input.componentsSeparatedByCharactersInSet(.newlineChar

如何在UITextView中的新行开始之前插入换行符?下面的图片应该会更清晰。
最初的
最终结果

不确定这是否是最好的解决方案,但目前它仍然有效。(对其他答案持开放态度)。谢谢你的主意

    let outputString = "";
    let splitInput = [String]()
    let input = incomingText
    outPut =  input.componentsSeparatedByCharactersInSet(.newlineCharacterSet())
    for index in splitInput{
   self.outputString +=  index + "\n\n"   
   }
   print(outputString)

如果要显示来自两个源的文本,但文本显示在一个视图中,其中每个源的行交替显示,则可以创建一个类似下面示例中的类

我创建了
UIView
的一个子类,它附带了两个
uitextview
作为
subview
。文本通过
DoubleTextView
类进行设置

uitextview
的原点不同,因此文本将显示为交替行


好处:

  • 不要担心哪些文本是原始文本,哪些文本是以编程方式添加的
  • 易于设置不同的样式
可能的不利因素:

  • 只有一个UITextView是可编辑的,因为其中一个始终是不可触摸的(已涵盖)

结果:

需要一些更好的数学知识来理想地定位或分隔文本


代码:

将字符串转换为
NSMutableAttributedString
并设置行距:

func convertLineHeight(string string_I: String) -> NSMutableAttributedString {

    let style = NSMutableParagraphStyle()
    style.lineSpacing = lineSpacing + self.font.lineHeight
    let attributes = [NSParagraphStyleAttributeName : style]
    let attributedString =  NSMutableAttributedString(string: string_I, attributes:attributes)

    return attributedString

}
偏移一个视图:

let alphaPoint = CGPoint(x: 0, y: (lineSpacing / 2) + self.font.lineHeight)
// this is almost perfect. is off by a few pixels.
全班:

class DoubleTextView : UIView {

    private var alphaTextView : UITextView!
    private var betaTextView : UITextView!

    private var lineSpacing : CGFloat = 0

    var font : UIFont = UIFont.systemFontOfSize(UIFont.systemFontSize()) {
        didSet {
            alphaTextView.font = self.font
            betaTextView.font = self.font
        }
    }

    var alphaText : String = "" {
        didSet {
            alphaTextView.attributedText = convertLineHeight(string: alphaText)
        }
    }
    var betaText : String = "" {
        didSet {
            betaTextView.attributedText = convertLineHeight(string: betaText)
        }
    }

    var alphaAttributedText : NSMutableAttributedString = NSMutableAttributedString() {
        didSet {
            alphaTextView.attributedText = convertLineHeight(attributedString: alphaAttributedText)
        }
    }

    var betaAttributedText : NSMutableAttributedString = NSMutableAttributedString() {
        didSet {
            betaTextView.attributedText = convertLineHeight(attributedString: betaAttributedText)
        }
    }

    init(frame: CGRect, lineSpacing lineSpacing_I: CGFloat) {

        lineSpacing = lineSpacing_I

        super.init(frame: frame)

        var adjustedSize = frame.size
        adjustedSize.height -= ((lineSpacing / 2) + self.font.lineHeight)

        let alphaPoint = CGPoint(x: 0, y: (lineSpacing / 2) + self.font.lineHeight)

        alphaTextView = UITextView(frame: CGRect(origin: alphaPoint, size: adjustedSize))
        alphaTextView.backgroundColor = UIColor.clearColor()
        alphaTextView.font = self.font
        betaTextView = UITextView(frame: CGRect(origin: CGPointZero, size: adjustedSize))
        betaTextView.font = self.font
        betaTextView.backgroundColor = UIColor.clearColor()



        self.addSubview(alphaTextView)
        self.addSubview(betaTextView)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    private func convertLineHeight(string string_I: String) -> NSMutableAttributedString {

        let style = NSMutableParagraphStyle()
        style.lineSpacing = lineSpacing + self.font.lineHeight
        let attributes = [NSParagraphStyleAttributeName : style]
        let attributedString =  NSMutableAttributedString(string: string_I, attributes:attributes)

        return attributedString

    }

    private func convertLineHeight(attributedString attributedString_I: NSMutableAttributedString) -> NSMutableAttributedString {

        let style = NSMutableParagraphStyle()
        style.lineSpacing = lineSpacing + self.font.lineHeight
        let attributes = [NSParagraphStyleAttributeName : style]
        attributedString_I.addAttributes(attributes, range: (attributedString_I.string as NSString).rangeOfString(attributedString_I.string))
        return attributedString_I

    }


}


var test = DoubleTextView(frame: CGRect(x: 0, y: 0, width: 200, height: 400), lineSpacing: 20)
test.backgroundColor = UIColor.whiteColor()

test.alphaText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

test.betaText = "This is the second text. It shows comments, edits, suggestions, thoughts about the first"

来自Chrisukhram的答案/想法

您是否尝试过
\n
?还是说行距?@RMenke仅使用“\n”影响第一行。对不起,我不是指行距。请在每行末尾使用
\n
,或者将行距设置为与换行符相同的高度。没有其他选择。我会设置行间距。计算新行何时开始并插入
\n
并非不可能,但要做的工作要多得多,而且“速度慢”。我不太确定每行末尾的
\n
将如何工作,因为换行符由UITextVIew的宽度决定。另外,将行距设置为与换行符相同的高度是否会创建新行?或者只是在两行之间创建一个间距。请解释,核心问题是什么,采取了哪些措施来解决它,以及为什么。正如目前所写,这个答案不是很有用。
txtField.text = textField.text.stringByAppendingString("\n")