Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

parsing - Swift String Tokenizer / Parser


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If the option of using html tags instead of {B}{/B} is acceptable, then you can use the StringEx library that I wrote for this purpose.

You can select a substring inside the html tag and replace it with another string like this:

let string = "This is a <b>string</b> and this is a substring."
let ex = string.ex

ex[.tag("b")].replace(with: "some value")

print(ex.rawString) // This is a <b>some value</b> and this is a substring.
print(ex.string) // This is a some value and this is a substring.

if necessary, you can also style the selected substrings and get NSAttributedString:

ex[.tag("b")].style([
    .font(.boldSystemFont(ofSize: 16)),
    .color(.black)
])

myLabel.attributedText = ex.attributedString

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...