所以我想用swift和xcode制作这样的东西:
从阵列中获取每个点的位置.我想到的是创建一个UILabel并创建一个遍历数组的for循环,并在每次迭代中添加到标签\ u {2022} +内容.我知道\ u {2022}是unicode中的点,问题是我需要一种方法将列表分成两列,如图所示,并使点点颜色为黄色.如果我按照上面描述的方式以编程方式添加点,则无法完成,因为默认颜色为黑色.由于点的数量与数组内容不同,例如,如果数组的大小为3,那么只有3个点在左边显示2,而在右边一个我需要一种方法来满足这个要求,我想到的另一种方法是有两个表视图,占用屏幕的一半,并根据数组将这些元素添加到每个表视图.什么应该是这里的最佳实践,或者有一种方法可以在故事板中以依赖于数组的形式进行此操作.
Nic*_* S. 10
我对上述解决方案不满意。所以这里有一个 Swifty 函数来获取项目符号列表:
func bulletPointList(strings: [String]) -> NSAttributedString {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.headIndent = 15
paragraphStyle.minimumLineHeight = 22
paragraphStyle.maximumLineHeight = 22
paragraphStyle.tabStops = [NSTextTab(textAlignment: .left, location: 15)]
let stringAttributes = [
NSAttributedString.Key.font: regularSystem(size: 22),
NSAttributedString.Key.foregroundColor: UIColor.black,
NSAttributedString.Key.paragraphStyle: paragraphStyle
]
let string = strings.map({ "•\t\($0)" }).joined(separator: "\n")
return NSAttributedString(string: string,
attributes: stringAttributes)
}
Run Code Online (Sandbox Code Playgroud)
这就是你如何使用它:
label.numberOfLines = 0
label.attributedText = bulletPointList(strings: ["Foo", "Bar", "Lol"])
Run Code Online (Sandbox Code Playgroud)
小智 6
在列的视图中使用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
}
}
Run Code Online (Sandbox Code Playgroud)
并简单地用于Helper.bulletedList创建您的弹出列表作为标签的归属文本
对于 Swift 5,你可以使用这个类:
class NSAttributedStringHelper {
static func createBulletedList(fromStringArray strings: [String], font: UIFont? = nil) -> NSAttributedString {
let fullAttributedString = NSMutableAttributedString()
let attributesDictionary: [NSAttributedString.Key: Any]
if font != nil {
attributesDictionary = [NSAttributedString.Key.font: font!]
} else {
attributesDictionary = [NSAttributedString.Key: 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([NSAttributedString.Key.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
}
}
Run Code Online (Sandbox Code Playgroud)
要使用它:
let stringArray = ["first row", "second row", "third row"]
label.attributedText = NSAttributedStringHelper.createBulletedList(fromStringArray: stringArray, font: UIFont.systemFont(ofSize: 15))
Run Code Online (Sandbox Code Playgroud)