如何使用不同的循环重构 Swift 中的 attributedText?

How to refactor attributedText in Swift using different loops?

func attributedText() -> NSAttributedString {
    let string = termsTextView.text as NSString
    let attributedString = NSMutableAttributedString(
        string: string as String,
        attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 15.0)]
    )
    let boldFontAttribute = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 15.0)]
    let semiBoldFontAttribute = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 12.0)]
    attributedString.addAttributes(boldFontAttribute, range: string.range(of: "You"))
    attributedString.addAttributes(boldFontAttribute, range: string.range(of: "Services."))
    attributedString.addAttributes(boldFontAttribute, range: string.range(of: "1. OVERVIEW."))
    attributedString.addAttributes(boldFontAttribute, range: string.range(of: "II. TERMS AND CONDITIONS"))
    attributedString.addAttributes(semiBoldFontAttribute, range: string.range(of: "A. Adults & Children."))
    attributedString.addAttributes(boldFontAttribute, range: string.range(of: "18 Years Old and Older"))
    attributedString.addAttributes(boldFontAttribute, range: string.range(of: "Under 18 Years Old"))
    attributedString.addAttributes(semiBoldFontAttribute, range: string.range(of: "B. Availability"))
    attributedString.addAttributes(semiBoldFontAttribute, range: string.range(of: "C. Authorized Use"))
    attributedString.addAttributes(semiBoldFontAttribute, range: string.range(of: "D. Linking (To & From) the Website; Advertisers"))
    attributedString.addAttributes(semiBoldFontAttribute, range: string.range(of: "E. Privacy Policy"))
    attributedString.addAttributes(semiBoldFontAttribute, range: string.range(of: "F. Prohibition against Harmful Transmissions & Appropriate Use of Website"))
    attributedString.addAttributes(boldFontAttribute, range: string.range(of: "User Content"))
    attributedString.addAttributes(semiBoldFontAttribute, range: string.range(of: "G. Disclaimer of Warranties; Limitation Of Liability"))
    attributedString.addAttributes(semiBoldFontAttribute, range: string.range(of: "I. Indemnification"))
    attributedString.addAttributes(semiBoldFontAttribute, range: string.range(of: "J. Severability"))
    attributedString.addAttributes(semiBoldFontAttribute, range: string.range(of: "K. Entire Agreement & Priority"))
    attributedString.addAttributes(semiBoldFontAttribute, range: string.range(of: "L. Choice Of Law"))
    attributedString.addAttributes(semiBoldFontAttribute, range: string.range(of: "M. Venue; Personal Jurisdiction; Service of Process"))
    attributedString.addAttributes(semiBoldFontAttribute, range: string.range(of: "N. Headings For Convenience Only"))
    attributedString.addAttributes(semiBoldFontAttribute, range: string.range(of: "O. Waiver"))
    attributedString.addAttributes(semiBoldFontAttribute, range: string.range(of: "Q. Copyright Complaints"))
    attributedString.addAttributes(semiBoldFontAttribute, range: string.range(of: "R. Contact"))
    attributedString.addAttributes(semiBoldFontAttribute, range: string.range(of: "S. Local Laws"))
    attributedString.addAttributes(semiBoldFontAttribute, range: string.range(of: "T. Prohibited Conduct"))
    return attributedString
}

这是我在 UITextView 上设置 attributedText 的代码 我正在尝试使用不同的循环进行重构,以尽可能地删除重复的代码。喜欢使用 enumswitch Case 知道我该如何实现吗?感谢

无论您采用何种方法都将相当冗长,因为您需要以某种方式获取所有文本。这种方法可能稍微好一点(假设您只寻找每种方法的一次出现,因为这是上面实现的):

let boldText = ["You", "Services.", "1. Overview", ...]

boldText.forEach{
   attributedString.addAttributes(semiBoldFontAttribute, range: string.range(of: [=10=]}
}

其他属性也一样

IMO 它稍微优雅一些​​,这意味着您可以将数组移出到它们自己的文件中并将它们视为常量。例如

enum LookupTexts {
   static let boldText = ["You", "Services.", "1. Overview", ...]
   static let semiBoldText = [...]
}

这将使调用站点

LookupTexts.boldText.forEach{
   attributedString.addAttributes(semiBoldFontAttribute, range: string.range(of: [=12=]}

这个和原来的有问题 - 如果文本不存在会怎样?您最好检查 if let 中的文本范围,然后仅在得到有效范围时才应用该属性。