Apply a Font and Font Size to a string

asobjc
foundation

(Andreas Kiel) #1

Sorry for all my beginner’s questions here.

Thanks to Shane I got my conversion of subtitles up and running - half way :wink:

I got the ‘semi-html’ converted to RTF or attributedString. The thing which is missing is font & font size. Latter can be set in HTML.
But I haven’t font a way to set the font of and attributedString.

Thanks in advance.


(Mark Alldritt) #2

Here is a how-to post on doing this:


(Andreas Kiel) #3

Thanks Mark.

The code snippet is fine for another problem I will face in the future.

Right now I need to apply/change the font of an attributed string.
So my question in the topic was wrong.

I used Shane’s code to convert my html-like string to an attributedString.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use framework "Foundation"
           
set theText to "<font color=\"#FFF800\">We'll kick their ass.</font>
<font color=\"#00F400\"><i>You see?</i></font>"
set theText to NSString's stringWithString:theText
set theData to theText's dataUsingEncoding:NSUTF8StringEncoding
if theData = missing value then error (theError's localizedDescription() as text)
set theATS to NSAttributedString's alloc()'s initWithHTML:theData documentAttributes:(missing value)

This results in an attributed string with Times 12.0px.
While in theory and my case/usage font and font size don’t matter they are needed for the UI.
Each of the 1 - 1000 or 2000 or so html snippets will be converted the above way and stored in a text cell in a table view. For readability it must be a sans-serif font; the size must be something 12-14 to match the table’s interface.
Each of these cells must be editable and displayed in a text view in a (user defined) bigger font size or even font.
I could carry 2 versions of the text in the data source: one with the user’s font selection, another with a table representation - only latter one should be visible in the table. But both need to synched.

I hope it make things a bit clearer.
Thanks.


(Mark Alldritt) #4

First, you need to convert your NSAttributedString into a NSMutableAttributedString before you can make changes (mutate):

set theMATS to theATS's mutableCopy()

Then, you need a font object to apply:

set theFont to NSFont's fontWithName:"Helvetica" |size|:30

And finally, you need to apply a style run using the font:

set theRange to {location:3, |length|: 10} -- or whatever...
(theMATS's addAttribute:NSFontAttributeName value:theFont range:theRange)

You can see this being done against words within a larger block of text in the Add Bolding section of the example I provide in the cited post.

Alternatively, you can define the font size in the HTML you are importing:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use framework "Foundation"

set theText to "<font color=\"#FFF800\" size=\"6\">We'll kick their ass.</font>
<font color=\"#00F400\" size=\"7\"><i>You see?</i></font>"
set theText to my (NSString's stringWithString:theText)
set theData to theText's dataUsingEncoding:(my NSUTF8StringEncoding)
if theData = missing value then error (theError's localizedDescription() as text)
set theATS to my (NSAttributedString's alloc()'s initWithHTML:theData documentAttributes:(missing value))

which results in:

18%20AM


(Andreas Kiel) #5

Many thanks Mark!!!

I failed with the first line - making a copy

BTW:

Shane’s code will fail if you have other than Roman languages.

set theData to theText’s dataUsingEncoding:(my NSUTF8StringEncoding)

You need to use a UTF16 string.
Maybe it helps others.


(Andreas Kiel) #6

Hmm,
I thought my problem was solved, but this will overwrite styles like bold or italic.


(Mark Alldritt) #7

Yes, it will. If you want to preserve those, you’ll need to iterate over the style runs and update the font in each one. This involves using the Font Manager to convert your large font to have the traits of the font in each style run.


(Shane Stanley) #8

Only if the HTML is encoded in other than UTF-8. If the HTML is encoded in UTF-8, the language is irrelevant.


(Andreas Kiel) #9

Will be true for a real html file.
But with the given portion of an html it is not.
Replace the English text in the html portion with some Bulgarian, Hebrew, Chinese or whatever non roman and the code will fail.


(Andreas Kiel) #10

I finally put all text formatting into the html using a “body” section with style.
"<body style=\"margin: 0.0px 0.0px 0.0px 0.0px; text-align: center; line-height: 14.0px; font: 14.0px Menlo; color: #ffffff\"><font color=\"#FFFFFF\">อรุณสวัสดิ์ คุณภรรยา</font></body>"


(Shane Stanley) #11

That’s because you’re not defining an encoding in your html, so it’s dropping back to a default. Try it with this:

set theText to "<meta charset=\"utf-8\"><body style=\"margin: 0.0px 0.0px 0.0px 0.0px; text-align: center; line-height: 14.0px; font: 14.0px Menlo; color: #ffffff\"><font color=\"#FFFFFF\">อรุณสวัสดิ์ คุณภรรยา</font></body>"

Using NSUTF16StringEncoding is probably helping the parser guess, but not specifying the encoding is the sort of thing likely to bite you at some stage.


(Andreas Kiel) #12

That’s a good idea!!!
Thanks.