问题描述
所以我想用swift和xcode做这样的事情:
So I want to make something like this using swift and xcode:
从阵列中获取每个点的位置。我想到的是创建一个UILabel并创建一个遍历数组的for循环,并在每次迭代中添加到标签\u {2022} +内容。我知道\u {2022}是unicode中的点,问题是我需要一种方法将列表分成两列,如图所示,并使点点颜色为黄色。如果我按照上面描述的方式以编程方式添加点,则无法完成,因为默认颜色为黑色。由于点的数量与数组内容不同,例如,如果数组的大小为3,那么只有3个点在左边显示2,而在右边一个我需要一种方法来满足这个要求,我想到的另一种方法是有两个表视图,占用屏幕的一半,并根据数组将这些元素添加到每个表视图。什么应该是最好的做法,或者有一种方法在故事板中以依赖于数组的形式进行此操作。
Where I get each dot from an array. What I have thought is to make a UILabel and make a for loop that iterates over the array and in each iteration it adds to the label \u{2022} + content. I know \u{2022} is the dot point in unicode, the problem is that I need a way to make the list divided in two columns as shown and to make the dot point color yellow. This cant be done if I add the dots programmatically as I described above because the default color would be black. As the number of dots varies from the array contents for example if the array is of size 3 then only 3 dots would show 2 in the left and one to the right I need a way to meet this requirement, the other method I thought was of having two table views that takes half the screen and add this elements to each table view depending on the array. What should be the best practice here or is there a way to make this in the storyboard in a form that is dependent of an array.
推荐答案
在列的视图中使用2个标签。两个标签都是多线的
use 2 labels inside a view for the columns. both labels being multulined
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
}
}
并简单地使用 Helper.bulletedList
创建您的弹出列表作为标签的归属文本
and simply use Helper.bulletedList
to create your bulletted list as Attributed text for the label
这篇关于如何使用Swift制作子弹列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!