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

时间:2015-11-02 02:03:11

标签: swift uitextview

如何在UITextView中新行开始之前插入换行符?下面的图像应该更多的光 的原单
original 最终结果
final

3 个答案:

答案 0 :(得分:1)

如果您想显示来自两个来源的文字,但文字显示在一个视图中,其中每个来源的行交替显示,您可以创建一个类,如下例所示。

我创建了一个UIView的子类,它带有两个UITextViews作为Subviews。文本通过DoubleTextView类设置。

UITextViews原点不同,因此文字会显示为替换行。

好处:

  • 不要担心哪个文本是原始文本以及以编程方式添加的文本。
  • 轻松设置不同的风格

可能的缺点:

  • 只有一个UITextView可以编辑,因为一个永远是不可触摸的(它被覆盖)

<强>结果:

enter image description here

理想情况下,需要一些更好的数学来定位或分隔文本。

<强>代码:

将字符串转换为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"

答案 1 :(得分:0)

不确定这是最好的解决方案,但现在可以使用。 (非常接受其他答案)。感谢RMenke的想法

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

答案 2 :(得分:-3)

txtField.text = textField.text.stringByAppendingString("\n")
来自chrissukhram的回答/想法