Please consider a donation to the Higher Intellect project. See https://preterhuman.net/donate.php or the Donate to Higher Intellect page for more info.

NeXTSTEP AppKit Appending Text

From Higher Intellect Vintage Wiki
Revision as of 01:13, 29 September 2018 by Netfreak (talk | contribs)

Appending Text

Q:  How can you add text to the end of a text object?
	
A:  The trick is to make a zero length selection at the end of the text, and add the new text there.  The following code snippet illustrates the necessary steps:

/* This routine assumes that you have defined currentDocWindow somewhere else */ 

- addText:(const char *)newString
{
	int length;
	id  currentDocText;
	
	/* Get the current text document */
	currentDocText = [[currentDocWindow contentView]docView];
	length = [currentDocText textLength];
	[currentDocText setSel:length :length];
	[currentDocText replaceSel:newString];
	
	return self;
}

QA396

Valid for 1.0, 2.0, 3.0, 3.1



Q:  How do I append text to a text object, such that the new text makes use of a different font?

A:  First of all, make sure that the text object can contain multiple fonts. Send the following message to your text object:

	id myText;
	...
	[myText setMonoFont:NO];
	...

Then, use the following code snippet to append the text, select the text, and change its font:

	id myText;
	int length;
	
	...
	length = [myText textLength];
	[myText setSel:length:length];	// put an empty selection at the end of the text
	[myText replaceSel:"some new text"];	// add some text
	[myText setSel:length :[myText textLength]];	// select the newly added text
	[myText setSelFont:[Font newFont:"Symbol" size:24.0]];	// change its font
	...
The above code works in all releases and leaves the text selected.

The following code snippet will not work (in Release 1.0 or 2.0) due to the way in which setSelFont:, and replaceSel: interact.  (For 3.0 this code snippet works similar to the code above except that the text is not highlighted when completed.)

	id myText;
	int length;
	
	...
	length = [myText textLength];
	[myText setSel:length:length];	// put an empty selection at the end of the text
	[myText setSelFont:[Font newFont:"Symbol" size:24.0]];	// change the font
	[myText replaceSel:"some new text"];	// add some text
	...

For 2.0:			
To avoid some of the flashing that may occur to the text object while selecting and modifying the font programmatically, you should perform a disableDisplay on the window containing the text object before selecting and modifying the text. After the modifications you should then reenableDisplay on the window and display the text object.

For more information about appending text to a text object see ../Appkit/appending_text.rtf.

QA747

Valid for 1.0, 2.0, 3.0, 3.1