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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:43:58+00:00 2026-05-17T00:43:58+00:00

I’m trying to format some text in NSTextView to be placed in a single

  • 0

I’m trying to format some text in NSTextView to be placed in a single line with black background. One piece of text needs to be left aligned, the other piece (but still in the same line) needs to be centered.

NSString *header = [NSString stringWithFormat:
        @""
            "<table style=\"width: 100%; background: black; "
            "color: white; font-family: Arial; "
            "font-size: 16px;\"><tr><td>"
                "1. zadatak"
            "</td><td align=\"center\" width=\"100%\">"
            ""
                "%@"
            ""
            "</td></tr></table>"
        "", [self.problemname.stringValue uppercaseString]];

Unfortunately, no matter what width I specify, NSTextView appears to ignore the table width.

Any workarounds, different approaches, or other suggestions?


More info on the background of the problem.

I’ve written an app for writing tasks for programming contests. Each task author submits content in different format, so getting them to standardize it in a simple bundle would be of great benefit.

I need this for a printing module. I’m taking several NSTextViews and stitching them together.

These contests have the following document formatting which we’re supposed to follow – Example PDF (GoogleDocs)
:

Contest header
+------------------------------------------------+
| 1st Task           TITLE             20 points |
+------------------------------------------------+

Introductory text here.


                    Input

Explanation of input data


                   Output

Explanation of output data


                   Sample Data

Input:       | Input:
abcd         | bcde
             |
Output:      | Output:
efgh         | fghi
             |
More info:   |
info text    |
  • 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-05-17T00:43:59+00:00Added an answer on May 17, 2026 at 12:43 am

    Modification of Apple’s sample code for programmatically adding tables to NSAttributedString:

    - (NSMutableAttributedString *) printTitleTableAttributedString
    {
        NSMutableAttributedString *tableString = [[NSMutableAttributedString alloc] initWithString:@"\n\n"];
        NSTextTable *table = [[NSTextTable alloc] init];
        [table setNumberOfColumns:3];
    
        [tableString appendAttributedString:[self printTitleTableCellAttributedStringWithString:@"1. zadatak\n"
                                                                                          table:table
                                                                                  textAlignment:NSLeftTextAlignment
                                                                                            row:0
                                                                                         column:0]];
    
        [tableString appendAttributedString:[self printTitleTableCellAttributedStringWithString:[self.problemname.stringValue stringByAppendingString:@"\n"]
                                                                                          table:table
                                                                                  textAlignment:NSCenterTextAlignment
                                                                                            row:0
                                                                                         column:1]];
    
        [tableString appendAttributedString:[self printTitleTableCellAttributedStringWithString:@"20 bodova\n"
                                                                                          table:table
                                                                                  textAlignment:NSRightTextAlignment
                                                                                            row:0
                                                                                         column:2]];
        [table release];
        return [tableString autorelease];
    }
    
    
    
    - (NSMutableAttributedString *) printTitleTableCellAttributedStringWithString:(NSString *)string
                                                                            table:(NSTextTable *)table
                                                                    textAlignment:(NSTextAlignment)textAlignment
                                                                              row:(int)row
                                                                           column:(int)column
    {
        NSColor *backgroundColor = [NSColor colorWithCalibratedRed:0 
                                                             green:0 
                                                              blue:0x80/255. 
                                                             alpha:0xFF];;
        NSColor *borderColor = [NSColor whiteColor];
    
    
        NSTextTableBlock *block = [[NSTextTableBlock alloc]
                                   initWithTable:table
                                   startingRow:row
                                   rowSpan:1
                                   startingColumn:column
                                   columnSpan:1];
        [block setBackgroundColor:backgroundColor];
        [block setBorderColor:borderColor];
        [block setWidth:0.0 type:NSTextBlockAbsoluteValueType forLayer:NSTextBlockBorder];
        [block setWidth:2.0 type:NSTextBlockAbsoluteValueType forLayer:NSTextBlockPadding];
    
        NSMutableParagraphStyle *paragraphStyle =
        [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
        [paragraphStyle setTextBlocks:[NSArray arrayWithObjects:block, nil]];
        [paragraphStyle setAlignment:textAlignment];
        [block release];
    
        NSMutableAttributedString *cellString =
        [[NSMutableAttributedString alloc] initWithString:string];
        [cellString addAttribute:NSParagraphStyleAttributeName
                           value:paragraphStyle
                           range:NSMakeRange(0, [cellString length])];
        [cellString addAttribute:NSForegroundColorAttributeName
                           value:[NSColor whiteColor]
                           range:NSMakeRange(0, [cellString length])];
        [paragraphStyle release];
    
        return [cellString autorelease];
    }
    

    Then I just append it into the textview:

    [printText.textStorage setAttributedString:[self printTitleTableAttributedString]];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to create an if statement in PHP that prevents a single post
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:

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.