【问题标题】:Dynamic UILabel Based on String基于字符串的动态UILabel
【发布时间】:2018-06-30 22:29:58
【问题描述】:

我有一个UILabel。 UILabel 中的文本如下所示:

Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Website - example.com
YouTube - youtube.com/example
Twitter - twitter.com/example
Instagram - twitter.com/example

所以这个文本是动态的。我从 API 获取字符串,并且 API 包含一个类似于上面的字符串。第一段发生变化。它可以是多个段落或任何内容。但是带有网站链接的文本保持不变。

好的,基本上,我希望标签只显示字符串的第一部分。然后,每当用户按下按钮时,它就会显示整个字符串。我无法设置numberOfLines,因为字符串的第一部分发生了变化。有没有办法告诉标签只显示文本到

Website - example.com
YouTube - youtube.com/example
Twitter - twitter.com/example
Instagram - twitter.com/example

然后每当按下按钮时,显示整个字符串?

希望这不会太令人困惑。感谢您的帮助!我正在使用 Swift 4。

【问题讨论】:

  • 如何确定第一次显示应该隐藏的部分是什么?
  • 你能显示你试过的代码吗?这似乎不是那么困难的任务。点击按钮时更改标签的文本

标签: ios swift string uilabel


【解决方案1】:

如果它的格式相同,并且您知道将第一个字符串与其余字符串分开的元素,则可以使用字符串上的 split 函数将原始 str 拆分为子字符串。

class ViewController: UIViewController 
{
   @IBOutlet weak var myLabel: UILabel!
   let strFromApi = "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. \nWebsite - example.com \nYouTube - youtube.com/example \nTwitter - twitter.com/example \nInstagram - twitter.com/example"

   override func viewDidLoad()
   {
       super.viewDidLoad()
       self.myLabel.text = getFirstSection(of: strFromApi)
   }

   func getFirstSection(of str: String) -> String
   {
       if let newStr = str.split(separator: "\n").first
       {
           return String(newStr)
       }
       return str
   }

   @IBAction func seeMoreAction(_ sender: UIButton)
   {
       self.myLabel.text = strFromApi
   }
}

【讨论】:

  • 这是个好主意。但是,API 不会为新行返回 /n。它只返回空格,而不是 \n 命令。
  • 如果您可以访问 API 并且可以更改它,那么将第一个字符串与链接分开可能会更容易。同样在这种情况下,如果您希望链接可点击,您可能需要使用文本视图。
【解决方案2】:

您可以将标签的文本分成两部分:您从 API 获得的 labelContent,以及您要添加到标签的固定字符串 labelFooter。然后你把属性观察者放在他们身上以更新UILabel

class ViewController: UIViewController {
    @IBOutlet weak var label: UILabel!

    /// The actual text that you get from the API
    var labelContent = "" {
        didSet {
            label.text = labelContent + (isExpanded ? labelFooter : "")
        }
    }

    /// The fixed footer string
    let labelFooter = """


    Website - example.com
    YouTube - youtube.com/example
    Twitter - twitter.com/example
    Instagram - twitter.com/example
    """

    /// Whether or not the label shows the footer
    var isExpanded = false {
        didSet {
            // A trick to trigger `didSet` on `labelContent`
            labelContent = { labelContent }()
        }
    }

    override func viewDidLoad() {
        // Get this from the API
        labelContent = "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."

        // This is a trick to trigger to `didSet` observer on `isExpanded`
        isExpanded = { isExpanded }()
    }

    /// The action to show/hide the footer
    @IBAction func toggleExpand(_ sender: Any) {
        isExpanded = !isExpanded
    }
}

【讨论】:

    猜你喜欢
    • 2018-06-30
    • 1970-01-01
    • 2018-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    • 2014-10-27
    • 1970-01-01
    相关资源
    最近更新 更多