How to allow NSAttributedString text be typed into a UITextView?

前端 未结 2 1887
迷失自我
迷失自我 2020-12-28 22:06

I\'m trying to allow different styles of text to be typed into a UITextView, a bit like a text editor using simple attributes such as bold or italics. I understand by using

相关标签:
2条回答
  • 2020-12-28 22:35

    This is what setTypingAttributes: is for. Set this to your attribute dictionary whenever the user presses one of your attribute buttons and new characters will pick up the requested attributes.

    0 讨论(0)
  • 2020-12-28 22:44

    Here are sample codes to do so if you are looking for examples

    Swift 3.0

    var attributes = textField.typingAttributes
    attributes["\(NSForegroundColorAttributeName)"] = UIColor.red
    textField.typingAttributes = attributes
    

    Objective-C

    NSMutableDictionary* attributes = [textField.typingAttributes mutableCopy];
    attributes[NSForegroundColorAttributeName] = [UIColor redColor];
    textField.typingAttributes = attributes;
    
    0 讨论(0)
提交回复
热议问题