Problem with struct vs. NSString - iphone

Another (hopefully easy) beginner's question: I tried to measure the size of an UITextView, but I got a big compiler error for the following code:
NSString *temp2String;
temp2String = [NSString stringWithFormat:#"%#",[textView text]];
CGSize strSize = [temp2String sizeWithFont:#"Courier" constrainedToSize:CGSizeMake(200, 10000)
lineBreakMode:UILineBreakModeWordWrap];
NSString *temp2 = [[NSString alloc] initWithFormat:#"String width: %f", strSize.width];
textViewSizeW.text = temp2;
[temp2 release];
The error was: Incompatible Objective-C types 'struct NSString *', expected 'struct UIFont *'when passing argument 1 of sizeWithFont:constrainedToSize:lineBreakMode:' from distinct Objective-C type".
I'm afraid but this sounds like goobledigook to me. Have I messed up Strings and Structs? I tried to be so careful not to mix them this time (I got it wrong earlier on... see my earlier post).
Any help would be very much appreciated!

In plain english, the error is "argument 1 of X method is an NSString object, expected a UIFont object".
So, the correct code is:
UIFont *strFont = [UIFont fontWithName:#"Courier" size:12];
CGSize strSize = [temp2String sizeWithFont:strFont constrainedToSize:CGSizeMake(200, 10000)
lineBreakMode:UILineBreakModeWordWrap];

You are calling the sizeWithFont: method with a paramater of NSString type "Courier". You need to instantiate a UIFont object using [UIFont fontWithName:#"CourierBlah" size:12.0 and pass the UIFont Object as the paramater.

goobledigook? It's pretty clear what went wrong.
you passed a struct NSString (#"Courier") but the method expected a struct UIFont.
I guess this is still the textview problem. so pass the font of the textview.
CGSize strSize = [temp2String sizeWithFont:textView.font constrainedToSize:CGSizeMake(200, 10000)
lineBreakMode:UILineBreakModeWordWrap];

Related

How to add a Euro currency symbol in a PDF in Objective-C

I am making a PDF file in Objective-C and all goes well. But when I add some curreny symbols in the PDF, it shows something totally different.
‚dž2,060,0 instead of €2,060,0
I have uses the following code to draw text in a PDF:
NSString *reducedString = #"€4,854,525";
CGContextShowTextAtPoint (currentContext,xPos, yPos, [textToDraw UTF8String], [reducedString length]);
Anyone knows how to draw the Euro symbol using this same code ? Is there anything I need to change in the text encoding?
[reducedString drawAtPoint:CGPointMake(xPos ,yPos) withFont:[UIFont systemFontOfSize:15];
Thanks in advance.
Try using the unicode of euro symbol
\u20AC
Right way to go is needs to use NSNumberFormatter.
NSInteger currency = 4345;
NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSString *result = [numberFormatter stringFromNumber:[NSNumber numberWithInteger:currency]];
And result will be string with currency sign depending on the locale.
Try this
//for the document Header
NSString *textToDraw = #"Your text";
UIFont *font = [UIFont boldSystemFontOfSize:18.0];
CGSize stringSize = [textToDraw sizeWithFont:font constrainedToSize:CGSizeMake(pageSize.width - 2*kBorderInset, pageSize.height - 2*kBorderInset - 2*kMarginInset) lineBreakMode:UILineBreakModeWordWrap];
//for title
fYheight=fYheight+25;
CGRect renderingRect = CGRectMake(kBorderInset, fYheight, pageSize.width - 2*kBorderInset, stringSize.height);
[textToDraw drawInRect:renderingRect withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
Add your text to textToDraw string. No need to convert to constant char.

Access last line while using UILineBreakModeWordWrap UILabel in iPhone

I am displaying a long string using UILabel with UILineBreakModeWordWrap. It is showing the string perfectly by wrapping text in UILabel. I want to access last line of UILabel. Does anyone know how to do this on an iPhone?
So I tried some stuff and searched a little around. What you wuld actually need is to count the word wraps and somehow detect the last string. But I didnt really figuere out how to do that.
So my sollution is something like this:
Your String //I googled some longer String
NSString *string = #"Standing across the room, I saw you smile\nSaid I want to talk to you-oo-oo for a little while\nBut before I make my move my emotions start running wild\nMy tongue gets tied and that's no lie\nLooking in your eyes\nLooking in you big brown eyes ooh yeah\nAnd I've got this to say to you\nHey!\nGirl I want to make you sweat\nSweat till you can't sweat no more\nAnd if you cry out\nI'm gonna push it some, more, more\nGirl I want to make you sweat\nSweat till you can't sweat no more\nAnd if you cry out\nI'm gonna push it\nPush it, push it some more";
Your Label:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 280, 440)];
label.font = [UIFont fontWithName:#"Helvetica" size:14];
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.text = string;
The call this Method:
NSString *result = [self getLastLineFromString:string];
NSLog(#"Result: %#", result);
getLastLineFromString: looks like this:
- (NSString *)getLastLineFromString: (NSString *)string{
NSArray *a = [string componentsSeparatedByString:#" "];
NSString *result = [a objectAtIndex:[a count]-1];
NSString *temp = #"";
int count = 1;
BOOL myBool = YES;
while (myBool) {
count++;
temp = result;
result = [a objectAtIndex:[a count] -count];
result = [NSString stringWithFormat:#"%# %#", result, temp];
NSLog(#"length: %i",[self lengthOfString:result]);
NSLog(#"result: %#",result);
//131 was a value i detected mayels, i guess u have do trick a little around to find a mathcing one for yourself
if ([self lengthOfString:result] >= 131) {
myBool = NO;
}
}
return result;
}
And The MethodlengthOfString: looks like this:
- (int)lengthOfString:(NSString *)string{
CGSize size1 = [string sizeWithFont:[UIFont fontWithName:#"Helvetica" size:14]];
return size1.width;
}
Output:
2012-01-06 16:17:08.341 get length[5472:207] result: it
Push it, push it some more
I know this is not a perfect sollution, but it might help you.

Set a value for CGRect.frame.size from a NSDictionary

I generate a dictionary from a JSON string, and one of the value of the dictionary aims to be used as the height of a WebView object.
NSDictionary *dic = [jsonParser objectWithString:response];
CGFloat *height = [dic valueForKey:#"intHeightPixels"];
CGRect frame = webView.frame;
frame.size.height = height;
webView.frame = frame;
In line 2 I get the following error:
Initializing 'CGFloat' (aka float) with an expression of incompatible type 'id'.
I'm newbie in Objective C I don't know if this has something go see with pointers, casting, please give me some light.
you can take particular value in string i.e,
NSString *str=[arr valueForKey:#"location"];
CGRect rect9 = CGRectFromString(str);
This will convert to CGRect so that you can use it in the place of Frame
You might want to try CGFloat *height = [[dic valueForKey:#"intHeightPixels"] floatValue];
In dictionaries there is only object (here it's a NSNumber)

Getting dimensions of an UITextView

I would like to determine the dimensions (especially number of lines) of a certain text string if placed in an UITextView with the dimensions 200 x 460 pixels, font Courier.
I tried to call the following method in order to get an integer which I can then display. However, it won't work (xCode tells me that I have incompatible types in the initialization):
NSString *temp2String;
temp2String = [NSString stringWithFormat:#"%#",[textView text]];
int strSize = [temp2String sizeWithFont:#"Courier" constrainedToSize:CGSizeMake(200, 10000)
lineBreakMode:UILineBreakModeWordWrap];
NSString *temp2 = [[NSString alloc] initWithFormat:#"Size of string: %d", strSize];
textViewSize.text = temp2;
[temp2 release];
I am a beginner and I posted a similar question earlier today, but I still can't figure out how to get CGSize to work and give me the answer I need. Any help would be highly appreciated.
CGSize is a structure (from CGGeometry.h):
struct CGSize {
CGFloat width;
CGFloat height;
};
Hence all you need to do is to fetch it and display whatever dimension you need to;
CGSize strSize = [temp2String sizeWithFont:#"Courier" constrainedToSize:CGSizeMake(200, 10000)
lineBreakMode:UILineBreakModeWordWrap];
Following your example (note the dimensions are given in float-values):
NSString *temp2 = [[NSString alloc] initWithFormat:#"Width of string: %f", strSize.width];
textViewSize.text = temp2;
[temp2 release];
CGSize is a struct. It contains two floats, named 'height' and 'width'.
CGSize strSize = [temp2String sizeWithFont:#"Courier" constrainedToSize:CGSizeMake(200, 10000)
lineBreakMode:UILineBreakModeWordWrap];
NSLog(#"Height %f, width %f", strSize.height, strSize.width);

Why is my sizeWithFont:constrainedToSize:lineBreakMode: always returning zero?

I'm trying to make a UITableViewCell that adjusts its height based on the length of a string it's displaying, but am getting hung up on this method.
Here's what I've got:
NSString *text = #"A really long string in here";
CGSize theSize = [text sizeWithFont:[UIFont boldSystemFontOfSize:18.0f] constrainedToSize:CGSizeMake(265.0f, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
NSString *stringHeight = [NSString stringWithFormat:#"%d", theSize.height];
No matter what, stringHeight displays as 0. What am I missing?
Thanks!
CGFloats correspond to the C float data type; the proper format specifier for them is %f:
NSString *text = #"A really long string in here";
CGSize theSize = [text sizeWithFont:[UIFont boldSystemFontOfSize:18.0f] constrainedToSize:CGSizeMake(265.0f, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];
NSString *stringHeight = [NSString stringWithFormat:#"%f", theSize.height];
Also, you should use CGFLOAT_MAX instead of MAXFLOAT
You could also use
NSStringFromCGSize(theSize);