I am trying to create a custom keyboard for iOS using the images that I entered as buttons. When I click the button, the image associated with the button is placed in the attribute string, which is loaded into the UiTextView inside the user-defined keyboard view. It works.
The problem is that when adding a new image to the attribute string, both old and new images in the string change to the image that I just clicked. I cannot understand why the old images in the line are changing.
Any suggestions? I tried using replaceCharactersInRange and insertAttributedString but cant get it working. Here is the code (after viewDidLoad):
let textAttachment = NSTextAttachment() let textView = UITextView(frame: CGRectMake(5, 5, 200, 40)) var attributedString = NSMutableAttributedString(string: "") @IBAction func buttonPressed(button :UIButton) { let string = button.titleLabel?.text textAttachment.image = UIImage(named: "\(string!).png")! textAttachment.image = UIImage(CGImage: textAttachment.image!.CGImage!, scale: 6, orientation: .Up) let attrStringWithImage = NSAttributedString(attachment: textAttachment) attributedString.appendAttributedString(attrStringWithImage); textView.attributedText = attributedString; }
Thanks!
ios iphone swift custom-keyboard ios-app-extension
knorrhane
source share