TextView and bold text

Hello…

Got a TextView and alot of data which i need to “seperate” by using bold headlines, but here is the problem :smiley:

How do i change the default font to Bold on a single word in a TextView…

Thanks…

A possible example:

set exampleText to "Lipsum\n\nLorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras aliquam odio eget felis. Mauris aliquam urna eu enim. Suspendisse non tortor. Praesent volutpat mi vestibulum libero. Phasellus quis neque. Maecenas neque erat, tincidunt condimentum, ullamcorper eget, laoreet sed, risus.\n\n Sed magna\n\n Sed egestas, lorem ut convallis viverra, mauris pede porta tortor, at consectetuer elit risus sed diam. Phasellus ac pede ut lorem accumsan rutrum. Duis dui purus, imperdiet ac, luctus id, rhoncus et, metus. Phasellus consequat. In metus. Duis consectetuer iaculis eros. Praesent pellentesque felis non orci. Sed a lorem et libero consectetuer fringilla."

tell application "TextEdit"
	make new document with properties {text:exampleText}
	
	tell first document
		set font of paragraph 1 to "Helvetica Bold"
		set font of paragraph 5 to "Helvetica Bold"
	end tell
end tell

Hello Bruce,

Yes this helps a bit, but im still a bit confused on how to make this work…


set contents of text view 1 of scroll view "imdbPlot" of window 1 to "Plot" & return & plot_outline & return & return & the_tagline

and here i want to make “Plot” bold, there will be added alot more variables and “info” fields which aloso needs to be bold, the point of
all this is i am creating an imdb search application, and want to be able to dynamic add new data, thats why i am using the TextView,
and then listing all the data…

/mkh

im not sure if this will work but if you look in a rtf file it uses "\f0\b\fs24 \cf0 " to make something bold.
try ticking th “Rich Text” box for the text box and then

set contents of text field "txt" of window "main" to "\f0\b\fs24 \cf0 " & myBoldTxt

could work :wink:

Sorry, I read that as TextEdit earlier. :rolleyes:

Captainhams thanks, but it is a TextView and the “Rich Text” is checked
but this only prints \f0\b\fs24 \cf0 into my view


set contents of text view 1 of scroll view "pilotview" of window "IMDB Search" to "\\f0\\b\\fs24 \\cf0 " & "Title:" & return & movie_title & return & return & "Release Date:" & return & release_date & return & return & "Genre:" & return & the_genre & return & return & "Tagline:" & return & the_tagline & return & return & "User Rating:" & return & user_rating & return & return & "MPAA Rating:" & return & mpaa_rating & return & return & "Plot Outline:" & return & plot_outline & return & return & "The Cast:" & return & the_cast & return

sorry, i have no idea.
Maybe just make two text views, one bold one not.
or maybe see if you can set the contents of a webview then just use “bold text not bold”

Yes, i was thinking of writing all data to a text file. but i got some other problems with all this pt…

there must be an easy way - I will have a better look

would be much appreciated if there is a solution for this.
I’ve added a link to screenshot of app, that might give you an idea of why i what this to work :smiley:

http://uniqz.dk/imdb.png

Text views are very complicated objects. There are many sub-objects at work in a text view, and there’s no good way to do what you want to do in applescript. While the applescriptkit does have some basic text scriptability, support for setting fonts and font attributes is limited. To avoid the complexity of the obj-c method I describe below, perhaps you could consider changing the color instead of the weight of the font. Applescript does have commands that can manipulate the color of text on a per-word/-character basis. See the text view documentation for some examples. Another alternative, might be to change the design of your interface to have fixed text fields for each line item instead of a large text view for all info.

Below is the solution I came up with. You create a temporary custom object that holds a mutable string as a variable. You create the string you’ll use as your content for the text view manually by adding lines one at a time, and then display the resulting string in your text view.

–EDIT–
Updated this script with your variable names hard-coded. Also added a small handler to make adding returns to each line easier.
–EDIT–

property TextView : null

on awake from nib theObject
	if name of theObject is "TextView" then
		set TextView to theObject
	end if
end awake from nib

on clicked theObject
	if name of theObject is "setContent" then
		setContent()
	end if
end clicked

to setContent()
	(* Create a new custom text object *)
	set newTextObject to (call method "init" of (call method "alloc" of class "TVBTextObject"))
	
	(* Append each paragraph with desired weight and number of trailing returns *)
	call method "appendString:isBold:" of newTextObject with parameters {prepareStringForAppend("Title:", 1), yes}
	call method "appendString:isBold:" of newTextObject with parameters {prepareStringForAppend(movie_title, 2), no}
	call method "appendString:isBold:" of newTextObject with parameters {prepareStringForAppend("Release Date:", 1), yes}
	call method "appendString:isBold:" of newTextObject with parameters {prepareStringForAppend(release_date, 2), no}
	call method "appendString:isBold:" of newTextObject with parameters {prepareStringForAppend("Genre:", 1), yes}
	call method "appendString:isBold:" of newTextObject with parameters {prepareStringForAppend(the_genre, 2), no}
	call method "appendString:isBold:" of newTextObject with parameters {prepareStringForAppend("Tagline:", 1), yes}
	call method "appendString:isBold:" of newTextObject with parameters {prepareStringForAppend(the_tagline, 2), no}
	call method "appendString:isBold:" of newTextObject with parameters {prepareStringForAppend("User Rating:", 1), yes}
	call method "appendString:isBold:" of newTextObject with parameters {prepareStringForAppend(user_rating, 2), no}
	call method "appendString:isBold:" of newTextObject with parameters {prepareStringForAppend("MPAA Rating:", 1), yes}
	call method "appendString:isBold:" of newTextObject with parameters {prepareStringForAppend(mpaa_rating, 2), no}
	call method "appendString:isBold:" of newTextObject with parameters {prepareStringForAppend("Plot Outline:", 1), yes}
	call method "appendString:isBold:" of newTextObject with parameters {prepareStringForAppend(plot_outline, 2), no}
	call method "appendString:isBold:" of newTextObject with parameters {prepareStringForAppend("The Cast:", 1), yes}
	call method "appendString:isBold:" of newTextObject with parameters {prepareStringForAppend(the_cast, 0), no}
	
	(* Update the text view *)
	call method "setTextContentForTextView:" of newTextObject with parameter TextView
	
	(* Dispose of our custom text object *)
	call method "release" of newTextObject
end setContent

to prepareStringForAppend(aString, numReturns)
	repeat numReturns times
		set aString to (aString & return)
	end repeat
	return aString
end prepareStringForAppend

Next, create a NSObject subclass in your project (mine was named “TVBTextObject”) by adding a new obj-c file pair (.h/.m) to your project. In each file place the following code…

I left in my log (NSLog…) statements so you can use them for troubleshooting purposes. You’ll want to comment them out or delete them once you’re confident that the code is sound.

jobu, awsome work, i’ll have a look on this, and yes, i was using “fixed text fields” but that is no good, i want to be able to add more data to my textView, so fixed fields is not the best way to do this…

I’m not very good at Object-C but this looks great, i’ll give some feedback when ready, thanks alot for the effort