【问题标题】:UITextView add line break before each line startsUITextView 在每行开始之前添加换行符
【发布时间】:2016-02-01 20:46:32
【问题描述】:

如何在 UITextView 中的新行开始之前插入换行符?下面的图片应该更能说明问题。
原始
最终结果

【问题讨论】:

  • 你试过\n 吗?还是你的意思是行距?
  • @RMenke 只有第一行使用“\n”。我不是指行距,抱歉。
  • 要么在每行的末尾使用\n,要么将行间距设置为与换行符相同的高度。没有其他选择。我会设置行距。计算新行何时开始并插入 \n 并非不可能,但需要更多的工作和“慢”。
  • 我不太确定每个末尾的\n 将如何工作,因为换行符由 UITextVIew 的宽度决定。还将行距设置为与换行符相同的高度会创建一个新行吗?或者只是在线条之间创建一个间距。

标签: swift uitextview


【解决方案1】:

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

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

UITextViews 的来源不同,因此文本将显示为交替行。


好处:

  • 不用担心哪些文本是原创的,哪些是您以编程方式添加的。
  • 轻松设置不同的样式

可能的缺点:

  • 只有一个 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"

【讨论】:

    【解决方案2】:

    不确定这是不是最好的解决方案,但目前可以。 (对其他答案非常开放)。感谢RMenke 的想法

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

    【讨论】:

      【解决方案3】:
      txtField.text = textField.text.stringByAppendingString("\n")
      

      来自 chrissukhram 的回答/想法

      【讨论】:

      • 请解释一下,核心问题是什么,采取了哪些措施来解决它,以及为什么。正如目前所写,这个答案不是很有用。
      猜你喜欢
      • 1970-01-01
      • 2011-04-11
      • 1970-01-01
      • 2014-03-01
      • 2012-03-14
      • 1970-01-01
      • 2013-11-29
      • 1970-01-01
      相关资源
      最近更新 更多