Issue AVSpeechUtterance - objective-c

I´m trying to make the AVSpeech speak up two NSString in the string *combine and pas it to AVSpeech. But it´s only *speekone that get passed to the *combine string. So I only ge the first string spoken. Am I declaring the string wrong somehow or do I need to change my method?
- (IBAction)UIButtonPlayPressed:(UIButton *)sender{
NSString *speekone = _activity.activityName;
NSString *speektwo = _activity.activityDescription;
NSString *combined = [NSString stringWithFormat:speekone, speektwo];
if (speechPaused == NO) {
//Title for button [self.UIButtonPlay setTitle:#"Pause" forState:UIControlStateNormal];
[self.synthesizer continueSpeaking];
speechPaused = YES;
NSLog(#"playing");
} else {
//Titleforbutton [self.UIButtonPlay setTitle:#"Play" forState:UIControlStateNormal];
speechPaused = NO;
[self.synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
NSLog(#"paused");
}
if (self.synthesizer.speaking == NO) {
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:combined];
[self.synthesizer speakUtterance:utterance];

The first argument to your stringWithFormat is not a format specifier.
This line:
NSString *combined = [NSString stringWithFormat:speekone, speektwo];
should become:
NSString *combined = [NSString stringWithFormat:#"%# %#", speekone, speektwo];

Related

How to change font in webview for loadRequest

- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSString *string = [NSString stringWithFormat:#"document.body.style.zoom = %#;", [UIFont fontWithName:centuryGothicBold size:5]];
[self.longNewsWebView stringByEvaluatingJavaScriptFromString:string];
}
Finally got a answer for my question after long research
- (void)webViewDidFinishLoad:(UIWebView *)webView{
[self stopProgressHUD];
NSString *cssString = [[[[#"body { font-family:" stringByAppendingString:centuryGothicRegular] stringByAppendingString:#"; font-size:"] stringByAppendingString:[NSString stringWithFormat:#"%ld",normalFont]] stringByAppendingString:#"}\""] ; //1
NSString *javascriptString = #"var style = document.createElement('style'); style.innerHTML = '%#'; document.head.appendChild(style)"; // 2
NSString *javascriptWithCSSString = [NSString stringWithFormat:javascriptString, cssString]; // 3
NSLog(#"javascriptWithCSSString: \n %#", javascriptWithCSSString);
NSString *loadedContent = [webView stringByEvaluatingJavaScriptFromString:javascriptWithCSSString];
NSLog(#"loaded ocntent: %#", loadedContent);
}
centuryGothicRegular is the name of font which where declare as constant.
normalFont is font size which is also declare as integer constant

NSRTFPboardType & paste into gmail browser window?

I have a NSTextView which contains some text with attributes (syntax highlighting). I'm trying to have a copy option which keeps the syntax highlighting so that I can paste it into a gmail text window. Currently the highlighting does not appear when I copy paste it, however if I were to copy the following section directly from this stackoverflow page:
- (void) copyAsRTF
{
NSPasteboard *pateboard = [NSPasteboard generalPasteboard];
NSData * rtfData = [[self textStorage] RTFFromRange: [self selectedRange]
documentAttributes: nil];
if (rtfData)
{
NSString * test = [[NSString alloc] initWithData: rtfData
encoding: NSUTF8StringEncoding];
[pateboard declareTypes: #[NSRTFPboardType]
owner: self];
[pateboard setData: rtfData
forType: NSRTFPboardType];
} // End of we had data
} // End of copyAsRTF
And paste it into gmail, it will paste with full syntax highlight no problem. The above code is what I use for generating my RTF code and I can confirm that it does generate proper RTF as I have a test variable being generated.
Any ideas what I am doing wrong here? To my understanding this SHOULD work.
(I should note that I have tried in multiple browsers - Chrome, Safari and Firefox).
As discovered in comments, in order to be working in chrome, you need to copy your text as html (public.html pasteboard type) or probably both html an rtf types.
Based on the comments with #Sega-Zero, I came up with the following (a bit sloppy, but it does the trick). For my specific needs, the html color & font should match the attributed string, so I enumerate that and create an html variable. This works in Safari, Chrome and Firefox.
#define PasteBoardTypeHTML #"public.html"
- (void) copyAsRTF
{
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
// Get our RTF data
NSData * rtfData = [[self textStorage] RTFFromRange: [self selectedRange]
documentAttributes: nil];
// Get the HTML for our current data
NSData *htmlData = [[self htmlForRange: self.selectedRange] dataUsingEncoding: NSUTF8StringEncoding];
NSMutableArray * clipboardTypes = [NSMutableArray array];
if (nil != rtfData)
{
[clipboardTypes addObject: NSPasteboardTypeRTF];
} // End of we have rtf
if(nil != htmlData)
{
[clipboardTypes addObject: PasteBoardTypeHTML];
} // End of we have html
// Set our pasteboard types (types need to be declared before we call setData).
[pasteboard declareTypes: clipboardTypes.copy
owner: self];
if (rtfData)
{
[pasteboard setData: rtfData
forType: NSPasteboardTypeRTF];
} // End of we have rtf
if(htmlData)
{
[pasteboard setData: htmlData
forType: PasteBoardTypeHTML];
} // End of we have html
} // End of copyAsRTF
- (NSString *) htmlForRange: (NSRange) range
{
NSMutableString * htmlOutput = [NSMutableString stringWithString: #"<meta charset='utf-8'><pre>"];
[[self textStorage] enumerateAttributesInRange: range
options: 0
usingBlock:
^(NSDictionary * attributes, NSRange range, BOOL * stop)
{
NSString * actualCode = [[self textStorage].string substringWithRange: range];
actualCode = [self textToHtml: actualCode];
NSMutableString * currentHtml = [NSMutableString stringWithString: #"<span"];
NSColor * color = attributes[NSForegroundColorAttributeName];
NSFont * font = attributes[NSFontAttributeName];
NSMutableArray * fontStyles = [NSMutableArray array];
if(nil != color)
{
NSString * fontColorStyle =
[NSString stringWithFormat: #"color: %#;", [color hexadecimalValue]];
[fontStyles addObject: fontColorStyle];
} // End of we have a color
if(nil != font)
{
NSString * fontDetailsStyle =
[NSString stringWithFormat: #"font-family: %#;", font.familyName];
[fontStyles addObject: fontDetailsStyle];
} // End of we have a font
if(nil != fontStyles && fontStyles.count > 0)
{
[currentHtml appendFormat: #" style=\"%#\"", [fontStyles componentsJoinedByString: #" "]];
} // End of we have font styles
[currentHtml appendString: #">"];
[currentHtml appendString: actualCode];
[currentHtml appendString: #"</span>"];
// Add our section
[htmlOutput appendString: currentHtml];
}]; // End of attribute enumerations
// End of html output
[htmlOutput appendString: #"</pre></meta>"];
return htmlOutput.copy;
} // End of htmlForRange:
- (NSString*)textToHtml:(NSString*)htmlString
{
htmlString = [htmlString stringByReplacingOccurrencesOfString:#"&" withString:#"&"];
htmlString = [htmlString stringByReplacingOccurrencesOfString:#"<" withString:#"<"];
htmlString = [htmlString stringByReplacingOccurrencesOfString:#">" withString:#">"];
htmlString = [htmlString stringByReplacingOccurrencesOfString:#"""" withString:#"""];
htmlString = [htmlString stringByReplacingOccurrencesOfString:#"'" withString:#"'"];
htmlString = [htmlString stringByReplacingOccurrencesOfString:#"\n" withString:#"<br>"];
return htmlString;
} // End of textToHtml:

Cannot append to NSString

I'm trying to append the file extension to a the stringValue returned by a subclassed NSTextFieldCell
I've tried everything I knew and could find on the internet, but this is just giving me a headache
the method is the following:
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
NSMutableString *filename = [[NSMutableString alloc] init];
[filename appendString:self.stringValue];
NSString *iconFileName = [[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:filename] stringByAppendingPathExtension:#"png"];
NSLog(#"%#", iconFileName);
}
The returned value is without the extension though!
I've also tried the following:
filename = [NSString stringWithFormat: #"%#.png", filename];
This returns the "filename" string without the ".png"
Similarly:
filename = [filename stringByAppendingString: #".png"];
returns just the "filename"
The table column where this cell belongs to is bound to an NSObject, and the method that sends the data to the column is the following:
- (NSString *) nationString {
NSMutableString *string = [[NSMutableString alloc] init];
int index = 0;
if (nationAddress && nationAddress > 0x0) {
index = [[[[controller database] nationsAddressIndex] valueForKey:[NSString stringWithFormat:#"%lli", nationAddress]] intValue];
Nation *nationality = [[[controller database] nations] objectAtIndex:index];
[string appendString:[nationality name]];
}
else {
[string appendString:#"---"];
}
return string;
}
Anyone has any idea why this might be happening, or can suggest any alternatives?
Any help will be appreciated
Thanks
This should return the complete path with extension:
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"Default.png"];
NSLog(#"%#", path);
So, assuming self.stringValue includes the extension, your method should work with this:
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
NSString *iconFileName = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:self.stringValue];
NSLog(#"%#", iconFileName);
}
If it doesn't include the extension, try this:
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
NSString *strWithPath = [NSString stringWithFormat:#"%#.png", self.stringValue];
NSString *iconFileName = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:strWithPath];
NSLog(#"%#", iconFileName);
}
Just for test. Try to use this code and update here a output values:
NSMutableString *filename = [[NSMutableString alloc] init];
[filename appendString:self.stringValue];
NSLog(#"text1: %# ;", filename);
filename = [NSString stringWithFormat: #"%#.png", filename];
filename = [NSString stringWithFormat: #"%#.png%#", filename, filename];
NSLog(#"text2: %# ;", filename);
These should work (barring a typo):
NSString* filename = #"abc";
NSString* result1 = [NSString stringWithFormat:#"%#.png", filename];
NSString* result2 = [filename stringByAppendingString:#".png"];
NSMutableString* result3 = [filename mutableCopy];
[result3 appendString:#".png"];
If they don't appear to be working then you have some problem with how you're initializing or displaying your values.
Hint: Place an NSLog(#"The answer is %#", resultN); statement immediately after each of the above (with "resultN" changed appropriately) to see what you're getting. Keep in mind that if you look from a different object you may be looking at different variables.

Why is this code not updating my UITextField?

if (![value integerValue]) {
textField.text = [[NSString alloc] initWithFormat:#"%d", *x];
[slider setValue:(float)*x];
}
if ([value integerValue]>100) {
textField.text = [[NSString alloc] initWithFormat:#"255"];
[slider setValue:100.0];
}
if ([value integerValue]<0) {
textField.text = [[NSString alloc] initWithFormat:#"0"];
[slider setValue:0.0];
}
[textField setText:value];
The textfield's value remains the one typed in, ignoring when I set it to a new value.
Can someone please explain why?
[textField setText:value];
sets the UITextField value back to value.
You are overwriting the textField.text property at the end of your method, no matter if it was "corrected" under one of your case-descisions.
Check that last line within your original code and note that textField.text = xyz is semantically equal to [textField setText:xyz].
To fix your issue, use this corrected version of your code:
if (![value integerValue]) {
textField.text = [[NSString alloc] initWithFormat:#"%d", *x];
[slider setValue:(float)*x];
}
else if ([value integerValue]>100) {
textField.text = [[NSString alloc] initWithFormat:#"255"];
[slider setValue:100.0];
}
else if ([value integerValue]<0) {
textField.text = [[NSString alloc] initWithFormat:#"0"];
[slider setValue:0.0];
}
else {
textField.text = value;
}

how to make one string from several? Xcode

I make some method, that takes digits and operands from stack and display it in a more user friendly style. The problem is with descriptionString variable, it return null in part when topOfStack is "+". I show a log below.
+(NSString *)descriptionOfTopOfStack:(NSMutableArray *)stack
{
NSString *descriptionString;
id topOfStack = [stack lastObject];
NSString *secondInStack;
NSString *thirdInStack;
if (topOfStack)
[stack removeLastObject];
if ([topOfStack isKindOfClass:[NSNumber class]]) {
descriptionString = [topOfStack stringValue];
}
else if([topOfStack isKindOfClass:[NSString class]]){
if(([topOfStack isEqualToString:#"+"]) || ([topOfStack isEqualToString:#"—"])){
secondInStack = [self descriptionOfTopOfStack:stack];
thirdInStack = [self descriptionOfTopOfStack:stack];
descriptionString = [descriptionString stringByAppendingFormat:#"%# %# %#",thirdInStack,topOfStack,secondInStack];
NSLog(#"description is %#",descriptionString);
}
}
return descriptionString;
}
i made example with 2 + 6, this is log:
2012-02-21 22:09:39.983 Calculator[12536:f803] stack = (
2,
6,
"+"
)
2012-02-21 22:09:39.983 Calculator[12536:f803] description is (null)
Why descriptionString is null? Where i made a mistake? Thanks
In the line:
descriptionString = [descriptionString stringByAppendingFormat:#"%# %# %#",thirdInStack,topOfStack,secondInStack];
The variable descriptionString is nil. Replace that line with the following.
descriptionString = [NSString stringWithFormat:#"%# %# %#",thirdInStack,topOfStack,secondInStack];
I suspect you are not setting a value to the descriptionString variable in the other branch of the if, so you are appending string to null. You could set the variable, or you could use [NSString stringWithFormat:format]:
if ([topOfStack isKindOfClass:[NSNumber class]]) {
descriptionString = [topOfStack stringValue];
}
else if([topOfStack isKindOfClass:[NSString class]]){
if(([topOfStack isEqualToString:#"+"]) || ([topOfStack isEqualToString:#"—"])){
secondInStack = [self descriptionOfTopOfStack:stack];
thirdInStack = [self descriptionOfTopOfStack:stack];
descriptionString = [NSString stringWithFormat:#"%# %# %#",thirdInStack,topOfStack,secondInStack];
NSLog(#"description is %#",descriptionString);
}
}