2011-04-04 60 views
69

¿Es posible formatear text en UILabel para mostrar punto de viñeta?¿Formatear UILabel con viñetas?

Si es así, ¿cómo puedo hacerlo?

+0

Al agregar una etiqueta de lista no ordenada (es decir.

  • UILabel
  • ...
, puede hacerlo. – Hoque

+0

@Hoque: 'UILabel's no tratan su texto como HTML. –

+2

¡Aquí hay una clase para esto! https://codeload.github.com/eyalc/ECListView/zip/master – Hemang

Respuesta

128

¿Quizás use el punto de código Unicode para el carácter de viñeta en su cadena?

Objetivo-c

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

Swift 4

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

También usaría un 'UITextField' para viñetas porque un 'UILabel' va a ser mucho más difícil. – Andrew

+4

@Andrew ¿Por qué un UILabel no es tan bueno? – daveMac

+0

@daveMac 'UILabel's son normalmente los más fáciles de usar con una línea de texto.Cuando se llega a bloques de texto más largos o más grandes (la forma en que se formatea la viñeta), es probable que un 'UITextField' sea más fácil de trabajar con – Andrew

68

sólo tiene que añadir "•"

Incluso yo estaba buscando algo como esto para mi Textview. Lo que hice, solo agregué la cadena anterior con mi cadena y la pasé a mi textView, lo mismo se puede hacer para las etiquetas también. Respondí esto por el futuro espectador.

+0

• Trabajó para mí. Tenía * en Xcode, simplemente copié/reemplacé usando • y funcionó bien para mi Etiqueta que reemplacé "Etiqueta" con • – Brian

6

en Swift 3,1

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

Salida este enlace, hice una vista personalizada para dar formato a texto con viñetas/otros símbolos/imagen (utilizando attributeText propiedad de UILabel) como símbolo de lista de elementos (Swift 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

Aquí es agradable solución con Swift

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) 
Atributos de 10

Añadir bala

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 
} 

Aquí es resultado:

enter image description here

1

En Swift 4 he utilizado "•" con la nueva línea

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

Salida:

enter image description here