[futurebasic] RE: [FB] adding attributes to a CFMutableAttributedStringRef [RESOLVED]

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : October 2009 : Group Archive : Group : All Groups

From: "Edwards, Waverly" <Waverly.Edwards@...>
Date: Tue, 13 Oct 2009 00:27:50 -0500
Yes, it can be done.  Thanks for pointing out my errors.  I'm sure I would NOT have figured the problem out without your help.
If anyone is interested, here is a working model.  The function is designed to add an attributed to an existing string with the
type CFMutableAttributedStringRef.  Some manipulation for casting string types were necessary.

Thanks RP,


W.



include "Tlbx CFAttributedString.incl"
include "Tlbx CoreText.incl"


local fn AddAttributeTest( ctx as CGContextRef )
'~'1
dim as str255       testStr, wholeStr
dim as CFIndex      strLen, where, foundLen
dim as CTFontRef    font
dim as CFStringRef  string
dim as CTLineRef    lineRef
dim as CGColorRef   redColor
dim as CFMutableAttributedStringRef  attrString


// Initialize an attributed string.
wholeStr     = "This is a test of the emergency broadcast sytem.  This is only a test"
testStr      = "emergency"
where        = instr(0, wholeStr, testStr ) - 1
foundLen     = len( testStr)
string       = fn CFSTR( wholeStr )
attrString   = fn CFAttributedStringCreateMutable( _kCFAllocatorDefault, 0 )
font         = fn CTFontCreateWithName( fn CFSTR( "Courier"), 12.0, 0 )
redColor     = fn CGColorCreateGenericRGB( 1.0, 0.0, 0.0, 1.0 )
CFAttributedStringReplaceString( attrString, fn CFRangeMake( 0, 0 ), string )

strLen   = fn CFAttributedStringGetLength( attrString ) 
CFAttributedStringSetAttribute( attrString, fn CFRangeMake( 0, strLen) , kCTFontAttributeName, font )


#if def _PASSTHROUGH // ObjC

/*
     Add the attributes. This section adds attributes to the selected range.
     It is unlike the "setAttribute" method, which would destroy unrelated attributes in the define range.
     Unfortunately, the "addAttribute" method is not available in carbon but is available to Obj-C

     The "setAttribute" method is the equivalent of fn CFAttributedStringSetAttribute(...)
*/

NSMutableAttributedString *str = (NSMutableAttributedString*) attrString;
NSString  *colorKey, *ulineKey;// testing
CFNumberRef underline;
SInt32 value;

colorKey  = (NSString*) kCTForegroundColorAttributeName;
ulineKey  = (NSString*) kCTUnderlineStyleAttributeName;
value     = 1; 
underline = CFNumberCreate( kCFAllocatorDefault, kCFNumberSInt32Type, &value );

NSRange testRange = NSMakeRange( where, foundLen );// no error checking for test

// cast redColor and underline as (id) so GCC will stop complaining about incompatible types
[ str beginEditing ];
[ str addAttribute: colorKey value:(id)redColor  range:testRange ];// add
[ str addAttribute: ulineKey value:(id)underline range:testRange ];// add another
//[ str addAttribute: testKey value:CGColorGetConstantColor(kCGColorWhite) range:testRange ];
//[ str addAttribute: testKey value:[NSColor blueColor] range:testRange ];
[ str endEditing ];
 
CFRelease( underline );
#endif


lineRef = fn CTLineCreateWithAttributedString( attrString )

CGContextSetTextPosition( ctx, 10.0, 10.0 )
CTLineDraw( lineRef, ctx )

CFRelease( lineRef )
CFRelease( attrString )
CFRelease( redColor )
end fn


dim as CGContextRef ctx
if ( system( _sysVers ) < 1050 ) then shutdown "Requires 10.5 or higher"

window 1
fn QDBeginCGContext( window( _wndPort ), @ctx )
fn AddAttributeTest( ctx )
fn QDEndCGContext(  window( _wndPort ), @ctx )

do
HandleEvents
until gFBQuit