Hello Having some issue with my code.
Please dont judge, first time trying to figure out some errors. Im receiving a "incompatible pointer types sending 'UIFount*'to parameter of type 'NSDictionary*_Nullable'"
Here is the code. any help is appreciated...
// Entirely cover the parent view
UIView *parent = self.superview;
if (parent) {
self.frame = parent.bounds;
}
CGRect bounds = self.bounds;
// Determine the total widt and height needed
CGFloat maxWidth = bounds.size.width - 4 * margin;
CGSize totalSize = CGSizeZero;
CGRect indicatorF = indicator.bounds;
indicatorF.size.width = MIN(indicatorF.size.width, maxWidth);
totalSize.width = MAX(totalSize.width, indicatorF.size.width);
totalSize.height += indicatorF.size.height;
//**Issue is HERE**
CGSize labelSize = [label.text sizeWithAttributes: label.font];
labelSize.width = MIN(labelSize.width, maxWidth);
totalSize.width = MAX(totalSize.width, labelSize.width);
totalSize.height += labelSize.height;
if (labelSize.height > 0.f && indicatorF.size.height > 0.f) {
totalSize.height += kPadding;
}
CGFloat remainingHeight = bounds.size.height - totalSize.height - kPadding - 4 * margin;
CGSize maxSize = CGSizeMake(maxWidth, remainingHeight);
CGSize detailsLabelSize = [detailsLabel.text sizeWithAttributes: detailsLabel.font
constrainedToSize:maxSize lineBreakMode:detailsLabel.lineBreakMode];
totalSize.width = MAX(totalSize.width, detailsLabelSize.width);
totalSize.height += detailsLabelSize.height;
if (detailsLabelSize.height > 0.f && (indicatorF.size.height > 0.f || labelSize.height > 0.f)) {
totalSize.height += kPadding;
}
totalSize.width += 2 * margin;
totalSize.height += 2 * margin;
sizeWithAttribute expect a NSDictionary and you are passing UIFont in it, that's why you are getting this warning, make a dictionary like this
NSDictionary *attributes = #{NSFontAttributeName: [UIFont fontWithName:#"YourFontName" size:YourFontSize]};
And then pass this attributes dictionary
CGSize labelSize = [label.text sizeWithAttributes: attributes];
Related
I'm having a problem with a deprecated method, I'm trying to change it for one working right now, but no luck, does someone knows how to solve this?
The problem is that I'm working on a code that is not mine, so I don't get it very well.
This is the original code (the one deprecated):
CGFloat remainingHeight = bounds.size.height - totalSize.height - kPadding - 4 * margin;
CGSize maxSize = CGSizeMake(maxWidth, remainingHeight);
CGSize detailsLabelSize = [detailsLabel.text sizeWithFont:detailsLabel.font
constrainedToSize:maxSize lineBreakMode:detailsLabel.lineBreakMode];
This is my solution (that throw the error):
CGFloat remainingHeight = bounds.size.height - totalSize.height - kPadding - 4 * margin;
CGSize maxSize = CGSizeMake(maxWidth, remainingHeight);
CGSize detailsLabelSize = [[detailsLabel.text sizeWithAttributes:#{NSFontAttributeName:detailsLabel.font}]constrainedToSize:maxSize lineBreakMode:detailsLabel.lineBreakMode];
The error is in the title (bad receiver type 'CGSize' (aka 'struct CGSize'))
Thank you so much!
Add this method in your implementation file:
-(CGSize)frameForText:(NSString*)text sizeWithFont:(UIFont*)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode {
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.lineBreakMode = lineBreakMode;
NSDictionary * attributes = #{NSFontAttributeName:font,
NSParagraphStyleAttributeName:paragraphStyle
};
CGRect textRect = [text boundingRectWithSize:size
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
return textRect.size;
}
And then change your code to:
CGSize detailsLabelSize = [self frameForText:detailsLabel.text sizeWithFont:detailsLabel.font constrainedToSize:maxSize lineBreakMode:detailsLabel.lineBreakMode];
Your problem is in this line:
CGSize detailsLabelSize = [[detailsLabel.text sizeWithAttributes:#{NSFontAttributeName:detailsLabel.font}]constrainedToSize:maxSize lineBreakMode:detailsLabel.lineBreakMode];
[detailsLabel.text sizeWithAttributes:#{NSFontAttributeName:detailsLabel.font}] returns a CGSize which is not an object. You are then attempting to call constrainedToSize:maxSize lineBreakMode:detailsLabel.lineBreakMode on this CGSize, which is impossible, because it's not an object.
I searched & spend so much time to resolve the issue of content size height, but it’s hard luck, nothing worked. Similar questions are available in stack & other forums also but i am not getting what is left, which is not allowing my textview to resolve this issue.
I have to place textView’s cursor & text at centre of it’s width & height. Now i use it-
UITextView *tv = object;
[tv setScrollEnabled:YES];
CGFloat height = [tv bounds].size.height;
CGFloat contentheight;
contentheight = [tv sizeThatFits:CGSizeMake(tv.frame.size.width, FLT_MAX)].height;
NSLog(#"iOS7; %f %f", height, contentheight);
// [tv sizeToFit];
CGFloat topCorrect = height - contentheight;
topCorrect = (topCorrect <0.0 ? 0.0 : topCorrect);
tv.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};
[tv setScrollEnabled:NO];
The Second Approach i used is-
#pragma mark- UITextView ContentHeight Messurment
- (CGFloat)measureHeightOfUITextView:(UITextView *)textView
{
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
{
// This is the code for iOS 7. contentSize no longer returns the correct value, so
// we have to calculate it.
//
// This is partly borrowed from HPGrowingTextView, but I've replaced the
// magic fudge factors with the calculated values (having worked out where
// they came from)
CGRect frame = textView.bounds;
// Take account of the padding added around the text.
UIEdgeInsets textContainerInsets = textView.textContainerInset;
UIEdgeInsets contentInsets = textView.contentInset;
CGFloat leftRightPadding = textContainerInsets.left + textContainerInsets.right + textView.textContainer.lineFragmentPadding * 2 + contentInsets.left + contentInsets.right;
CGFloat topBottomPadding = textContainerInsets.top + textContainerInsets.bottom + contentInsets.top + contentInsets.bottom;
frame.size.width -= leftRightPadding;
frame.size.height -= topBottomPadding;
NSString *textToMeasure = textView.text;
if ([textToMeasure hasSuffix:#"\n"])
{
textToMeasure = [NSString stringWithFormat:#"%#-", textView.text];
}
// NSString class method: boundingRectWithSize:options:attributes:context is
// available only on ios7.0 sdk.
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];
NSDictionary *attributes = #{ NSFontAttributeName: textView.font, NSParagraphStyleAttributeName : paragraphStyle };
CGRect size = [textToMeasure boundingRectWithSize:CGSizeMake(CGRectGetWidth(frame), MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
CGFloat measuredHeight = ceilf(CGRectGetHeight(size) + topBottomPadding);
return measuredHeight;
}
else
{
return textView.contentSize.height;
}
}
The response is coming like it-
After editing it, it's appearing like it-
Now, what else i can try to put textView's text on the centre of UITextView? Please update me.
i do following code after scale uitextview but its not given me exact result
UITextView *textView = (UITextView *)[mainView viewWithTag:10];
int newFontSize,oldFontSize;
oldFontSize = textView.font.pointSize;
newFontSize =((textView.frame.size.height * textView.frame.size.width) * oldFontSize) / (textView.contentSize.height * textView.contentSize.width);
double olddistance = sqrt(pow((textView.frame.origin.x - (textView.frame.origin.x + textView.contentSize.width)), 2.0) + pow((textView.frame.origin.y - (textView.frame.origin.y + textView.contentSize.height)), 2.0));
double newDistance = sqrt(pow((textView.frame.origin.x - (textView.frame.origin.x + textView.frame.size.width)), 2.0) + pow((textView.frame.origin.y - (textView.frame.origin.y + textView.frame.size.height)), 2.0));
float scale = newDistance/olddistance;
float newWidth = scale * textView.contentSize.width;
float newHeight = scale * textView.contentSize.height;
self.frame = CGRectMake(self.frame.origin.x,self.frame.origin.y, newWidth+40, newHeight+40);
if (textView.font.pointSize * scale < 10)
{
textView.font = [UIFont fontWithName:textView.font.fontName size:10];
self.frame = CGRectMake(self.frame.origin.x,self.frame.origin.y, textView.contentSize.width,textView.contentSize.height);
}
else
{
textView.font = [UIFont fontWithName:textView.font.fontName size:textView.font.pointSize * scale];
}
I have once made a text view which resizes itself to exactly fit all the text in.
What you need is to provide the text and the width of your text view.
Here is the code:
-(CGSize) sizeForString:(NSString *)string WithWidth:(CGFloat)width {
CGSize size = [string sizeWithFont:[UIFont boldSystemFontOfSize:16]
constrainedToSize:CGSizeMake(width, MAXFLOAT)
lineBreakMode:NSLineBreakByWordWrapping];
return size;
}
The function returns the proper size for your text view, therefore, you may adjust the text view accordingly.
I have a problem on my app on iOS 7
I calculate my label size using:
CGSize constraint = CGSizeMake(275, 141);
CGSize size;
size = [_rtLabelQuestion.plainText sizeWithFont:[UIFont fontWithName:#"KGWhattheTeacherWants" size:intFont] constrainedToSize:constraint lineBreakMode:NSLineBreakByTruncatingTail];
and then place my label frame using:
_rtLabelQuestion.frame = CGRectMake(aLabelTemp.frame.origin.x, aLabelTemp.frame.origin.y, size.width + 2 , size.height + 10);
But on iOS 7 width and height of my CGSize is always smaller from exactly intFont pixels, i've been working on that 2 days and no solution, please help, thank you !
The size: you pass in to -[UIFont fontWithName:size:] is the point size of the font.
The size you get back from -[NSString sizeWithFont:constrainedToSize:lineBreakMode:] is the actual size of the laid out glyphs from the string.
If you want a given string to be a specific size, in a specific face, you will probably have to do a Newton's-method type iteration where you change the point size you're passing in until the laid out size is close enough to what you want. (I would not expect such an operation to be fast in the general case, BTW.) It might look something like this:
NSString* theString = #"FOOBAR";
CGSize constraint = CGSizeMake(275, 12);
CGFloat desiredHeight = 12.0;
CGFloat pointSize = desiredHeight;
CGFloat actualHeight = 0;
do {
CGSize size = [theString sizeWithFont: [UIFont fontWithName:#"Helvetica" size: pointSize] constrainedToSize: constraint lineBreakMode:NSLineBreakByTruncatingTail];
actualHeight = size.height;
CGFloat ratio = actualHeight / desiredHeight;
pointSize = pointSize / ratio;
} while (fabs(actualHeight - desiredHeight) > 0.01);
PS: -[NSString sizeWithFont:constrainedToSize:lineBreakMode:] is deprecated in iOS7. Compiler tells me to use -boundingRectWithSize:options:attributes:context:] instead. Using the new recommended method, it might look like this:
NSString* theString = #"FOOBAR";
CGSize constraint = CGSizeMake(275, 12);
CGFloat desiredHeight = 12.0;
CGFloat pointSize = desiredHeight;
CGFloat actualHeight = 0;
NSStringDrawingContext* ctx = [[NSStringDrawingContext alloc] init];
do {
CGRect br = [theString boundingRectWithSize: constraint options: 0 attributes: #{ NSFontAttributeName : [UIFont fontWithName:#"Helvetica" size: pointSize] } context:ctx];
actualHeight = br.size.height;
CGFloat ratio = actualHeight / desiredHeight;
pointSize = pointSize / ratio;
} while (fabs(actualHeight - desiredHeight) > 0.01);
Try using ceil to ensure you round up to an integer:
_rtLabelQuestion.frame = CGRectMake(aLabelTemp.frame.origin.x, aLabelTemp.frame.origin.y, ceil(size.width) + 2 , ceil(size.height) + 10);
CGSize viewwidth;
viewwidth=[[UIScreen mainScreen] bounds].size;
this will help you to get device current resolution using cgsize.
viewwidth.width or viewwidth.height
as above u can use it. and also as below code
rightslide=[[UIView alloc]initWithFrame:CGRectMake(0,0, viewwidth.width, viewwidth.height)];
Hi i have a MBProgressHUD on my iPad screen. Works perfectly fine. But i want to change the label to show in three lines.Like this
self.hud = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
self.hud.frame = CGRectMake(0, 0, 120, 143);
[self.navigationController.view addSubview:self.hud];
self.hud.delegate = self;
self.hud.mode = MBProgressHUDModeAnnularDeterminate;
NSString *strloadingText = [NSString stringWithFormat:#"Loading Data.\r Please Wait.\r 1-2 Minutes"];
NSLog(#"the loading text will be %#",strloadingText);
self.hud.labelText = strloadingText;
[self.hud show:YES];
So i want the label in 3 lines
Loading Data.
Please Wait
1-2 Minutes
OR
can i assign an image to the HUD?
All this should be in the labeltext. But i am ending up with only one line. How can i do that?
If you need more info, please ask.Thanks.
MBProgressHUD's detailsLabelText property is multiline but not labelText property.
So, you can try something like this
MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.frame = CGRectMake(0, 0, 120, 143);
hud.mode = MBProgressHUDModeAnnularDeterminate;
NSString *strloadingText = [NSString stringWithFormat:#"Loading Data."];
NSString *strloadingText2 = [NSString stringWithFormat:#" Please Wait.\r 1-2 Minutes"];
NSLog(#"the loading text will be %#",strloadingText);
hud.labelText = strloadingText;
hud.detailsLabelText=strloadingText2;
You can set detailsLabelText font by using the property detailsLabelFont.
I had a question like this, too !
You can set hud.label.numberOfLines = 0;
And it works!
The reason why labelText differs from detailsText, I imagine because it's meant to be a very similar to UIAlertView from the title/description perspective.
The differences between the two labels is quite distinct because of their purpose, for instance:
Titles have bigger fonts, oft times bold in comparison to detail text.
Titles are meant to be short and obvious, taken from a popular dictionary site (description speaks for itself):
Title: A descriptive name; an epithet.
I'd recommend not having a multi-line title, keeping it short, and using the description text.
The reason why multi-line titles do not work is because of the layoutSubviews implementation, the size is not being calculated. if you inspect MBProgressHud.m, within layoutSubviews,
CGFloat remainingHeight = bounds.size.height - totalSize.height - kPadding - 4 * margin;
CGSize maxSize = CGSizeMake(maxWidth, remainingHeight);
CGSize detailsLabelSize = [detailsLabel.text sizeWithFont:detailsLabel.font
constrainedToSize:maxSize lineBreakMode:detailsLabel.lineBreakMode];
totalSize.width = MAX(totalSize.width, detailsLabelSize.width);
totalSize.height += detailsLabelSize.height;
if (detailsLabelSize.height > 0.f && (indicatorF.size.height > 0.f || labelSize.height > 0.f)) {
totalSize.height += kPadding;
}
Note the -[NSString sizeWithFont: constrainedToSize: lineBreakMode:] call for the description text; this method calculates the size required to display the text - using as many lines as necessary, whereas the -[NSString sizeWithFont:] calculates the size required to display the text, but only up to displaying one line.
I would advise against having a multi-line title, and instead provide a shorter title, with some description text to accompany it.
If you simply must have the multi-line title (all changes within MBProgressHud.m):
- (void)setupLabels {
label = [[UILabel alloc] initWithFrame:self.bounds];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = MBLabelAlignmentCenter;
label.opaque = NO;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.font = self.labelFont;
label.text = self.labelText;
>>> label.numberOfLines = 0;
[self addSubview:label];
...
Replace:
CGSize labelSize = [label.text sizeWithFont:label.font];
labelSize.width = MIN(labelSize.width, maxWidth);
totalSize.width = MAX(totalSize.width, labelSize.width);
totalSize.height += labelSize.height;
if (labelSize.height > 0.f && indicatorF.size.height > 0.f) {
totalSize.height += kPadding;
}
CGFloat remainingHeight = bounds.size.height - totalSize.height - kPadding - 4 * margin;
CGSize maxSize = CGSizeMake(maxWidth, remainingHeight);
CGSize detailsLabelSize = [detailsLabel.text sizeWithFont:detailsLabel.font
constrainedToSize:maxSize lineBreakMode:detailsLabel.lineBreakMode];
With:
CGFloat remainingHeight = bounds.size.height - totalSize.height - kPadding - 4 * margin;
CGSize maxSize = CGSizeMake(maxWidth, remainingHeight);
CGSize labelSize = [label.text sizeWithFont:label.font constrainedToSize:maxSize lineBreakMode:label.lineBreakMode];
totalSize.width = MAX(totalSize.width, labelSize.width);
totalSize.height += labelSize.height;
if (labelSize.height > 0.f && indicatorF.size.height > 0.f) {
totalSize.height += kPadding;
}
remainingHeight = bounds.size.height - totalSize.height - kPadding - 4 * margin;
CGSize detailsLabelSize = [detailsLabel.text sizeWithFont:detailsLabel.font
constrainedToSize:maxSize lineBreakMode:detailsLabel.lineBreakMode];
Hope this isn't too late to help.
self.hud.minSize = CGSizeMake(300, 100);