2017-03-29 29 views
3

所以我想讓這樣的事情迅速使用Xcode中和:如何使用Swift製作項目符號列表?

enter image description here

如果我從一個陣列中的每個點。我的想法是製作一個UILabel並創建一個循環遍歷數組,並在每次迭代中添加到標籤\ u202 +內容。我知道\ u202C}是unicode中的點,問題是我需要一種方法將列表分成兩列(如圖所示),並使點點顏色變爲黃色。如果我像上面描述的那樣以編程方式添加點,這是不能完成的,因爲默認顏色是黑色的。由於點的數量根據數組內容而不同,例如數組的大小爲3,那麼只有3個點將顯示左邊2和右邊1我需要一種方法來滿足這個要求,另一種方法我認爲是有兩個表格視圖,佔用一半的屏幕,並根據數組將這些元素添加到每個表格視圖。這裏最好的做法應該是什麼,或者是否有一種方法可以在故事板中以依賴於數組的形式進行創建。

+0

可能需要使用CoreText,請參閱本教程[https://www.raywenderlich.com/4147/core-text-tutorial-for-ios-making-a-magazine-app ) – Tj3n

+1

你看過UICollectionView嗎?我自己並沒有使用它,但是這看起來像是一個完美的用例。 –

+0

所以你真正想知道的是如何畫兩欄文字,子彈部分只是一個紅色的鯡魚,對吧? – matt

回答

5

在列的視圖內使用2個標籤。這兩個標籤被multulined

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 
    } 
} 

,並簡單地使用Helper.bulletedList創建您採用項目符號列表作爲標籤

0

在斯威夫特的製表位屬性文本將與以下工作改變

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

對於斯威夫特4,您可以使用此類:

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 
    } 
} 

使用它:

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