Forum Archive

Give Attributes to Specific TextView Index Range

AtomBombed

My last post consisted of me asking how to give attributes to characters you are currently typing.

This thread's about how to give attributes to a specific range of text in a TextView object. I already understand how to give attributed text to the characters you are typing, but not to the ones already typed. This is my big issue.

If anyone could show me some examples, and give insights, it would be very much appreciated.

Thanks,

Sean

JonB

you will want to take a look at

https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableAttributedString_Class/index.html#//apple_ref/occ/instm/NSMutableAttributedString/

addAttibutes_range_ will let you add an attribute -- first argument is a dictionary of attributes, second is a NSRange object, which can be created by NSRange(start, length).
you can also use addAttribute_value_range_ where you use a single string attribute name and value rather than packed in a dictionary.

Also of interest may be the corresponding removeAttribute... and setAttribute... methods.

I am not sure, but you might need to preceed these with a beginEditing and end with endEditing, and might need to use on_main_thread. Also, this needs to be a NSMutableAttributedAtring, which is writable.

AtomBombed

Oh, you're a life saver. Thank you so much. I was looking all over the developer website and couldn't find that.