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 8663711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:03:14+00:00 2026-06-12T17:03:14+00:00

I have a UITextView which I am using to display text. Along the side

  • 0

I have a UITextView which I am using to display text. Along the side I have a UITableView that I am using to display the line Numbers. The problem occurs when when trying to align the line numbers with the lines of text. Here is a screenshot of the problem: https://www.dropbox.com/s/7sjqm4nrsqm6srh/Screen%20Shot%202012-10-11%20at%209.16.58%20PM.png
For example, line 7 should be moved down since it is not at the start of a new line. I think the problem has to deal with the spacing when the lines of text wrap, but I am not sure.

So for the UITableView, I am creating the same amount of cells as there are lines of text. Then the cells are stretched so their height is the same as each line of text by using the method:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
int i = indexPath.row;
if (i == 0) {
    return 10;
}

UIFont *font = _codeView.font;

NSArray *strings = [_codeView.text componentsSeparatedByString:@"\n"];
NSString *tempString = [strings objectAtIndex:i-1];

if (([strings objectAtIndex:i-1] == @"\n") | ([[strings objectAtIndex:i-1] length] == 0) ) {
    tempString = @"hi";
}

CGSize stringSize = [tempString sizeWithFont:font constrainedToSize:CGSizeMake(_codeView.bounds.size.width, 9999) lineBreakMode:NSLineBreakByWordWrapping];
CGFloat height = stringSize.height;

return height;
}

The _codeView is the UITextView that I am using to display the code. The UITableView that displays the line numbers also has one blank cell at the top so I can align first cell with a line number with the first line of text.

I have tried multiple ways of playing with the spacing, but none of them seem to fix the problem. Any help would be greatly appreciated.

  • 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-12T17:03:15+00:00Added an answer on June 12, 2026 at 5:03 pm

    I believe the problem is with the size you pass to sizeWithFont. You are setting the width to the textView’s width. This will actually be a little too wide. The text view actually has a little bit of a margin. Try subtracting a few points from the width. Also, you are dealing with attributed text, not plain strings. You should look at the methods for calculating the height of an NSAttributedString.

    One last thing in the code you posted. Your are splitting the _codeView.text on every call to heightForRowAtIndexPath:. That’s bad. You should do this once and save off the strings. This method is called a lot. That is a lot of redundant string splits.

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

Sidebar

Related Questions

I have a UITextView which is connected to UITableView for Autocompletion. The problem is
I have a UITextView in which i have 5 hyperlinks and 5 phone numbers.How
I have a UITextView in a UITableViewCell which grows together with it. Using [myTableView
I have an UITextView which is for instance 380 characters in length: NSLog(@aTextView.text lenght
I'm writing iPhone/iPad application. I have UITextView in which I automatically appending text. How
I have a simple UITextView in which I display some links. The UITextView automatically
I have a UITextView which I'm assigning a large amount of text to (~25
I'm trying to set up a CTFrame that exactly matches my UITextView 's text
I have an UITextView which takes data from XML. Now that XML node is
In my app i am using UITextView to display three lines. In that i

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.