Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8612339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:36:07+00:00 2026-06-12T04:36:07+00:00

I am trying to implement my own keyboard containing emojis. For this purpose I

  • 0

I am trying to implement my own keyboard containing emojis.
For this purpose I am inserting the emoji in the cursor position.

This works fine if no 4-bytes emoji characters exist in UITextField. Otherwise the app gets crashed.

I am posting the insertion code here. Can someone point out how to solve the issue?

UITextField *field = self.textField;
UITextRange *range = field.selectedTextRange;
int pos = [field offsetFromPosition:field.beginningOfDocument toPosition:range.end];
NSString * firstHalfString = [field.text substringToIndex:pos];  
NSString * secondHalfString = [field.text substringFromIndex:pos];  
field.text = [NSString stringWithFormat: @"%@%@%@", firstHalfString, emoticon, secondHalfString];
UITextPosition *newPos = [field positionFromPosition:field.beginningOfDocument offset:pos + 1];
field.selectedTextRange = [field textRangeFromPosition:newPos toPosition:newPos];

this line returns nil if there are emojis in the text:

UITextPosition *newPos = [field positionFromPosition:field.beginningOfDocument offset:pos + 1];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:36:08+00:00Added an answer on June 12, 2026 at 4:36 am

    At the end I solved this by writing my own length and offset calculation methods which count 4-byte characters as 1 character, not two.

    @implementation NSString (UnicodeAdditions)
    -(NSInteger)utf32length {
        const char* bytes = [self UTF8String];
        int length = [self lengthOfBytesUsingEncoding:NSUTF16StringEncoding];
        int newLength = 0;
        for (int i=0; i<length; i++) {
            if (((unsigned char)bytes[i] >> 7) == 0b00000000)  {
              newLength++;
            }
            else if (((unsigned char)bytes[i] >> 5) == 0b00000110) {
                newLength++;
                i+=1;
            }
            else if (((unsigned char)bytes[i] >> 4) == 0b00001110) {
                newLength++;
                i+=2;
            }
            else if (((unsigned char)bytes[i] >> 3) == 0b00011110) {
                newLength++;
                i+=3;
            }
        }
        return newLength;
    }
    
    
    -(NSInteger)utf32offsetWithOffset:(NSInteger)offset {
        const char* bytes = [self UTF8String];
        int length = [self lengthOfBytesUsingEncoding:NSUTF16StringEncoding];
        int newLength = 0;
        for (int i=0; i<length && offset!=0; i++) {
            if (((unsigned char)bytes[i] >> 7) == 0b00000000)  {
                offset--;
                newLength++;
            }
            else if (((unsigned char)bytes[i] >> 5) == 0b00000110) {
                offset--;
                newLength++;
                i+=1;
            }
            else if (((unsigned char)bytes[i] >> 4) == 0b00001110) {
                offset--;
                newLength++;
                i+=2;
            }
            else if (((unsigned char)bytes[i] >> 3) == 0b00011110) {
                offset-=2;
                newLength++;
                i+=3;
            }
        }
        return newLength;
    }
    
    @end
    

    see the full blog post http://bit.ly/PT9VSz

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application where I'm trying to implement my own custom keyboard. This
Problem statement I'm trying to implement my own virtual keyboard following example softkeyboard, found
I've been going around this question Here And trying to implement my own solution.
I'm trying to implement my own qDebug() style debug-output stream, this is basically what
I'm trying to implement my own Hibernate NamingStrategy based on the ImprovedNamingStrategy. This is
I'm trying to implement my own List system in Java. the List class file
I'm trying to implement my own GenericIdentity implementation but keep receiving the following error
I am trying to implement my own authentication method for AuthKit and am trying
I'm trying to implement my own version of the 'cd' command that presents the
For all those ligthning fast shop users. I'm trying to implement my own first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.