【问题标题】:Break text that covers other views中断覆盖其他视图的文本
【发布时间】:2016-09-29 09:46:38
【问题描述】:

是否可以在 UITextview 中自动中断文本,使文本没有覆盖它,如下例所示?

【问题讨论】:

    标签: ios uitextview swift3


    【解决方案1】:

    添加CoreText框架

    import CoreText
    

    imageView添加到UITextView后添加以下代码,

    var exclusionPath = UIBezierPath(rect: CGRect(x: imageView.frame.orgin.x, y: imageView.frame.orgin.y, width: imageView.frame.size.width, height: imageView.frame.size.height))
    textView.textContainer.exclusionPaths = [exclusionPath]
    textView.addSubview(imageView)
    

    【讨论】:

      【解决方案2】:

      你应该使用NSLineBreakByCharWrapping,意思是这个词会在一个字符之后被剪切。

      textview.textContainer.lineBreakMode = NSLineBreakByCharWrapping;
      

      【讨论】:

      • 问题不在于换行,​​而在于它覆盖了图像:)
      【解决方案3】:

      您可以完全按照这种方式进行设计。虽然我没用过 Swift 3,但是你可以根据语法来格式化。

      这里exclusionPath将负责启动图片后的文字,exclusionPath1负责启动图片下方的文字,根据您的要求。

      我的代码:

          let txtView = UITextView(frame: CGRectMake(0, 64, SCREEN_SIZE.width, 150));
          self.view.addSubview(txtView);
      
          let imageView = UIImageView(frame: CGRectMake(0, 0, 50, 50));
          imageView.image = UIImage(named: "icon");
      
          let exclusionPath = UIBezierPath(rect: CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height));
          let exclusionPath1 = UIBezierPath(rect: CGRectMake(-50, 50, imageView.frame.size.width, imageView.frame.size.height));
      
          txtView.textContainer.exclusionPaths  = [exclusionPath, exclusionPath1];
          txtView.addSubview(imageView);
      

      输出:

      希望对你有帮助

      编码愉快...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-05
        相关资源
        最近更新 更多