How to change font in webview for loadRequest - objective-c

- (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

Related

Xcode Objective-C: Download a custom font and display it in a UILabel

First of all: Is it possible to download a custom font and display it in a UILabel?
I'm using this code to download a custom font from internet and display it in a UILabel but it does not work.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize txt_UserName;
- (void)viewDidLoad {
[super viewDidLoad];
// 1
NSString *dataUrl = #"https://dl.dafont.com/dl/?f=stars_fighters";
NSURL *url = [NSURL URLWithString:dataUrl];
// 2
NSURLSessionDataTask *downloadTask = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// 4: Handle response here
if (error == nil) {
NSLog(#"no error!");
if (data != nil) {
NSLog(#"There is data!");
[self loadFont:data];
}
} else {
NSLog(#"%#", error.localizedDescription);
}
}];
// 3
[downloadTask resume];
}
- (void)loadFont:(NSData *)data
{
NSData *inData = data;
CFErrorRef error;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data);
CGFontRef font = CGFontCreateWithDataProvider(provider);
if(!CTFontManagerRegisterGraphicsFont(font, &error)){
CFStringRef errorDescription = CFErrorCopyDescription(error);
NSLog(#"Failed to load font: %#", errorDescription);
// CFRelease(errorDescription);
}
// CFRelease(font);
// CFRelease(provider);
CTFontRef ctFont = CTFontCreateWithGraphicsFont(font, 30, NULL, NULL);
UIFont *uiFont = CFBridgingRelease(ctFont);
dispatch_async(dispatch_get_main_queue(), ^{
[self.txt_UserName setFont:uiFont];
});
[self fontTest];
}
- (void)fontTest
{
NSArray *fontFamilies = [UIFont familyNames];
for (int i = 0; i < [fontFamilies count]; i++) {
NSString *fontFamily = [fontFamilies objectAtIndex:i];
NSArray *fontNames = [UIFont fontNamesForFamilyName:[fontFamilies objectAtIndex:i]];
NSLog (#"%#: %#", fontFamily, fontNames);
}
}
#end
Anyway I'm getting this error: Failed to load font: The operation couldn’t be completed. Invalid argument
At the place of the custom font I get a default iOS font.
Of course it's possible to download a custom font and display it in a UILabel,here is my code that works well:
step 1: Assuming the font file has been saved in the sandbox path Documents / StarsFighters.ttf, load the font and get font name(in YOUR_CLASS .m file):
+ (NSString *)loadFontAndReturnFontName {
NSString *imgFilePath = [NSString stringWithFormat:#"%#/Documents/StarsFighters.ttf",NSHomeDirectory()];
NSURL *fontUrl = [NSURL fileURLWithPath:imgFilePath];
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL((__bridge CFURLRef)fontUrl);
CGFontRef fontRef = CGFontCreateWithDataProvider(fontDataProvider);
CGDataProviderRelease(fontDataProvider);
CTFontManagerRegisterGraphicsFont(fontRef, NULL);
NSString *fontName = CFBridgingRelease(CGFontCopyPostScriptName(fontRef));
return fontName;
}
step2: use custom font
NSString *fontname = [YOUR_CLASS loadFontAndReturnFontName];
//fontname is #"StarsFighters",best set it to a global variable or save it
YOUR_LABEL.font = [UIFont fontWithName:fontname size:18.0];

Issue AVSpeechUtterance

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];

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:

custom Annotations being switched when reloaded on MKMapView

I've been having this issue for a couple of weeks now, and I still have not found an answer. on my MapView I have custom annotations, and when I hit the "reload button" all the information is correct as in the annotation "title, subtitle". but the annotation has changed. the annotations are in a NSMutableArray and I'm sure that the issue i am having revolves around that. here is the code I am using to reload the annotations.
so just prevent any confusion, my custom annotations work just fine when i first load the mapView. But once i hit the reload button, all the annotation's information like "location,title, subtitle" all that is correct, just the actual annotation has changed. Like all the annotations have been switched around.
if anyone can help, it would greatly be appreciated! thanks!
- (IBAction)refreshMap:(id)sender {
NSArray *annotationsOnMap = myMapView.annotations;
[myMapView removeAnnotations:annotationsOnMap];
[locations removeAllObjects];
[citiesArray removeAllObjects];
[self retrieveData];
}
-(void) retrieveData {
userLAT = [NSString stringWithFormat:#"%f", myMapView.userLocation.coordinate.latitude];
userLNG = [NSString stringWithFormat:#"%f", myMapView.userLocation.coordinate.longitude];
NSString *fullPath = [mainUrl stringByAppendingFormat:#"map_json.php?userID=%#&lat=%#&lng=%#",theUserID,userLAT,userLNG];
NSURL * url =[NSURL URLWithString:fullPath];
NSData *data = [NSData dataWithContentsOfURL:url];
json =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
citiesArray =[[NSMutableArray alloc]init];
for (int i = 0; i < json.count; i++)
{
//create city object
NSString * eID =[[json objectAtIndex:i] objectForKey:#"userid"];
NSString * eAddress =[[json objectAtIndex:i] objectForKey:#"full_address"];
NSString * eHost =[[json objectAtIndex:i] objectForKey:#"username"];
NSString * eLat =[[json objectAtIndex:i] objectForKey:#"lat"];
NSString * eLong =[[json objectAtIndex:i] objectForKey:#"lng"];
NSString * eName =[[json objectAtIndex:i] objectForKey:#"Restaurant_name"];
NSString * eState = [[json objectAtIndex:i] objectForKey:#"type"];
NSString * annotationPic = [[json objectAtIndex:i] objectForKey:#"Annotation"];
NSString * eventID = [[json objectAtIndex:i] objectForKey:#"id"];
//convert lat and long from strings
float floatLat = [eLat floatValue];
float floatLONG = [eLong floatValue];
City * myCity =[[City alloc] initWithRestaurantID: (NSString *) eID andRestaurantName: (NSString *) eName andRestaurantState: (NSString *) eState andRestaurantAddress: (NSString *) eAddress andRestaurantHost: eHost andRestaurantLat: (NSString *) eLat andRestaurantLong: (NSString *) eLong];
//Add our city object to our cities array
// Do any additional setup after loading the view.
[citiesArray addObject:myCity];
//Annotation
locations =[[NSMutableArray alloc]init];
CLLocationCoordinate2D location;
Annotation * myAnn;
//event1 annotation
myAnn =[[Annotation alloc]init];
location.latitude = floatLat;
location.longitude = floatLONG;
myAnn.coordinate = location;
myAnn.title = eName;
myAnn.subtitle = eHost;
myAnn.type = eState;
myAnn.AnnotationPicture = annotationPic;
myAnn.passEventID = eventID;
myAnn.hotZoneLevel = hotZone;
[locations addObject:myAnn];
[self.myMapView addAnnotations:locations];
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString *annotationIdentifier = #"AnnotationIdentifier";
MKAnnotationView *annotationView = (MKAnnotationView *) [self.myMapView
dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if (!annotationView)
{
annotationView = [[MKAnnotationView alloc]
initWithAnnotation:annotation
reuseIdentifier:annotationIdentifier];
NSString *restaurant_Icon = ((Annotation *)annotation).AnnotationPicture;
NSString *restaurant_Callout = [NSString stringWithFormat:#"mini.%#",restaurant_Icon];
UIImage *oldImage = [UIImage imageNamed:restaurant_Icon];
UIImage *newImage;
CGSize newSize = CGSizeMake(75, 75);
newImage = [oldImage imageScaledToFitSize:newSize]; // uses MGImageResizeScale
annotationView.image= newImage;
annotationView.canShowCallout = YES;
UIImage *Mini_oldImage = [UIImage imageNamed:event_Callout];
UIImage *Mini_newImage;
CGSize Mini_newSize = CGSizeMake(30,30);
Mini_newImage = [Mini_oldImage imageScaledToFitSize:Mini_newSize]; // uses MGImageResizeScale
UIImageView *finalMini_callOut = [[UIImageView alloc] initWithImage:Mini_newImage];
annotationView.leftCalloutAccessoryView = finalMini_callOut;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.rightCalloutAccessoryView = rightButton;
}
else
{
annotationView.annotation = annotation;
}
return annotationView;
}
If nothing else, you're setting the icon and the callout based upon the annotation, but only doing that in viewForAnnotation if the annotation was not dequeued. You really want to do any annotation-specific customization outside of that if block, in case an annotation view is reused.
Unrelated to your reported issue, there are a few other observations:
You probably should be doing retrieveData asynchronously so you don't tie up the main thread with your data retrieval/parsing. Go ahead and dispatch the adding of the entry to your array and adding the annotation to the map in the main queue, but the network stuff and should definitely be done asynchronously.
You probably should check to make sure data is not nil (e.g. no network connection or some other network error) because JSONObjectWithData will crash if you pass it a nil value.
Your use of locations seems unnecessary because you're resetting it for every entry in your JSON. You could either (a) retire locations entirely and just add the myAnn object to your map's annotations; or (b) initialize locations before the for loop. But it's probably misleading to maintain this ivar, but only populate it with the last annotation.

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.