我怎样才能输入表情符号或输入任何东西

Posted

技术标签:

【中文标题】我怎样才能输入表情符号或输入任何东西【英文标题】:How can I make the type emoji or type anything 【发布时间】:2015-06-23 09:17:29 【问题描述】:

我目前正在开发自定义键盘 当我尝试在新窗口中组合 textDocumentProxy 时遇到问题

所以添加了表情符号键盘按钮 我通过页面添加了两个表情符号按钮现在我的问题是如何合并 textDocumentProxy 另一个窗口或它们之间的链接减弱甚至可以输入表情符号

我尝试输入任何内容,而不仅仅是表情符号

完整的源代码在这里

https://github.com/archagon/tasty-imitation-keyboard

import UIKit

class DefaultSettings1: ExtraView, UITableViewDelegate 

    @IBOutlet var tableView1: UITableView?
    @IBOutlet var effectsView: UIVisualEffectView?
    @IBOutlet var backButton1: UIButton?
    @IBOutlet var settingsLabel: UILabel?
    @IBOutlet var pixelLine: UIView?
    @IBOutlet var keyPressed: UIButton?



    override var darkMode: Bool 
        didSet 
            self.updateAppearance(darkMode)
        
    

    let cellBackgroundColorDark = UIColor.whiteColor().colorWithAlphaComponent(CGFloat(0.25))
    let cellBackgroundColorLight = UIColor.whiteColor().colorWithAlphaComponent(CGFloat(1))
    let cellLabelColorDark = UIColor.whiteColor()
    let cellLabelColorLight = UIColor.blackColor()
    let cellLongLabelColorDark = UIColor.lightGrayColor()
    let cellLongLabelColorLight = UIColor.grayColor()

    // TODO: these probably don't belong here, and also need to be localized
      required init(globalColors: GlobalColors.Type?, darkMode: Bool, solidColorMode: Bool) 
        super.init(globalColors: globalColors, darkMode: darkMode, solidColorMode: solidColorMode)
        self.loadNib()
    



    required init(coder aDecoder: NSCoder) 
        fatalError("loading from nib not supported")
    

    func loadNib() 
        let assets = NSBundle(forClass: self.dynamicType).loadNibNamed("DefaultSettings1", owner: self, options: nil)

        if assets.count > 0 
            if var rootView = assets.first as? UIView 
                rootView.setTranslatesAutoresizingMaskIntoConstraints(false)
                self.addSubview(rootView)

                let left = NSLayoutConstraint(item: rootView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0)
                let right = NSLayoutConstraint(item: rootView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0)
                let top = NSLayoutConstraint(item: rootView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0)
                let bottom = NSLayoutConstraint(item: rootView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)

                self.addConstraint(left)
                self.addConstraint(right)
                self.addConstraint(top)
                self.addConstraint(bottom)
            
        

        self.tableView1?.registerClass(DefaultSettings1TableViewCell.self, forCellReuseIdentifier: "cell")
        self.tableView1?.estimatedRowHeight = 44;
        self.tableView1?.rowHeight = UITableViewAutomaticDimension;

        // XXX: this is here b/c a totally transparent background does not support scrolling in blank areas
        self.tableView1?.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.01)

        self.updateAppearance(self.darkMode)
    



    func tableView1(tableView1: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
        if var cell = tableView1.dequeueReusableCellWithIdentifier("cell") as? DefaultSettings1TableViewCell 

            if cell.sw.allTargets().count == 0 
                cell.sw.addTarget(self, action: Selector("toggleSetting1:"), forControlEvents: UIControlEvents.ValueChanged)
            



            cell.backgroundColor = (self.darkMode ? cellBackgroundColorDark : cellBackgroundColorLight)
            cell.label.textColor = (self.darkMode ? cellLabelColorDark : cellLabelColorLight)
            cell.longLabel.textColor = (self.darkMode ? cellLongLabelColorDark : cellLongLabelColorLight)

            cell.changeConstraints()

            return cell
        
        else 
            assert(false, "this is a bad thing that just happened")
            return UITableViewCell()
        
    




    func updateAppearance(dark: Bool) 
        if dark 
            self.effectsView?.effect
            let blueColor = UIColor(red: 135/CGFloat(255), green: 206/CGFloat(255), blue: 250/CGFloat(255), alpha: 1)
            self.pixelLine?.backgroundColor = blueColor.colorWithAlphaComponent(CGFloat(0.5))
            self.backButton1?.setTitleColor(blueColor, forState: UIControlState.Normal)
            self.settingsLabel?.textColor = UIColor.whiteColor()

            if let visibleCells = self.tableView1?.visibleCells() 
                for cell in visibleCells 
                    if var cell = cell as? UITableViewCell 
                        cell.backgroundColor = cellBackgroundColorDark
                        var label = cell.viewWithTag(2) as? UILabel
                        label?.textColor = cellLabelColorDark
                        var longLabel = cell.viewWithTag(3) as? UITextView
                        longLabel?.textColor = cellLongLabelColorDark
                    
                
            
        
        else 
            let blueColor = UIColor(red: 0/CGFloat(255), green: 122/CGFloat(255), blue: 255/CGFloat(255), alpha: 1)
            self.pixelLine?.backgroundColor = blueColor.colorWithAlphaComponent(CGFloat(0.5))
            self.backButton1?.setTitleColor(blueColor, forState: UIControlState.Normal)
            self.settingsLabel?.textColor = UIColor.grayColor()

            if let visibleCells = self.tableView1?.visibleCells() 
                for cell in visibleCells 
                    if var cell = cell as? UITableViewCell 
                        cell.backgroundColor = cellBackgroundColorLight
                        var label = cell.viewWithTag(2) as? UILabel
                        label?.textColor = cellLabelColorLight
                        var longLabel = cell.viewWithTag(3) as? UITextView
                        longLabel?.textColor = cellLongLabelColorLight
                    
                
            
        
    

    

class DefaultSettings1TableViewCell: UITableViewCell 
       var sw: UISwitch
    var label: UILabel
    var longLabel: UITextView
    var constraintsSetForLongLabel: Bool
    var cellConstraints: [NSLayoutConstraint]

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) 
        self.sw = UISwitch()
        self.label = UILabel()
        self.longLabel = UITextView()
        self.cellConstraints = []

        self.constraintsSetForLongLabel = false

        super.init(style: style, reuseIdentifier: reuseIdentifier)

        self.sw.setTranslatesAutoresizingMaskIntoConstraints(false)
        self.label.setTranslatesAutoresizingMaskIntoConstraints(false)
        self.longLabel.setTranslatesAutoresizingMaskIntoConstraints(false)

        self.longLabel.text = nil
        self.longLabel.scrollEnabled = false
        self.longLabel.selectable = false
        self.longLabel.backgroundColor = UIColor.clearColor()

        self.sw.tag = 1
        self.label.tag = 2
        self.longLabel.tag = 3

        self.addSubview(self.sw)
        self.addSubview(self.label)
        self.addSubview(self.longLabel)

        self.addConstraints()
    

    required init(coder aDecoder: NSCoder) 
        fatalError("init(coder:) has not been implemented")
    

    func addConstraints() 
        let margin: CGFloat = 8
        let sideMargin = margin * 2

        let hasLongText = self.longLabel.text != nil && !self.longLabel.text.isEmpty
        if hasLongText 
            let switchSide = NSLayoutConstraint(item: sw, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: -sideMargin)
            let switchTop = NSLayoutConstraint(item: sw, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: margin)
            let labelSide = NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: sideMargin)
            let labelCenter = NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: sw, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)

            self.addConstraint(switchSide)
            self.addConstraint(switchTop)
            self.addConstraint(labelSide)
            self.addConstraint(labelCenter)

            let left = NSLayoutConstraint(item: longLabel, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: sideMargin)
            let right = NSLayoutConstraint(item: longLabel, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: -sideMargin)
            let top = NSLayoutConstraint(item: longLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: sw, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: margin)
            let bottom = NSLayoutConstraint(item: longLabel, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -margin)

            self.addConstraint(left)
            self.addConstraint(right)
            self.addConstraint(top)
            self.addConstraint(bottom)

            self.cellConstraints += [switchSide, switchTop, labelSide, labelCenter, left, right, top, bottom]

            self.constraintsSetForLongLabel = true
        
        else 
            let switchSide = NSLayoutConstraint(item: sw, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: -sideMargin)
            let switchTop = NSLayoutConstraint(item: sw, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: margin)
            let switchBottom = NSLayoutConstraint(item: sw, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -margin)
            let labelSide = NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: sideMargin)
            let labelCenter = NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: sw, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)

            self.addConstraint(switchSide)
            self.addConstraint(switchTop)
            self.addConstraint(switchBottom)
            self.addConstraint(labelSide)
            self.addConstraint(labelCenter)

            self.cellConstraints += [switchSide, switchTop, switchBottom, labelSide, labelCenter]

            self.constraintsSetForLongLabel = false
        
    

    // XXX: not in updateConstraints because it doesn't play nice with UITableViewAutomaticDimension for some reason
    func changeConstraints() 
        let hasLongText = self.longLabel.text != nil && !self.longLabel.text.isEmpty
        if hasLongText != self.constraintsSetForLongLabel 
            self.removeConstraints(self.cellConstraints)
            self.cellConstraints.removeAll()
            self.addConstraints()
        

    




【问题讨论】:

所以您希望无法在自定义键盘中设置视图中的表情符号按钮? 是的,你能帮帮我吗?对不起,我可能无法很好地解释自己,但我很高兴将源代码发送给您,那么如何更好地理解我 是的,当用户启用表情符号按钮时,您可以将更改存储到NSUserDefaults 我给你发了一封感谢信 【参考方案1】:

您需要根据您的要求将许多内容更改到您的项目中,例如您可以通过隐藏该 tableView 并在其上添加一个 UIView 来使用该设置视图,然后您可以将表情符号添加到该视图中,并且当用户单击设置按钮时键盘您可以隐藏您的表情符号视图并显示表格视图,但是当用户想要键入不需要隐藏任何内容的微笑时,只需显示您的表情符号视图。

HERE 是您更新的项目。

【讨论】:

以上是关于我怎样才能输入表情符号或输入任何东西的主要内容,如果未能解决你的问题,请参考以下文章

带有 android 4.4 的谷歌键盘中的默认表情符号按钮

UITextField - 防止用户输入非标准字符(如表情符号)

如何使用 javascript 检测表情符号

如何使用 javascript 检测表情符号

Discord.js 发送表情符号

微信符号表情怎么打?