当前位置: 动力学知识库 > 问答 > 编程问答 >

objective c - Having trouble interperating ansi color codes

问题描述:

I have spent the weekend working on a personal project and got stuck here. Basically, I need to turn

[0;37m[33m o0==============================~o[0]o~==============================0o

into

o0==============================~o[0]o~==============================0o (only this text would be yellow now)

Using cocoa's regex functionality, I was able to find and capture the "[0;", "37m" and "[33m" individually. the "0;" indicates the server's desire for any previous text styling to be removed and returned to the default which is black background and white text. The "37m" indicates that the server would like for text to be colored white (not sure why this is here, but this is what the server sends). The final "33m" indicates that the server wants the text to be colored yellow. My code correctly finds, strips out, and identifies the requested color changes in the string, but I am having trouble applying these colors to the NSAttributedString I create. The ranges supplied by the regex searches are no longer valid once I strip out the color sequences in the final string, what is an effective way to figure out where the color changes should be applied to the stripped string? In this example, all the color codes are supplied at the beginning, but in other cases, the color codes could be in the middle to cause the string to change color mid-line. NSAttributedString can handle this if I could figure out the proper ranges to assign the requested colors to.

网友答案:

Now that Lion is out I can post the answer. Basically you can use the fancy regex abilities in Lion to figure out what is up. The code to do this (which needs to be refactored, but at least it works) can be found here:

https://github.com/sgoodwin/Turbo-Mud/blob/experiment/Turbo%20Mud/Turbo_MudAppDelegate.m

分享给朋友:
您可能感兴趣的文章:
随机阅读: