2011-04-04 38 views
69

是否可以將text格式化爲UILabel以顯示項目符號使用子彈點格式化UILabel?

如果是這樣,我該怎麼做?

+0

通過添加無序列表標記(即,

  • UILabel
  • ...
,你可以這樣做。 – Hoque 2011-04-04 02:02:27

+0

@Hoque:UILabel不會將它們的文本視爲HTML。 – 2011-04-04 02:23:32

+2

這裏有一個類! https://codeload.github.com/eyalc/ECListView/zip/master – Hemang 2013-10-23 17:28:29

回答

128

也許使用Unicode代碼點作爲字符串中的子彈字符?

Objective-C的

myLabel.text = @"\u2022 This is a list item!"; 

斯威夫特4

myLabel.text = "\u{2022} This is a list item!" 
+0

我也會使用'UITextField'作爲子彈因爲'UILabel'會更困難。 – Andrew 2011-04-04 03:40:16

+4

@Andrew爲什麼UILabel不如好? – daveMac 2013-03-15 17:32:24

+0

@daveMac'UILabel's通常最容易與一行文本一起使用。當你閱讀更長/更大的文本塊時(格式化子彈的方式),「UITextField」可能會更容易處理 – Andrew 2013-03-16 19:33:02

68

,只需加上 「•」

即使我一直在尋找這樣的事情對我的TextView。我做了什麼,只是將上面的字符串附加到我的字符串中,並將其傳遞給我的textView,對於標籤也是如此。我爲未來的Viewer回答了這個問題。

+0

•爲我工作。我有*在Xcode我只是複製/替換使用•它工作正常我的標籤我用「標籤」替換• – Brian 2017-06-22 17:22:45

6

在SWIFT 3.1

lblItemName.text = "\u{2022} This is a list item!" 
1

退房這個環節,我做了一個自定義視圖來格式化與要點/其他符號/圖像文本(使用的UILabel的attributeText財產)的列表項標誌(雨燕3.0) https://github.com/akshaykumarboth/SymbolTextLabel-iOS-Swift

import UIKit 

    class ViewController: UIViewController { 

    @IBOutlet var symbolView: SymbolTextLabel! 

    var testString = "Understanding the concept of sales" 

    var bulletSymbol = "\u{2022}" 
    var fontsize: CGFloat= 18 
    override func viewDidLoad() { 

     super.viewDidLoad() 
     //First way // Dynamically creating SymbolTextLabel object 

     let symbolTextLabel = SymbolTextLabel(frame: CGRect(x: 0, y: 0, width: 0, height: 0)) 

     symbolTextLabel.setText(text: testString, symbolCode: bulletSymbol) //setting text and symbol of text item 

     symbolTextLabel.setFontSize(textSize: fontsize) // setting font size 

     //symbolTextLabel.setSpacing(spacing: 5) // setting space between symbol and text 

     self.view.addSubview(symbolTextLabel) 
//second way // from storyboard or interface builder 

    symbolView.setText(text: testString, symbolCode: bulletSymbol) 
//setting text and symbol of text item 

    symbolView.setFontSize(textSize: fontsize) // setting font size 

     //symbolView.setSpacing(spacing: 5) // setting space between symbol and text 

     } 
    } 
12

這裏是斯威夫特

let label = UILabel() 
label.frame = CGRect(x: 40, y: 100, width: 280, height: 600) 
label.textColor = UIColor.lightGray 
label.numberOfLines = 0 

let arrayString = [ 
    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", 
    "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", 
    "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.", 
    "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." 
] 

label.attributedText = add(stringList: arrayString, font: label.font, bullet: "") 

self.view.addSubview(label) 
很好的解決方案10

添加子彈屬性

func add(stringList: [String], 
     font: UIFont, 
     bullet: String = "\u{2022}", 
     indentation: CGFloat = 20, 
     lineSpacing: CGFloat = 2, 
     paragraphSpacing: CGFloat = 12, 
     textColor: UIColor = .gray, 
     bulletColor: UIColor = .red) -> NSAttributedString { 

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

    let paragraphStyle = NSMutableParagraphStyle() 
    let nonOptions = [NSTextTab.OptionKey: Any]() 
    paragraphStyle.tabStops = [ 
     NSTextTab(textAlignment: .left, location: indentation, options: nonOptions)] 
    paragraphStyle.defaultTabInterval = indentation 
    //paragraphStyle.firstLineHeadIndent = 0 
    //paragraphStyle.headIndent = 20 
    //paragraphStyle.tailIndent = 1 
    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(
      [NSAttributedStringKey.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 
} 

下面是結果:

enter image description here

1

斯威夫特4我曾用 「•」 新線

@IBOutlet weak var bulletLabel: UILabel! 
let arrayOfLines = ["Eat egg for protein","You should Eat Ghee","Wheat is with high fiber","Avoid to eat Fish "] 
for value in arrayOfLines { 
    bulletLabel.text = bulletLabel.text! + " • " + value + "\n" 
    } 

輸出:

enter image description here