Why am I unable to save my UIFontDescriptor into a variable? - objective-c

I have the following code:
UIFontDescriptor *fd = [UIFont fontWithDescriptor:[[UIFont systemFontOfSize:[UIFont systemFontSize]].fontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold|UIFontDescriptorTraitItalic];
But it gives me the error "Expected expression". What does that mean?

Initially, I counted 4 open brackets and 3 close brackets, which will cause an error. Additionally, fontWithDescriptor: should be fontWithDescriptor:size:, which is probably the expected expression.
Finally, using fontWithDescriptor:size: to define a font, then asking for its descriptor is redundant, so a cleaner version of the command is this:
UIFontDescriptor *fd = [[[UIFont systemFontOfSize:[UIFont systemFontSize]] fontDescriptor] fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold|UIFontDescriptorTraitItalic];

Related

warning use of undefined constant.. this will throw an error in future version of php

This should be enough for someone to correct my issue - I'm very much a newbie at this.
It's a short bit of code to strip spaces from the ends of strings submitted in forms.
The warning message is saying "Use of undefined constant mystriptag - assumed 'mystriptag' (this will throw an error..."
How should I change this?
function mystriptag($item)
{
$item = strip_tags($item);
}
array_walk($_POST, mystriptag);
function t_area($str){
$order = array("\r\n", "\n", "\r");
$replace = ', ';
$newstr = str_replace($order, $replace, $str);
return $newstr;
}
You must use single quote in order for PHP to understand your parameter mystriptag.
So the correct line would be :
array_walk($_POST, 'mystriptag');

f.open() issue resulting in Unbound error for f.close()

I don't quite have an answer but I'm narrowing it down. Somehow I'm mixing/confusing types, I believe, between what is provided by commands like 'os.path' and type str().
As I've made the assignment of the logfile(s) globally, even though I can print it in the function, when the variable is used in fout = open(... it's actually a null that's being referenced, i.e. open() doesn't like/can't use the type it finds.
The error:
UnboundLocalError: local variable 'fout' referenced before assignment
I am simply writing a log of dot files (left on USB drives by OSX) for deletion, but the try/except is now falling over. First the original version.
working code:
logFile = "/Users/dee/Desktop/dotFile_names.txt"
try:
fout = open(logFile, 'w')
for line in dotFile_names:
fout.write(line)
except IOError as e:
print ("Error : %s not found." % fout)
finally:
fout.close()
Attempting better practice, I sought to put the log file specs and path as variables so they can be modified if need be - I hope to make it cross platform workable. these variables are at the head of the program, i.e. not in main(), but I pass them in and print() statements have shown me they are successfully being referenced. i.e. I get this printed:
/Users/dee/Desktop/dotFile_names.txt
Despite this the error I get is:
UnboundLocalError: local variable 'fout' referenced before assignment -
error points at the "fout.close()" line
Error producing code
logFilespec = "dotFile_names.txt"
fullLogFileSpec = []
userDesktop = os.path.join(os.path.expanduser('~'), 'Desktop')
fullLogFilespec = os.path.join(userDesktop, logFilespec)
try:
print "opening " + fullLogFilespec
fout = open(fullLogFileSpec, 'w')
for line in dotFile_names:
print "..", # are we executing this line..?
fout.write(line)
except IOError as e:
print ("Error : %s not found." % fout)
finally:
print "\nclosing " + fullLogFilespec
fout.close()
I've found that if I modify this line by converting to a string
fout = open(fullLogFileSpec, 'w')
fout = open(str(fullLogFileSpec), 'w')
the error goes away, BUT NO file is created on the Desktop!
At the very least I guess that I am passing something unrecognisable to fout = open() but it is not being caught by the except. Then when I pass something that does seem to allow fout =open() to work it seems to be a ghost?
So I figure I am lost between a String and whatever kind of reference/pointer os.path.expanduser() gives me.
I'm sure it's insanely simple. Before adding the str() code I also checked all indentation, removing them all and adding back using the editor indent hotkeys, just in case that was affecting things somehow.
OK, it looks like I was wearing my dumb glasses, I think declaring
fullLogFileSpec = []
as a list instead of a string was my error.
Similar as it is, having re-written it without that list declaration this code is working fine:
logfile_directory = os.path.join(os.path.expanduser('~'),'Desktop')
log_bf_file_spec = 'ItemsFoundByFolder_' + Deez_1.current_datetime() + '.txt'
log_by_folder = os.path.join(logfile_directory, log_bf_file_spec)
the function later calls, with no error:
fout_by_folder = open(log_by_folder, 'w')

Error creating PDF with Zend (fontWithName)

I'm trying to generate a PDF file using Zend but I keep getting errors when trying to set the font.
Here is my code:
$pdf = new Zend_Pdf();
$page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
$font = new Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
$page->setFont($font,24)
->drawText("Hello world!",72,720);
$pdf->$page;
$pdf->save("example.pdf");
And this is the error:
Parse error: syntax error, unexpected 'fontWithName' (T_STRING), expecting variable (T_VARIABLE) or '$' in /Users/pawel/Sites/Zend/application/modules/default/controllers/IndexController.php on line 83
I think you can just remove new for the font declaration:
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
$style = new Zend_Pdf_Style();
$style->setFont($font, 24);
$page->setStyle($style);
fontWithName is a static function and Zend_Pdf_Font is an abstract class.
See the documentation for example.
There is another problem:
Replace
$pdf->$page;
by
$pdf->pages[] = $page;

CTFontManagerRegisterGraphicsFont does not register the family name

For reasons related to licensing, I have to decrypt a font file and load it into memory then then register it instead of reading directly from a URL. For this I have to use CTFontManagerRegisterGraphicsFont.
The problematic part comes when I try to use [NSFont fontWithName:#"Open Sans" size:21.0] where it will only accept the font's PostScript name (i.e. ("OpenSans or OpenSans-Bold for the bold weight) and won't work the family name "Open Sans".
If a font is registered using ATSApplicationFontsPathin the info.plist file or using CTFontManagerRegisterFontsForURLs then I am able to use the font's family name. But I can't do it this way.
("Open Sans" is used here as an example)
Here's the relevant code I'm using to register the font.
NSString *fontPath = [[NSBundle mainBundle] pathForResource:#"OpenSans-Regular" ofType:#"ttf" inDirectory:#"Fonts"];
NSData *fontData = [NSData dataWithContentsOfFile:fontPath];
CGDataProviderRef fontProviderRef = CGDataProviderCreateWithCFData((CFDataRef)fontData);
CGFontRef fontRef = CGFontCreateWithDataProvider(fontProviderRef);
CFErrorRef error;
if (! CTFontManagerRegisterGraphicsFont(fontRef, &error)) {
CFStringRef errorDescription = CFErrorCopyDescription(error);
NSLog(#"Failed to load font: %#", errorDescription);
CFRelease(errorDescription);
}
CFRelease(fontRef);
CFRelease(fontProviderRef);
Is there a way to make the font family name available to NSFont with CTFontManagerRegisterGraphicsFont?
(Using Xcode 5 and targeting Mac OS X >= 10.8)
update:
Ok, to verify that the family name comes from the font file itself: I tried this with a font I have... GillSansMTStd-Light. I changed the family name in the font file to "Gill Sans Std Mt Light", and subsequently I could use [ NSFont fontWithName:#"Gill Sans MT Std Light" size:10.0 ].
fontforge font info panel:
before:
after:
I looked through all the combinations of CT/NS font and font manager APIs I could think of. Seems there is no API to associate a family name with a font--I think this data comes from the font file itself.
I guess there needs to be a (Mac) font family and font subfamily entry in the font name table. (cf. https://developer.apple.com/fonts/TTRefMan/RM06/Chap6name.html)
I have used FontForge to edit font files in the past--maybe that can help you? Seems you can install it via homebrew.
Finally, I think there's an easier way to add a font to the CTFontManager. I changed your code to:
// register the font:
NSURL * url = [[ NSBundle mainBundle ] URLForResource:#"OpenSans-Regular" withExtension:#"ttf" ] ;
assert( url ) ;
CFErrorRef error = 0 ;
bool ok = CTFontManagerRegisterFontsForURL( (__bridge CFURLRef)url, kCTFontManagerScopeProcess, &error ) ;
assert( ok && !error ) ;
// make sure it's registered (using PS name):
CTFontRef f = CTFontCreateWithName( CFSTR( "OpenSans-Regular" ), 10.0, NULL ) ;
assert( f ) ;

Applescript from Mac App says "Expected end of line but found \U201c\"\U201d."

I am trying to perform a copy/paste for my to the the last active app, here's my code:
NSString *appleScriptSource = [NSString stringWithFormat:#"\ntell application \"%#\" to activate\ntell application \"System Events\" to tell process \"%#\"\nkeystroke \"v\" using command down\nend tell", [lastApp localizedName], [lastApp localizedName]];
NSDictionary *error;
NSAppleScript *aScript = [[NSAppleScript alloc] initWithSource:appleScriptSource];
NSAppleEventDescriptor *aDescriptor = [aScript executeAndReturnError:&error];
The problem is that on some computers it works just fine, but on others it fails. My error output from error that is returned by executeAndReturnError is:
2012-06-13 17:43:19.875 Mini Translator[1206:303] (null) (error: {
NSAppleScriptErrorBriefMessage = "Expected end of line but found \U201c\"\U201d.";
NSAppleScriptErrorMessage = "Expected end of line but found \U201c\"\U201d.";
NSAppleScriptErrorNumber = "-2741";
NSAppleScriptErrorRange = "NSRange: {95, 1}";
})
I can't seem to figure out what it means or why it happens.
We tried copying the generated apple-script code into the Apple Script editor, and here it works just fine.
My App is sandboxed - i have added the bundle identifiers for the key "com.apple.security.temporary-exception.apple-events" for the apps i want to support.
Any suggestions?
I am guessing that the \u201c and \u201d are red herrings and just represent smart quotes around a double quote in the error message produced by apple script, and your issue lies with the localized name of the last application you are formatting into the script. I am not sure why you might see this on one machine and not another.
For example if the name was 'Some " App' then the double quotes would end up mismatched as it would end up injected into the middle of a double quoted string. You might want to try and replace any double quotes in the name with '\"' which will escape them.
e.g.
NSString *esc = [[lastApp localizedName] stringByReplacingOccurrencesOfString:#"\"" withString:#"\\\""];