lixiang1994 / AttributedString

基于Swift插值方式优雅的构建富文本, 支持点击长按事件, 支持不同类型过滤, 支持自定义视图等.
MIT License
846 stars 76 forks source link

根据多个Range设置字体颜色时,只有最后一个生效 #59

Open TouchGuoGuo opened 1 year ago

TouchGuoGuo commented 1 year ago

使用该方法public mutating func set(attributes: [Attribute], range: NSRange)设置字体颜色 在多个range的情况下只有最后一个生效

lixiang1994 commented 1 year ago

等价于调用

func setAttributes(_ attrs: [NSAttributedString.Key : Any]?, range: NSRange)

你可以用

public mutating func add(attributes: [Attribute], range: NSRange) {
EdgarDegas commented 1 year ago

这个库,按照作者的回复,应该是基于 NSAttributedString,所以在确定range的时候,应该先将Swift.String转化成NSString。比如:

let length = "🤔".count
let range = NSRange(location: 0, length: length)

应该改成:

let length = ("🤔" as NSString).length
let range = NSRange(location: 0, length: length)

我脱机敲的,不知道准不准确。

lixiang1994 commented 1 year ago

或者可以用"🤔".utf8.count 等于 ("🤔" as NSString).length