根据传入的值显示大量文本

时间:2017-01-16 19:14:46

标签: ios swift

我正在使用Swift 3开发iOS程序。我的工作正常,按下按钮时会输出一个数字,然后在标签中显示文字。

但是文字相当多,而且会有10种变体。是否有更好的方法,而不是在VC中,可能是XML文件或10?

我知道这很麻烦,把它放在UIViewController子类中,并不喜欢这个想法。

func infoArea () {

    if step == 1 {

        infoBox.text = "The purpose of this track - in a nutshell: Stop eating foods which pile on the pounds. Develop less desire for fattening and unhealthy foods and drinks.\n \n \nSlim for Life - Step 1: Less Desire \n \n \nWhile listening you'll experience: \n \n • A deeply therapeutic meditative process \n • A feeling of whole mind and body relaxation \n • An opportunity to embrace change and become totally focused on releasing excess weight \n • The ability to develop less desire for foods and drinks that do you harm and more desire for those that do you good \n • A chance to take control as you recognise that this is not another diet \n • A life-changing experience! \n \n \nAfterwards and with repeated listening you'll experience: \n \n • Feelings of calm, inner peace and a greater sense of security \n • Less mind chatter and more clarity as you take control of your thoughts \n • Feelings of not fancying unhealthy and fattening foods and drinks \n • Wondering if your taste buds are changing as you no longer enjoy sweet things \n • An ability to comfortably ignore snacks, sweets and junk foods \n • Determination to succeed \n • Increased energy levels as you develop the habits of a naturally slim person \n • Improved health as you’re sleeping better and feeling more nourished from sleep \n • A gentle connection between your mind and your body that promotes healing \n • More self-confidence \n \n \n \nPermanent weight release is something you have been keeping from happening. \nWhether your thoughts and feelings about yourself are good or bad, they will return to you as automatically as an echo. \nBe sure to send yourself only good, strong, positive thoughts, feelings and emotions. \n \nLearn to love yourself."

        let range = NSMakeRange(0, 152)
        let range2 = NSMakeRange(193, 1470)
        infoBox.attributedText = attributedString(from: infoBox.text, nonBoldRange: range, nonBoldRange2: range2)


    } else if step == 2 {
        infoBox.text = "This is going to be information number 2"

    } else if step == 3 {
        infoBox.text = "this will be number 3"

2 个答案:

答案 0 :(得分:0)

要简单地以更方便的方式存储文本,即使文本很长,我也不建议使用XML,也不建议使用10个不同的XML

让我们创建一个小帮助器结构,什么将保存静态String类型Array,并通过访问正确的索引从中获取所有东西。但是,您可以在texts子类上声明UIViewController变量,这不会造成太大的伤害。

struct DisplayableContents {
    static let texts: [String] = [
    "first long text",
    "second long text",
    "third long text",
    "etc..."
    ]
}

您可以通过以下方式在UIViewController子类中使用它:

func infoArea () {
  infoBox.text = DisplayableContents.texts[step]
} 

答案 1 :(得分:0)

我遇到了类似的问题并使用plist解决了它包含“step”信息(然后您可以将其用作字典)。对于每个步骤,我在plist中有一个“描述”条目用于简短描述,而fileName条目用于较长的描述。我将这个plist / dictionary包装在一个可以返回属性的小类中,如果需要,可以获取文件内容(在我的情况下,它是项目文件的一部分)。