2017-03-29 6 views
3

Deshalb mag ich so etwas wie diese mit schnellen und Xcode machen:Wie erstellt man eine Liste mit Swift?

enter image description here

Wo ich jeden Punkt aus einem Array erhalten. Was ich gedacht habe ist, ein UILabel zu erstellen und eine for-Schleife zu erstellen, die über das Array iteriert und in jeder Iteration dem Label \ u {2022} + content hinzufügt. Ich weiß, dass \ u {2022} der Punkt in Unicode ist, das Problem ist, dass ich einen Weg brauche, um die Liste in zwei Spalten wie gezeigt zu teilen und die Punktpunktfarbe gelb zu machen. Dies kann nicht gemacht werden, wenn ich die Punkte programmatisch wie oben beschrieben hinzufüge, weil die Standardfarbe schwarz wäre. Da die Anzahl der Punkte vom Array-Inhalt abweicht, zum Beispiel, wenn das Array die Größe 3 hat, dann würden nur 3 Punkte links und rechts zeigen, dass ich eine Möglichkeit brauche, um diese Anforderung zu erfüllen, die andere Methode, von der ich dachte, war mit zwei Tabellenansichten, die die Hälfte des Bildschirms einnehmen und diese Elemente zu jeder Tabellenansicht hinzufügen, abhängig vom Array. Was sollte hier die beste Vorgehensweise sein, oder gibt es eine Möglichkeit, dies im Storyboard in einer Form zu erstellen, die von einem Array abhängt.

+0

Möglicherweise müssen Core dafür verwenden, finden Sie in der Anleitung [hier] (https://www.raywenderlich.com/4147/core-text-tutorial-for-ios-making-a-magazine-app) – Tj3n

+1

Haben Sie sich UICollectionView angesehen? Ich habe es selbst nicht oft benutzt, aber das sieht nach einem perfekten Anwendungsfall aus. –

+0

Also, was Sie wirklich wissen wollen, ist, wie man Text in zwei Spalten zeichnet, und der Teil der Kugel ist nur ein Ablenkungsmanöver, nicht wahr? – matt

Antwort

5

Verwenden Sie 2 Beschriftungen in einer Ansicht für die Spalten. beide Etiketten

multulined werden
class Helper { 

    static func bulletedList(strings:[String], textColor:UIColor, font:UIFont, bulletColor:UIColor, bulletSize:BulletSize) -> NSAttributedString { 
     let textAttributesDictionary = [NSFontAttributeName : font, NSForegroundColorAttributeName:textColor] 

     let bulletAttributesDictionary = [NSFontAttributeName : font.withSize(bulletSize.rawValue), NSForegroundColorAttributeName:bulletColor] 
     let fullAttributedString = NSMutableAttributedString.init() 

     for string: String in strings 
     { 
      let bulletPoint: String = "\u{2022}" 
      let formattedString: String = "\(bulletPoint) \(string)\n" 
      let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: formattedString) 
      let paragraphStyle = createParagraphAttribute() 

      attributedString.addAttributes([NSParagraphStyleAttributeName: paragraphStyle], range: NSMakeRange(0, attributedString.length)) 
      attributedString.addAttributes(textAttributesDictionary, range: NSMakeRange(0, attributedString.length)) 

      let string:NSString = NSString(string: formattedString) 
      let rangeForBullet:NSRange = string.range(of: bulletPoint) 

      attributedString.addAttributes(bulletAttributesDictionary, range: rangeForBullet) 
      fullAttributedString.append(attributedString) 
     } 
     return fullAttributedString 
    } 

    static func createParagraphAttribute() -> NSParagraphStyle { 

     var paragraphStyle: NSMutableParagraphStyle 
     paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle 
     paragraphStyle.tabStops = [NSTextTab(textAlignment: .left, location: 15, options: NSDictionary() as! [String : AnyObject])] 
     paragraphStyle.defaultTabInterval = 15 
     paragraphStyle.firstLineHeadIndent = 0 
     paragraphStyle.lineSpacing = 3 
     paragraphStyle.headIndent = 10 
     return paragraphStyle 
    } 
} 

und einfach Helper.bulletedList benutzen, um Ihre bulletted Liste als Zugeschrieben Text für das Label

0

In Swift tabstop mit folgenden Änderungen 4

let paragraphStyle = NSMutableParagraphStyle() 
paragraphStyle.minimumLineHeight = 0 // 0 means unlimited 
paragraphStyle.maximumLineHeight = 0 
paragraphStyle.firstLineHeadIndent = 30 
paragraphStyle.headIndent = 0 
paragraphStyle.tabStops = [NSTextTab(textAlignment: .left, location: 15, options: Dictionary<NSTextTab.OptionKey, Any>())] 
paragraphStyle.defaultTabInterval = 10 //changing defaultTabInterval changes the distance between black dot & text 
paragraphStyle.lineSpacing = 5 
0

Für Swift arbeiten zu schaffen, Sie können diese Klasse verwenden:

class NSAttributedStringHelper { 
    static func createBulletedList(fromStringArray strings: [String], font: UIFont? = nil) -> NSAttributedString { 

     let fullAttributedString = NSMutableAttributedString() 
     let attributesDictionary: [NSAttributedStringKey: Any] 

     if font != nil { 
      attributesDictionary = [NSAttributedStringKey.font: font!] 
     } else { 
      attributesDictionary = [NSAttributedStringKey: Any]() 
     } 

     for index in 0..<strings.count { 
      let bulletPoint: String = "\u{2022}" 
      var formattedString: String = "\(bulletPoint) \(strings[index])" 

      if index < strings.count - 1 { 
       formattedString = "\(formattedString)\n" 
      } 

      let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: formattedString, attributes: attributesDictionary) 
      let paragraphStyle = NSAttributedStringHelper.createParagraphAttribute() 
    attributedString.addAttributes([NSAttributedStringKey.paragraphStyle: paragraphStyle], range: NSMakeRange(0, attributedString.length)) 
     fullAttributedString.append(attributedString) 
     } 

     return fullAttributedString 
    } 

    private static func createParagraphAttribute() -> NSParagraphStyle { 
     let paragraphStyle: NSMutableParagraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle 
     paragraphStyle.tabStops = [NSTextTab(textAlignment: .left, location: 15, options: NSDictionary() as! [NSTextTab.OptionKey : Any])] 
     paragraphStyle.defaultTabInterval = 15 
     paragraphStyle.firstLineHeadIndent = 0 
     paragraphStyle.headIndent = 11 
     return paragraphStyle 
    } 
} 

Um es zu verwenden:

let stringArray = ["first row", "second row", "third row"] 
label.attributedText = NSAttributedStringHelper.createBulletedList(fromStringArray: stringArray, font: UIFont.systemFont(ofSize: 15)) 
Verwandte Themen