Re: How can I get attributed string info in a form I can save?
Re: How can I get attributed string info in a form I can save?
- Subject: Re: How can I get attributed string info in a form I can save?
- From: Dave Camp <email@hidden>
- Date: Tue, 8 Mar 2005 13:08:53 -0800
On Mar 8, 2005, at 12:42 PM, Douglas Davidson wrote:
On 2005-03-08 11:59:32 -0800 Dave Camp <email@hidden> wrote:
I need to be able to encode the style information into some custom
XML (very much like converting the text into HTML). I was hoping
Cocoa would give me something broken down into more detail like:
font = Helvetica
bold = yes
size = 12.00 pt
etc...
How can I get more useful style information about the text? Am I
really expected to be able to parse strings like those shown above?
There must be another way of doing this.
NSFontManager will tell you whether a particular NSFont is bold,
italic, etc. However, not all fonts have all variants of
bold/non-bold, italic/non-italic. If you have (for example) a font
with no non-bold variant, NSFontManager will correctly tell you that
it is bold, but you should be careful when writing this information
out in external formats--if you specify that font and request that it
be bolded, there are some applications out there that will take that
information and then try to synthesize an extra-bolded variant of the
already bold font.
There really isn't a standard font naming convention. The most
reliable name is the Postscript name, but the name you are looking for
may be closer to the family name.
I've obviously been hitting the cold medication too hard today... upon
further inspection of the docs and my log output I realized that what I
was dumping were NSFont objects, not strings. Adding the following code
yields information closer to what I was looking for but still not
correct:
NSDictionary *dict = [string attributesAtIndex:index
effectiveRange:&effectiveRange];
NSFont *font = [dict objectForKey:@"NSFont"];
NSFontDescriptor *desc = [font fontDescriptor];
CFShow([desc fontAttributes]);
<CFDictionary 0x3861e0 [0xa01900e0]>{type = mutable, count = 3,
capacity = 4, pairs = (
0 : <CFString 0xa2dc4bb0 [0xa01900e0]>{contents =
"NSFontNameAttribute"} = <CFString 0x33be00 [0xa01900e0]>{contents =
"Helvetica-Oblique"}
3 : <CFString 0xa2dc4bd0 [0xa01900e0]>{contents =
"NSFontSizeAttribute"} = <CFNumber 0x3860c0 [0xa01900e0]>{value =
+12.0000000000, type = kCFNumberFloat32Type}
4 : <CFString 0xa2dc4ba0 [0xa01900e0]>{contents =
"NSFontFamilyAttribute"} = <CFString 0x33c2f0 [0xa01900e0]>{contents =
"Helvetica"}
So, from this I can tell that the font is Helvetica and I can get the
font size. This is a good start.
However, the "bold" and "italics" seem to wind up as a font variant
(e.g. Helvetica-Oblique for runs of text that are in italics).
So, to restate my problem (I don't think I was clear before)... If the
user typed "this is a sample" and set "sample" to be in italics, I want
to write some code to convert this into "this is a <i>sample<\i>" for
transmission over the network.
Looking at NSFont, I don't see any methods that will tell me if the
font is italic or bold or whatnot, nor do I see anything in
NSFontDescriptor. It appears I can pass that NSFont to [NSFontManager
traitsOfFont:] and get back a traits mask which might be what I want.
However, that seems a roundabout way of doing it.
Is that the expected way of performing an operation like this?
Thanks,
Dave
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden