【问题标题】:How to keep a combination of attributes on a string in swift如何在swift中保持字符串上的属性组合
【发布时间】:2022-01-20 20:47:25
【问题描述】:

我有一个需要设置属性的字符串。属性(粗体、斜体、删除线、下划线)被单独添加,但它们并没有与字符串保持完整。我想知道如何用字符串保持不同的属性不变,然后根据需要将它们一一删除。

Bold - 

   if textBold == true {
                            let string = text
                            let attributes: [NSAttributedString.Key: Any] = [
                                .font: UIFont.boldSystemFont(ofSize: 25)
                            ]
                            let attributedString = NSAttributedString(string: string, attributes: attributes)
                            modifiedString = attributedString
                            text_View.attributedText = modifiedString

                            text_View.sizeToFit()
                            databaseHandlerObj.editLabelData(textProjectId: fetchTextProjectId, text_Id: fetchTextId, text: modifiedString.string)
                        }

Italic - 
        if textItalic == true {

                            text_View.sizeToFit()
                            let string = text
                            
                            let attributes: [NSAttributedString.Key: Any] = [
                                .font: UIFont.italicSystemFont(ofSize: CGFloat(fontSize))
                            ]
                            let attributedString = NSAttributedString(string: string, attributes: attributes)
                            self.modifiedString = attributedString
                            text_View.attributedText = modifiedString
                            
                            databaseHandlerObj.editLabelData(textProjectId: fetchTextProjectId, text_Id: fetchTextId, text: modifiedString.string)
                        }

Underline -
   if textUnderline == true {
                            text_View.sizeToFit()
                            let string = text
                            let attributedString = NSMutableAttributedString.init(string: string)
                            attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: 1, range:
                                                            NSRange.init(location: 0, length: attributedString.length))
                            self.modifiedString = attributedString
                            text_View.attributedText = modifiedString
                            
                            databaseHandlerObj.editLabelData(textProjectId: fetchTextProjectId, text_Id: fetchTextId, text: modifiedString.string)
                        }
                        

Strikethrough - 
      if textStrikethrough == true {
                            text_View.sizeToFit()
                            let string = text
                            let attributeString: NSMutableAttributedString =  NSMutableAttributedString(string: string)
                            attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length))
                            self.modifiedString = attributeString
                            text_View.attributedText = modifiedString
                            databaseHandlerObj.editLabelData(textProjectId: fetchTextProjectId, text_Id: fetchTextId, text: modifiedString.string)
                        }

【问题讨论】:

    标签: ios swift string uitextview nsattributedstring


    【解决方案1】:

    在属性字典上添加条件。

    let string = text
    var attributes: [NSAttributedString.Key: Any] = [:]
    
    if bold {
        attributes[.font] = UIFont.boldSystemFont(ofSize: 25)
    }
    if italic {
        attributes[.font] = UIFont.italicSystemFont(ofSize: CGFloat(fontSize))
    }
    if underline {
        attributes[.underlineStyle] = 1
    }
    
    let attributedString = NSAttributedString(string: string, attributes: attributes)
    // Other Code
    
    

    【讨论】:

    • 哇!非常感谢。有效。几天以来,我一直坚持这一点。根据代码,文本保持粗体或斜体样式。我现在唯一的问题是,如何同时在文本上保持粗体和斜体样式。
    • 对于粗体和斜体,您需要使用粗体和斜体的字体。见stackoverflow.com/questions/38533323/…
    • 不知道为什么 OP 接受了这个答案。它甚至无法回答这个问题
    • @leo 我的回答有什么问题??
    • 如果字体是斜体,它会简单地覆盖粗体
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 1970-01-01
    • 1970-01-01
    • 2015-10-17
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    相关资源
    最近更新 更多