format uilabel带有子弹点?

问题描述 投票:0回答:11
可以在

text

中格式化
UILabel
以显示
bulletpoint

如果那么,我该怎么做?

ios objective-c swift uilabel bullet
11个回答
190
投票

对象c

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

swift4


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

即将添加
" • "

97
投票

我正在为我的

textView
寻找这样的东西。我所做的,只需用我的字符串附加字符串,然后将其传递给我的

textView

,也可以为

labels
做同样的事情。
我为将来的观众回答了这一点。
    

在这里是很好的解决方案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)

83
投票
add子弹属性

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 }
there是结果:

    

in

swift4enter image description here我用新线条使用了“•”

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

11
投票

输出:

swift3.1

lblItemName.text = "\u{2022} This is a list item!" enter image description here

是的。 复制并粘贴以下子弹:

8
投票
Swift的编译器可以根据Xcode中的需求解释和显示子弹,无需其他。

Reuse

extension String {
    static var bullet: String {
        return "• "
    }
}


print(String.bullet + "Buy apples")
let secondPoint: String = .bullet + "Buy oranges"
print(secondPoint)

7
投票

输出



• Buy apples • Buy oranges

可调用的阵列

extension Array where Element == String {

    var bulletList: String {
        var po = ""
        for (index, item) in self.enumerated() {
            if index != 0 {
                po += "\n"
            }
            po += .bullet + item
        }
        return po
    }
}


print(["get apples", "get oranges", "get a bannana"].bulletList)

输出

• get apples
• get oranges
• get a bannana

检查此链接,我做了一个自定义视图,以使用子弹点/其他符号/图像(使用uilabel的attributeText属性)作为列表项目符号(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 } }

如果您也想对子弹点排列文本缩进,则可以使用以下方法,该方法具有适当的凹痕和间距属性:

4
投票
NSAttributedString

您可以通过以下方法使用该方法,并通过文本传递- (NSAttributedString *)attributedStringForBulletTexts:(NSArray *)stringList withFont:(UIFont *)font bulletString:(NSString *)bullet indentation:(CGFloat)indentation lineSpacing:(CGFloat)lineSpacing paragraphSpacing:(CGFloat)paragraphSpacing textColor:(UIColor *)textColor bulletColor:(UIColor *)bulletColor { NSDictionary *textAttributes = @{NSFontAttributeName: font, NSForegroundColorAttributeName: textColor}; NSDictionary *bulletAttributes = @{NSFontAttributeName: font, NSForegroundColorAttributeName: bulletColor}; NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new]; paragraphStyle.tabStops = @[[[NSTextTab alloc] initWithTextAlignment: NSTextAlignmentLeft location:indentation options:@{}]]; paragraphStyle.defaultTabInterval = indentation; paragraphStyle.lineSpacing = lineSpacing; paragraphStyle.paragraphSpacing = paragraphSpacing; paragraphStyle.headIndent = indentation; NSMutableAttributedString *bulletList = [NSMutableAttributedString new]; for (NSString *string in stringList) { NSString *formattedString = [NSString stringWithFormat:@"%@\t%@\n", bullet, string]; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:formattedString]; if (string == stringList.lastObject) { paragraphStyle = [paragraphStyle mutableCopy]; paragraphStyle.paragraphSpacing = 0; } [attributedString addAttributes:@{NSParagraphStyleAttributeName: paragraphStyle} range:NSMakeRange(0, attributedString.length)]; [attributedString addAttributes:textAttributes range:NSMakeRange(0, attributedString.length)]; NSRange rangeForBullet = [formattedString rangeOfString:bullet]; [attributedString addAttributes:bulletAttributes range:rangeForBullet]; [bulletList appendAttributedString:attributedString]; } return bulletList; }

,并提供已经有一个:
NSArray

4
投票
UILabel


@krunal
重构Swift 5

NSArray *stringArray = @[@"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 = [self attributedStringForBulletTexts:stringArray withFont:label.font bulletString:@"•" indentation:15 lineSpacing:2 paragraphSpacing:10 textColor:UIColor.blackColor bulletColor:UIColor.grayColor];

扩展中的解决方案:
NSAttributedString

对我来说,解决方案是在uilabel

3
投票
import UIKit public extension NSAttributedString { static func makeBulletList(from strings: [String], bulletCharacter: String = "\u{2022}", bulletAttributes: [NSAttributedString.Key: Any] = [:], textAttributes: [NSAttributedString.Key: Any] = [:], indentation: CGFloat = 20, lineSpacing: CGFloat = 1, paragraphSpacing: CGFloat = 10) -> NSAttributedString { let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.defaultTabInterval = indentation paragraphStyle.tabStops = [ NSTextTab(textAlignment: .left, location: indentation) ] paragraphStyle.lineSpacing = lineSpacing paragraphStyle.paragraphSpacing = paragraphSpacing paragraphStyle.headIndent = indentation let bulletList = NSMutableAttributedString() for string in strings { let bulletItem = "\(bulletCharacter)\t\(string)\n" var attributes = textAttributes attributes[.paragraphStyle] = paragraphStyle let attributedString = NSMutableAttributedString( string: bulletItem, attributes: attributes ) if !bulletAttributes.isEmpty { let bulletRange = (bulletItem as NSString).range(of: bulletCharacter) attributedString.addAttributes(bulletAttributes, range: bulletRange) } bulletList.append(attributedString) } if bulletList.string.hasSuffix("\n") { bulletList.deleteCharacters( in: NSRange(location: bulletList.length - 1, length: 1) ) } return bulletList } }

称其为:

extension UILabel{
func setBulletAttributes(bullet:String , text:String){
    var attributes = [NSAttributedString.Key: Any]()
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.headIndent = (bullet as NSString).size(withAttributes: attributes).width
    attributes[.paragraphStyle] = paragraphStyle
    self.attributedText = NSAttributedString(string: text, attributes: attributes)
}

我已经更新了上面的@@krunal
的答案,这给了我们适当的结果。

1
投票

如果有人在寻找带有像我这样的子弹点的文本文本,下面就是答案。顺便说一句,它仅适用于静态文本。

func createBullets(for list: [String], font: UIFont, bullet: String = " • ", bulletIndent: CGFloat = 0, // Bullet position textIndent: CGFloat = 20, // Wrapped text alignment lineSpacing: CGFloat = 0, paragraphSpacing: CGFloat = 0, textColor: UIColor, bulletColor: UIColor) -> NSAttributedString { let bulletList = NSMutableAttributedString() for string in list { let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.firstLineHeadIndent = bulletIndent // Bullet alignment paragraphStyle.headIndent = textIndent // Wrapped text alignment paragraphStyle.lineSpacing = lineSpacing paragraphStyle.paragraphSpacing = paragraphSpacing let formattedString = "\(bullet) \(string)\n" let attributedString = NSMutableAttributedString(string: formattedString, attributes: [ .font: font, .foregroundColor: textColor, .paragraphStyle: paragraphStyle ]) // Apply bullet color separately let bulletRange = (formattedString as NSString).range(of: bullet) attributedString.addAttributes([.foregroundColor: bulletColor], range: bulletRange) bulletList.append(attributedString) } return bulletList }

我已将上述文本分配给文本视图。它适合我。

这里我添加了带有子弹点的警报视图控制器,希望这有所帮助。
***注意:
这仅适用于uialertViewController

0
投票
• Better experience - Refer a friend and How to Play \n• Tournaments performance improvement\n• UI/UX Improvements\n• Critical bug fixes

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.