Getting dimensions of an UITextView - iphone

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

Related

Change the Font Style - (Set Lable text in All-Caps format ) in Objective C

I want to set the UILable text font style in Small-Caps format like below image.
Please give me the solution for this if anyone know,
Thanks.
:)
If I didn't get you wrong, is this what you want?
NSString *uppercaseString = [yourString uppercaseString];
Fonts available in iOS don't have "capitalic style". You should add own font or try to create font using function CTFontDescriptorCreateCopyWithFeature.
I think that the simplest way will be to build attributed string (NSAttributedString) with mixed font sizes.
For iOS 7 custom fonts, the method is described by Anthony Mattox. For system font I do not know a way.
Typesetting a font in small caps on iOS
you can try with this
NSString* str=#"mudit";
label.text=[str uppercaseString];
it will give you the output like this:MUDIT
To make the UILabel render as an upper-case string, where the first letter of each word is larger, you can do something like this:
#implementation CapitalicTextLabel
- (void)drawRect:(CGRect)rect
{
// Drawing code
NSArray* words = [self.text componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetCharacterSpacing(context, 1);
CGContextSetFillColorWithColor(context, [self.textColor CGColor]);
CGAffineTransform myTextTransform = CGAffineTransformScale(CGAffineTransformIdentity, 1.f, -1.f );
CGContextSetTextMatrix (context, myTextTransform);
CGFloat x = 0;
float centeredY = (self.font.pointSize + (self.frame.size.height - self.font.pointSize) / 2) - 2;
CGFloat firstLetterSize = self.font.pointSize * 1.4;
for (NSString* word in words)
{
NSString* letter = [[word substringToIndex:1] uppercaseString];
CGContextSelectFont(context, [self.font.fontName cStringUsingEncoding:NSASCIIStringEncoding], firstLetterSize, kCGEncodingMacRoman);
CGContextShowTextAtPoint(context, x, centeredY, [letter cStringUsingEncoding:NSASCIIStringEncoding], [letter length]);
x = CGContextGetTextPosition(context).x;
NSString* restOfWord = [[[word substringFromIndex:1] uppercaseString] stringByAppendingString:#" "];
CGContextSelectFont(context, [self.font.fontName cStringUsingEncoding:NSASCIIStringEncoding], self.font.pointSize,
kCGEncodingMacRoman);
CGContextShowTextAtPoint(context, x, centeredY, [restOfWord cStringUsingEncoding:NSASCIIStringEncoding], [restOfWord length]);
CGPoint v = CGContextGetTextPosition(context);
x = CGContextGetTextPosition(context).x;
}
}
#end
This implementation doesn't handle splitting across multiple-lines or honor the TextAlignment setting, This would be should be simple enough to add afterwards.
Example:
try with this one
NSString *string = #"askdfjgjksdfgh";
NSString *upString = [string uppercaseString];
NSLog(#"U %#", upString);
It is not possible to change the font size or type of a individual letter within a UILabel.
What you want to do is to have 2 labels, one in the begging for the first bigger letter and one right after that for the remaining word.
In order to access the first letter and the rest of the word you may use:
NSString * word = #"COMPLETED";
NSString * firstLetter = [word substringToIndex:1];
NSString * remainingWord = [word substringFromIndex:1];
What you see in the picture are probably pictures of words and not UILabels.

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)

Problem with struct vs. NSString

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

UITextView position

Is there a way to find ypos in a UITextView, or/and find which row that currently marker is active?
you need to do some calculation
find cursor position by
NSRange cursorPosition = [tf selectedRange];
substring from cursor position use this sub string to calculate width of the string by
sizeWithFont:constrainedToSize:
and then divide it by width of the your TextView width.. it will give you at which line your cursor is... It's logically seems correct... haven't tried it... try it let me know if it is working or not..
Something like that:
- (int) getCurrentLine: (UITextView *) textView{
int pos = textView.selectedRange.location;
NSString *str =textView.text;
NSCharacterSet *charset = [NSCharacterSet whitespaceAndNewlineCharacterSet];
int num = pos;
for (; num< str.length; num++){
unichar c = [str characterAtIndex:num];
if ([charset characterIsMember:c]) break;
}
str = [str substringToIndex:num];
CGSize tallerSize = CGSizeMake(textView.frame.size.width - 16, 999999);
CGSize lc1 = [#"Yyg" sizeWithFont: textView.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap];
CGSize lc = [str sizeWithFont: textView.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap];
lc.height = lc.height / lc1.height;
return lc.height;
}

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