如何添加 Show More/Show Less UIButton 来控制 UITextView [英] How to add a Show More/Show Less UIButton to control UITextView

查看:45
本文介绍了如何添加 Show More/Show Less UIButton 来控制 UITextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 UITextView 来显示一些内容.在 textView 下方,我添加了一个 UIButton,可以控制 TextView 是显示其部分内容还是全部内容.

理想情况下,当点击按钮时,TextView 将扩展其高度以适应内容的长度.

我还没有找到好的解决方案.我想知道我是否应该使用 UIlabel 而不是 textView.

解决方案

你可以让这件事以这种方式工作:

1.在 Storyboard 中添加 UITextView 和 UIButton.

2.如果您使用 Autolayout Constraints,请制作 UITextView 高度的出口.

3.在 ViewController 类中,将出口设为:

<块引用>

@IBOutlet var txtView:UITextView!

@IBOutlet var btn_Show:UIButton!

@IBOutlet var textView_HeightConstraint: NSLayoutConstraint!

4.制作一个方法,根据其中的文本为您提供 UITextView 的高度.

<块引用>

func getRowHeightFromText(strText : String!) ->CGFloat{让 textView : UITextView!= UITextView(frame: CGRect(x: self.txtView.frame.origin.x,y: 0,宽度:self.txtView.frame.size.width,高度:0))textView.text = strTexttextView.font = UIFont(name: "Fira Sans", size: 16.0)textView.sizeToFit()var txt_frame : CGRect!= CGRect()txt_frame = textView.frame变量大小:CGSize!= CGSize()大小 = txt_frame.size尺寸.高度 = 50 + txt_frame.size.height返回 size.height}

5.点击按钮:

<块引用>

 @IBAction func showMore_Button_Clicked(_ sender: UIButton){如果 sender.tag == 0{让高度 = self.getRowHeightFromText(strText: self.txtView.text)self.textView_HeightConstraint.constant = 高度self.view.layoutIfNeeded()btn_Show.setTitle("ShowLess", for: .normal)发件人.标签 = 1}别的{self.textView_HeightConstraint.constant = 116self.view.layoutIfNeeded()btn_Show.setTitle("ShowMore", for: .normal)发件人.标签 = 0}}

6.如果您没有使用 AutoLayoutConstraints,只需在单击 UIButton 时更改 UITextView 的 Frame(Height).不需要textView_HeightConstraint"

 @IBAction func showMore_Button_Clicked(_ sender: UIButton){如果 sender.tag == 0{让高度 = self.getRowHeightFromText(strText: self.txtView.text)self.txtView.frame = CGRect(x: self.txtView.frame.origin.x, y: self.txtView.frame.origin.y, width: self.txtView.frame.size.width, height: height)btn_Show.setTitle("ShowLess", for: .normal)发件人.标签 = 1}别的{self.txtView.frame = CGRect(x: self.txtView.frame.origin.x, y: self.txtView.frame.origin.y, width: self.txtView.frame.size.width, height: 116)btn_Show.setTitle("ShowMore", for: .normal)发件人.标签 = 0}}

I'm using a UITextView to display some contents. Beneath the textView, I added a UIButton that can control if the TextView will display part or all of its content.

Ideally, when button tapped, the TextView will expand its Height to accommodate the length of the content.

I haven't found a good solution. I wonder if I should use UIlabel instead of textView.

解决方案

You can make this thing working this way:

1. Add a UITextView and UIButton in Storyboard.

2. If you are using Autolayout Constraints, make an outlet of UITextView height.

3. In ViewController class make outlets as:

@IBOutlet var txtView: UITextView!

@IBOutlet var btn_Show: UIButton!

@IBOutlet var textView_HeightConstraint: NSLayoutConstraint!

4. Make a method that will give you height of UITextView according to the text in it.

func getRowHeightFromText(strText : String!) -> CGFloat
{
    let textView : UITextView! = UITextView(frame: CGRect(x:      self.txtView.frame.origin.x,
    y: 0, 
    width: self.txtView.frame.size.width,
    height: 0))
    textView.text = strText
    textView.font = UIFont(name: "Fira Sans", size:  16.0)
    textView.sizeToFit()

    var txt_frame : CGRect! = CGRect()
    txt_frame = textView.frame

    var size : CGSize! = CGSize()
    size = txt_frame.size

    size.height = 50 + txt_frame.size.height

    return size.height
}

5. On Click of Button:

   @IBAction func showMore_Button_Clicked(_ sender: UIButton)

     {

       if sender.tag == 0

         {
            let height = self.getRowHeightFromText(strText: self.txtView.text)

            self.textView_HeightConstraint.constant = height

            self.view.layoutIfNeeded()

            btn_Show.setTitle("ShowLess", for: .normal)

            sender.tag = 1

        }

        else
        {

            self.textView_HeightConstraint.constant = 116

            self.view.layoutIfNeeded()

            btn_Show.setTitle("ShowMore", for: .normal)

            sender.tag = 0

        }

    }

6. If you are not using AutoLayoutConstraints, just change the Frame(Height) of UITextView on click of UIButton. No need of "textView_HeightConstraint"

 @IBAction func showMore_Button_Clicked(_ sender: UIButton)

    {

        if sender.tag == 0

        {

            let height = self.getRowHeightFromText(strText: self.txtView.text)

            self.txtView.frame = CGRect(x: self.txtView.frame.origin.x, y: self.txtView.frame.origin.y, width: self.txtView.frame.size.width, height: height)

            btn_Show.setTitle("ShowLess", for: .normal)

            sender.tag = 1

        }

        else

        {

            self.txtView.frame = CGRect(x: self.txtView.frame.origin.x, y: self.txtView.frame.origin.y, width: self.txtView.frame.size.width, height: 116)

            btn_Show.setTitle("ShowMore", for: .normal)

            sender.tag = 0

        }

    }

这篇关于如何添加 Show More/Show Less UIButton 来控制 UITextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆