2017-03-29 8 views
3

は、だから私は、この使用して迅速かつXcodeのようなものを作りたいです。私が考えているのは、UILabelを作成し、forループを作成して配列全体を反復し、各反復でlabel \ u {2022} + contentに追加することです。私は\ u {2022}がユニコードのドット点であることを知っていますが、問題は、リストを2つの列に分割して点の色を黄色にする方法が必要だということです。これは既定の色が黒であるため、上記のようにプログラムでドットを追加するとできません。ドットの数が配列の内容から変化すると、たとえば配列のサイズが3の場合、3ドットだけが左に2つ、右に1つ表示されます。この要件を満たす方法が必要です。 2つのテーブルビューで画面の半分を占め、この要素を配列に応じて各テーブルビューに追加します。ここでのベストプラクティスは何か、または配列に依存する形式でストーリーボードにこれを作成する方法があります。Swiftで弾丸を作る方法は?私は、アレイからの各ドットを得る</p> <p><a href="https://i.stack.imgur.com/gTnXC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gTnXC.png" alt="enter image description here"></a></p> <p>:

+0

はチュートリアル[ここ](https://www.raywenderlich.com/4147/core-text-tutorial-for-ios-making-a-magazine-appを参照し、そのためにCoreTextを使用する必要があります) – Tj3n

+1

あなたはUICollectionViewを見ましたか?私はそれ自身をあまり使用していませんが、これはこのための完全な使用例のように見えます。 –

+0

本当に知っておきたいのは、2つの列にテキストを描画する方法であり、箇条書きの部分はちょうど赤いニシンですよね? – matt

答えて

5

2つのラベルを列のビュー内で使用します。両方のラベルが

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

をmultulinedし、単に次のように動作しますスウィフトTABSTOPで

0

ラベルの属性付きテキストとしてあなたbullettedリストを作成するHelper.bulletedListを使用して変更されている

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 
スウィフト4については
0

、あなたは、このクラスを使用することができます。

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)) 
関連する問題

 関連する問題