IOS 自定义键盘。如何在自定义键盘中设置特殊字符

IOS Customkeyboard. How to set special character in custom keyboard

我正在为 iphone 6 和 6Plus 开发自定义键盘。我已经为 shift key active-inactive 或 123 和 abc key 开发了逻辑。但我也想显示自定义符号,如所示的苹果键盘。请查看图片。

目前我可以将大写字母更改为小写字母,将 123 更改为 abc,但我该如何设置这个特殊字符。 注意:- 我在 github 上搜索了很多演示,但我无法在任何自定义键盘中看到特殊字符。

这是我对 大写ABC-123 按钮的逻辑。

-(void)UpparCase:(UIButton*)sender   // To make caps or small letter
{

sender.selected=!sender.selected;
if(sender.selected)
{
    if(isPortrait)
    {

    [self.ObjKeyLayout.btnCapKey setBackgroundImage:[UIImage imageNamed:@"upparActive.png"] forState:UIControlStateNormal];
    }
    else
    {
        [self.ObjKeyLayout.btnCapKey setBackgroundImage:[UIImage imageNamed:@"Uppar_selctd_land.png"] forState:UIControlStateNormal];

    }

    for(UIButton *key in self.ObjKeyLayout.ArryCharKey)
    {


        NSString *uppercaseString = [[key currentTitle] uppercaseString];

        [key setTitle:uppercaseString forState:UIControlStateNormal];

        [key addTarget:self action:@selector(pressKey:) forControlEvents:UIControlEventTouchUpInside];

    }
   }
   else
  {

    if(isPortrait)
    {

    [self.ObjKeyLayout.btnCapKey setBackgroundImage:[UIImage imageNamed:@"UpperCase.png"] forState:UIControlStateNormal];
    }
    else
    {
        [self.ObjKeyLayout.btnCapKey setBackgroundImage:[UIImage imageNamed:@"Uppar_land.png"] forState:UIControlStateNormal];

    }
    for(UIButton *key in self.ObjKeyLayout.ArryCharKey)
    {


        NSString *uppercaseString = [[key currentTitle] lowercaseString];

        [key setTitle:uppercaseString forState:UIControlStateNormal];

        [key addTarget:self action:@selector(pressKey:) forControlEvents:UIControlEventTouchUpInside];

      }

   }
}

- (IBAction)action123:(UIButton*)sender // To set 123 and ABC
 {


 sender.selected=!sender.selected;

 if(sender.selected)
 {

    [self.ObjKeyLayout.btn123Key setTitle:@"ABC" forState:UIControlStateNormal];
            int i=0;

    for(UIButton *key in self.ObjKeyLayout.ArryCharKey)
    {

        [key setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
        NSLog(@"\n\n123isSelected\nI-%i\tArrayCharKey.Count:-%i",i,(int)self.ObjKeyLayout.ArryCharKey.count);
        if(isPortrait)
        {
             NSLog(@"\n\n123isSelected\nI-%i\tArrayCharKey.Count:-%i\nArraySpecialImageKey.Count-%i\n",i,(int)self.ObjKeyLayout.ArryCharKey.count,(int)arrspecialImageKey.count);
            [key setBackgroundImage:[UIImage imageNamed:[arrspecialImageKey objectAtIndex:i]] forState:UIControlStateNormal];




        }
        else{

            [key setBackgroundImage:[UIImage imageNamed:[arrSpecialIpad objectAtIndex:i]] forState:UIControlStateNormal];

        }

        [key setTitle:[NumKey objectAtIndex:i] forState:UIControlStateNormal];

        [key addTarget:self action:@selector(pressKey:) forControlEvents:UIControlEventTouchUpInside];
        i++;
    }
  }
  else
     {
    [self.ObjKeyLayout.btn123Key setTitle:@"123" forState:UIControlStateNormal];
    int i=0;
    for(UIButton *key in self.ObjKeyLayout.ArryCharKey)
    {


        [key setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];


        if(isPortrait)
        {
            [key setBackgroundImage:[UIImage imageNamed:[arrKeyImages objectAtIndex:i]] forState:UIControlStateNormal];
        }
        else
        {


            [key setBackgroundImage:[UIImage imageNamed:[keyIpad objectAtIndex:i]] forState:UIControlStateNormal];
        }
        ;

        NSString *uppercaseString = [arrAlphabet objectAtIndex:i];

        [key setTitle:uppercaseString forState:UIControlStateNormal];

        [key addTarget:self action:@selector(pressKey:) forControlEvents:UIControlEventTouchUpInside];
        i++;
       // is123selected=NO;
      }
  }

}

你取两个数组,一个用于 alphabetical,另一个用于 numerical

NSArray *NumKey;

NSArray *arrAlphabet;

现在 NumKey :

NumKey=[[NSArray alloc]initWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"0", @"-", @"/", @":", @";", @"(", @")", @"$", @"&", @"@", @"\"", @".", @",", @"?", @"!",@"'",@"^", nil];

arrAlphabet

 arrAlphabet = [[NSArray alloc]initWithObjects:@"Q", @"W", @"E", @"R", @"T", @"Y", @"U", @"I", @"O", @"P", @"A", @"S", @"D", @"F", @"G", @"H", @"J", @"K", @"L", @"Z", @"X", @"C", @"V", @"B",@"N",@"M", nil];

IBOutlet in keyBoard 123 or ABC.

如下所示以编程方式添加事件。

[self.keybard.btnNumeric addTarget:self action:@selector(action123:) forControlEvents:UIControlEventTouchUpInside];

123更改为ABC或将ABC更改为123的方法

- (IBAction)action123:(UIButton*)sender
{

    if([self.keybard.btnNumeric.titleLabel.text isEqualToString:@"123"])
    {
        [self.keybard.btnCapital setTitle:@"#+=" forState:UIControlStateNormal];

        [self.keybard.btnNumeric setTitle:@"ABC" forState:UIControlStateNormal];
        int i=0;
        for(UIButton *key in self.keybard.keyArray)
        {
            key.layer.cornerRadius = 6.0;

            [key setTitle:[NumKey objectAtIndex:i] forState:UIControlStateNormal];

            [key addTarget:self action:@selector(pressKey:) forControlEvents:UIControlEventTouchUpInside];
            i++;
        }
    }
    else
    {
        [self.keybard.btnCapital setTitle:@"⬆️" forState:UIControlStateNormal];

        [self.keybard.btnNumeric setTitle:@"123" forState:UIControlStateNormal];

        int i=0;
        for(UIButton *key in self.keybard.keyArray)
        {

            strFirstCap = @"0";

            NSString *uppercaseString = [arrAlphabet objectAtIndex:i];

            [key setTitle:[uppercaseString lowercaseString] forState:UIControlStateNormal];

            [key addTarget:self action:@selector(pressKey:) forControlEvents:UIControlEventTouchUpInside];
            i++;
        }

    }

}

输出:

我用这种方式设置了我的问题,因为我也设置了按键图像。

  @interface KeyboardViewController ()
{
     BOOL ABCselect;
BOOL select123;
BOOL keypng;
BOOL checkactiveshift;
}

  -(void)UpparCase:(UIButton*)sender
{

if (ABCselect==YES) {
    if (isPortrait) {


                     [self.ObjKeyLayout.btnCapKey setBackgroundImage:[UIImage imageNamed:@"key.png"] forState:UIControlStateNormal];
                    [self.ObjKeyLayout.btnCapKey setTitle:@"" forState:UIControlStateNormal];
                }
                else
                {
                    [self.ObjKeyLayout.btnCapKey setBackgroundImage:[UIImage imageNamed:@"special_land_key.png"] forState:UIControlStateNormal];
                    [self.ObjKeyLayout.btnCapKey setTitle:@"" forState:UIControlStateNormal];
                }
    int i=0;
    // int j=0;

    for(UIButton *key in self.ObjKeyLayout.ArryCharKey)
    {

        [key setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
        NSLog(@"\n\n123isSelected\nI-%i\tArrayCharKey.Count:-%i",i,(int)self.ObjKeyLayout.ArryCharKey.count);
        if(isPortrait)
         {
            NSLog(@"\n\n123isSelected\nI-%i\tArrayCharKey.Count:-%i\nArraySpecialImageKey.Count-%i\n",i,(int)self.ObjKeyLayout.ArryCharKey.count,(int)arrspecialImageKey.count);
            [key setBackgroundImage:[UIImage imageNamed:[arrspecialImageKey objectAtIndex:i]] forState:UIControlStateNormal];

           }
        else{

            [key setBackgroundImage:[UIImage imageNamed:[arrSpecialIpad objectAtIndex:i]] forState:UIControlStateNormal];

        }

        [key setTitle:[NumKey objectAtIndex:i] forState:UIControlStateNormal];

         [key addTarget:self action:@selector(pressKey:) forControlEvents:UIControlEventTouchUpInside];
        i++;
       }

     //select123=NO;
    keypng=YES;
    ABCselect=NO;
   }

   else if (select123==YES)
   {

     if(isPortrait)
     {

         [self.ObjKeyLayout.btnCapKey setBackgroundImage:[UIImage imageNamed:@"Caps.png"] forState:UIControlStateNormal];
        [self.ObjKeyLayout.btnCapKey setTitle:@"" forState:UIControlStateNormal];

     }
     else
     {
        [self.ObjKeyLayout.btnCapKey setBackgroundImage:[UIImage imageNamed:@"Uppar_land.png"] forState:UIControlStateNormal];
        [self.ObjKeyLayout.btnCapKey setTitle:@"" forState:UIControlStateNormal];


     }
    for(UIButton *key in self.ObjKeyLayout.ArryCharKey)
    {


        NSString *uppercaseString = [[key currentTitle] lowercaseString];

        [key setTitle:uppercaseString forState:UIControlStateNormal];

        [key addTarget:self action:@selector(pressKey:) forControlEvents:UIControlEventTouchUpInside];

    }
    select123=NO;
    keypng=NO;
    checkactiveshift=YES;

 }
 else if (keypng==YES)
 {
    [self.ObjKeyLayout.btnCapKey setTitle:@"123" forState:UIControlStateNormal];
            //[self.ObjKeyLayout.btnCapKey setTitle:@"" forState:UIControlStateNormal];
            [self.ObjKeyLayout.btnCapKey setBackgroundImage:[UIImage imageNamed:@"123.png"] forState:UIControlStateNormal];

    [self.ObjKeyLayout.btn123Key setTitle:@"ABC" forState:UIControlStateNormal];


            int i=0;
            for(UIButton *key in self.ObjKeyLayout.ArryCharKey)
            {
                [key setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];


                if(isPortrait)
                {
                    [key setBackgroundImage:[UIImage imageNamed:[specialkey2 objectAtIndex:i]] forState:UIControlStateNormal];
                }
                else
                {


                    [key setBackgroundImage:[UIImage imageNamed:[specialkey2Ipad objectAtIndex:i]] forState:UIControlStateNormal];
                }
                ;
                [key setTitle:[spec2title objectAtIndex:i] forState:UIControlStateNormal];

                [key addTarget:self action:@selector(pressKey:) forControlEvents:UIControlEventTouchUpInside];
                i++;
                // [self action123:(UIButton*)sender];
                // [self addGesturToKeyBoard];

                //isshiftselected=NO;
                keypng=NO;
                ABCselect=YES;
              //  [self action123:(UIButton*)sender];
            }
    }


    else if(!self.ObjKeyLayout.btnCapKey.isSelected){


   sender.selected=!sender.selected;
   if(sender.selected )
   {
       shiftactive=YES;

       if(isPortrait)
       {

           [self.ObjKeyLayout.btnCapKey setBackgroundImage:[UIImage imageNamed:@"upparActive.png"] forState:UIControlStateNormal];
       }
       else
       {
           [self.ObjKeyLayout.btnCapKey setBackgroundImage:[UIImage imageNamed:@"Uppar_selctd_land.png"] forState:UIControlStateNormal];

       }

       for(UIButton *key in self.ObjKeyLayout.ArryCharKey)
       {


           NSString *uppercaseString = [[key currentTitle] uppercaseString];

           [key setTitle:uppercaseString forState:UIControlStateNormal];

           [key addTarget:self action:@selector(pressKey:) forControlEvents:UIControlEventTouchUpInside];


       }
            //   [sender1 setSelected: NO];
          // shift=YES;
       [sender setSelected: YES];
    }

      else
       {

       if(isPortrait)
        {

           [self.ObjKeyLayout.btnCapKey setBackgroundImage:[UIImage imageNamed:@"Caps.png"] forState:UIControlStateNormal];
       }
       else
       {
           [self.ObjKeyLayout.btnCapKey setBackgroundImage:[UIImage imageNamed:@"Uppar_land.png"] forState:UIControlStateNormal];

       }
       for(UIButton *key in self.ObjKeyLayout.ArryCharKey)
       {


           NSString *uppercaseString = [[key currentTitle] lowercaseString];

           [key setTitle:uppercaseString forState:UIControlStateNormal];

           [key addTarget:self action:@selector(pressKey:) forControlEvents:UIControlEventTouchUpInside];

        }
             //  [sender1 setSelected: YES];

     }

     }
     else if (self.ObjKeyLayout.btnCapKey.isSelected)
    {
    if(isPortrait)
    {

       [self.ObjKeyLayout.btnCapKey setBackgroundImage:[UIImage imageNamed:@"Caps.png"] forState:UIControlStateNormal];
    }
    else
    {
       [self.ObjKeyLayout.btnCapKey setBackgroundImage:[UIImage imageNamed:@"Uppar_land.png"] forState:UIControlStateNormal];

    }
    for(UIButton *key in self.ObjKeyLayout.ArryCharKey)
    {


       NSString *uppercaseString = [[key currentTitle] lowercaseString];

       [key setTitle:uppercaseString forState:UIControlStateNormal];

       [key addTarget:self action:@selector(pressKey:) forControlEvents:UIControlEventTouchUpInside];

      }
       //  [sender1 setSelected: YES];
      self.ObjKeyLayout.btnCapKey.selected = !self.ObjKeyLayout.btnCapKey.isSelected;


       }
     }

   - (IBAction)action123:(UIButton*)sender
    {


  // dummy=YES;
    sender.selected=!sender.selected;
     if(sender.selected)
    {
   // is123selected=YES;
    //[self dummy];

    [self.ObjKeyLayout.btn123Key setTitle:@"ABC" forState:UIControlStateNormal];
     //[self.ObjKeyLayout.btnCapKey setBackgroundImage:[UIImage imageNamed:@"key.png"] forState:UIControlStateNormal];
    ABCselect=YES;
    [self UpparCase:(UIButton*)sender];

    int i=0;
   // int j=0;

    for(UIButton *key in self.ObjKeyLayout.ArryCharKey)
    {

        [key setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
        NSLog(@"\n\n123isSelected\nI-%i\tArrayCharKey.Count:-%i",i,(int)self.ObjKeyLayout.ArryCharKey.count);
        if(isPortrait)
        {
             NSLog(@"\n\n123isSelected\nI-%i\tArrayCharKey.Count:-%i\nArraySpecialImageKey.Count-%i\n",i,(int)self.ObjKeyLayout.ArryCharKey.count,(int)arrspecialImageKey.count);
            [key setBackgroundImage:[UIImage imageNamed:[arrspecialImageKey objectAtIndex:i]] forState:UIControlStateNormal];




        }
        else{

            [key setBackgroundImage:[UIImage imageNamed:[arrSpecialIpad objectAtIndex:i]] forState:UIControlStateNormal];

        }

        [key setTitle:[NumKey objectAtIndex:i] forState:UIControlStateNormal];

        [key addTarget:self action:@selector(pressKey:) forControlEvents:UIControlEventTouchUpInside];
        i++;
    }
    makeshift=NO;
}
else
{ABCselect=NO;
    [self.ObjKeyLayout.btn123Key setTitle:@"123" forState:UIControlStateNormal];
             select123=YES;
    [self UpparCase:(UIButton*)sender];



    int i=0;
    for(UIButton *key in self.ObjKeyLayout.ArryCharKey)
    {


        [key setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];


        if(isPortrait)
        {
            [key setBackgroundImage:[UIImage imageNamed:[arrKeyImages objectAtIndex:i]] forState:UIControlStateNormal];
        }
        else
        {


            [key setBackgroundImage:[UIImage imageNamed:[keyIpad objectAtIndex:i]] forState:UIControlStateNormal];
        }
        ;

        NSString *uppercaseString = [arrAlphabet objectAtIndex:i];

        [key setTitle:uppercaseString forState:UIControlStateNormal];

        [key addTarget:self action:@selector(pressKey:) forControlEvents:UIControlEventTouchUpInside];
        i++;
        //is123selected=NO;
       }
    }
     [self.ObjKeyLayout.btnCapKey setSelected: NO];

    }