Tôi có những điều sau đây cho phép tôi tạo danh sách dấu đầu dòng hoạt động thực sự tốt, tuy nhiên, sau khi danh sách dấu đầu dòng được tạo, tôi cần thao tác chuỗi được phân bổ đã xuất hoặc in đậm hoặc in nghiêng hoặc cả hai.Điều chỉnh các phần tử sau khi thao tác chuỗi thành AttributedString - Swift 4
Chức năng tôi có là:
@IBOutlet var label: UILabel!
let bulletString = ["String 1","String 2","String 3"]
label.attributedText = label.bulletPoints(stringList: bulletString, font: UIFont.stdFontMediumSeventeen, bullet: "•", lineSpacing: 4, paragraphSpacing: 4, textColor: UIColor.darkGreyColor, bulletColor: UIColor.darkGreyColor)
func bulletPoints(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.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
}
Những gì tôi đang tìm kiếm là một cách để vượt qua trong một boolean nêu nếu chuỗi đạn đòi hỏi một trong hai chữ in đậm hoặc in nghiêng và nếu như vậy những gì các yếu tố của chuỗi intital là cần điều trị này.
Chức năng bulletPoints
nằm trong tệp mở rộng và hoạt động như mong đợi.
Không rõ bạn đang cố gắng đạt được điều gì .. Bạn có muốn sửa đổi chuỗi được phân bổ hiện tại bằng cách áp dụng các thuộc tính mới không? Hoặc bạn muốn tham số mới trong hàm chịu trách nhiệm in đậm/in nghiêng? –
Vì vậy, có, tôi đang tìm một hàm mà tôi có thể gọi trong hàm trên để đặt các phần nhất định của chuỗi thành chữ đậm hoặc in nghiêng hoặc cả hai, nhưng đối với hàm trên có thể sử dụng được mà không có các phần tử này. –
Tôi không hiểu mục tiêu của bạn là gì. Làm thế nào để bạn biết nơi để áp dụng đậm/nghiêng (trong khác, nơi áp dụng một phông chữ mới)? Nếu nó nằm trên một mục của danh sách, giống như bạn đã làm 'attributedString.addAttributes ([NSAttributedStringKey.paragraphStyle: paragraphStyle], phạm vi: NSMakeRange (0, attributedString.length))' làm tương tự với 'NSAttributedStringKey.font' thay thế và một phông chữ tùy chỉnh. – Larme