具有换行符(\ n)的AttributedString无法正常工作

时间:2017-12-04 09:39:58

标签: ios swift3 nsattributedstring nsmutableattributedstring

我在最后添加Continue reading。如果文本中有任何换行符,则继续读取不可见。这是iOS特定的错误还是我遗漏了什么?

let activityData.feed = "Hi this is \n \n stack overflow"

let formattedString = NSMutableAttributedString()
                formattedString.normal(activityData.feed!).bold(LanguageManager.shared.getLocale(key: "Continue Reading"))
                labelFeed.attributedText = formattedString

属性字符串的扩展

extension NSMutableAttributedString {
    @discardableResult func bold(_ text:String) -> NSMutableAttributedString {
        let attrs:[String:AnyObject] = [NSFontAttributeName : UIFont.systemFont(ofSize: 16.0), NSForegroundColorAttributeName : UIColor.CNS_BlueColor]
        let boldString = NSMutableAttributedString(string:"\(text)", attributes:attrs)
        self.append(boldString)
        return self
    }

    @discardableResult func normal(_ text:String)->NSMutableAttributedString {
        let normal =  NSAttributedString(string: text)
        self.append(normal)
        return self
    }
}

enter image description here

1 个答案:

答案 0 :(得分:1)

您是否尝试使用html代码<br>代替\n? 如果您已经有一个attributesstring,请使用html标签的antvantage。