Membuat Format Bullet & Numbering di Swift

Belajar membuat format bullet and numbering di swift dengan mudah.  Untuk membuat pemformatan tersebut buatlah code seperti berikut :

format bullet

Tuliskan koding berikut :

//belajaraplikasi.com
//===================

import UIKit

var source = "https://belajaraplikasi.com"
var str = "Belajar Membuat Format Bullet And Numbering"
var line = "------------------------------------------\n"

//Fungsi untuk format bullet & numbering
//------------------------------------------
func add(
stringList: [String], 
font: UIFont, 
bullet: String = "\u{2022}", 
indentation: CGFloat = 20, 
lineSpacing: CGFloat = 2, 
paragraphSpacing: CGFloat = 12, 
textColor: UIColor = .black, 
bulletColor: UIColor = .black) -> NSAttributedString {

   let textAttributes: [NSAttributedString.Key: Any] = 
    [NSAttributedString.Key.font: font, NSAttributedString.Key.foregroundColor: textColor]
   let bulletAttributes: [NSAttributedString.Key: Any] = 
    [NSAttributedString.Key.font: font, NSAttributedString.Key.foregroundColor: bulletColor]

   let paragraphStyle = NSMutableParagraphStyle()
   let nonOptions = [NSTextTab.OptionKey: Any]()
paragraphStyle.tabStops = [
NSTextTab(textAlignment: .left, location: indentation, options: nonOptions)]
   paragraphStyle.defaultTabInterval = indentation
   paragraphStyle.lineSpacing = lineSpacing
   paragraphStyle.paragraphSpacing = paragraphSpacing
   paragraphStyle.headIndent = indentation

   let bulletList = NSMutableAttributedString()
   for string in stringList {
      let formattedString = "\(bullet)\t\(string)\n"
      let attributedString = NSMutableAttributedString(string: formattedString)

      attributedString.addAttributes(
[NSAttributedString.Key.paragraphStyle : paragraphStyle],
range: NSMakeRange(0, attributedString.length))

      attributedString.addAttributes(
textAttributes,
range: NSMakeRange(0, attributedString.length))

      let string:NSString = NSString(string: formattedString)
      let rangeForBullet:NSRange = string.range(of: bullet)
      attributedString.addAttributes(bulletAttributes, range: rangeForBullet)
      bulletList.append(attributedString)
   }
   return bulletList
}

//Penggunaan fungsi
//-----------------
var stringArray = [
   "Budi juara 1 makan krupuk sekarung",
   "Shinta juara 1 balap karung",
   "Bagong juara 2 masukin pulpen ke dalam botol\n\n",
]
print(source)
print(str)
print(line)
print(
   add(
      stringList: stringArray, 
      font: .boldSystemFont(ofSize: 12), 
      bullet: "•",
   )
) //membuat bullet dengan menekan keyboard alt + 8

Sekian dan terima kasih,
semoga membantu 🙂

Leave a Reply

Your email address will not be published.