Monday, 27 March 2017

Set attributed text in UITextView dynamically

In UITextView we can set both plain and attributed text. We can easily monitor the changes in the state of UITextView by its delegate methods.

Today we will see, how to set the attributed string in a  UITextView. For example, to set attributed string dynamically , we can change the attribute for a string in shouldChangeTextIn range: delegate method.

To set the attributed use typingAttributes property of the UITextView in   shouldChangeTextIn range: method:

 textView.typingAttributes = [NSFontAttributeName:UIFont.systemFont(ofSize: 17, weight: UIFontWeightMedium)]

The above line of code set the attribute for the whole text in textView. For changing attributes for specific case assign different attributes to typingAttributes property.Now you will see your text will appear with attributes you set to the typingAttributes property as you type.




No comments:

Post a Comment