AttributedString icon indicating copy to clipboard operation
AttributedString copied to clipboard

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

Open TouchGuoGuo opened this issue 2 years ago • 3 comments

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

TouchGuoGuo avatar Apr 03 '23 07:04 TouchGuoGuo

等价于调用

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

你可以用

public mutating func add(attributes: [Attribute], range: NSRange) {

lixiang1994 avatar Apr 03 '23 08:04 lixiang1994

这个库,按照作者的回复,应该是基于 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)

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

EdgarDegas avatar Apr 06 '23 05:04 EdgarDegas

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

lixiang1994 avatar Apr 06 '23 11:04 lixiang1994