ios6 uilabel not center text aligned - objective-c

I'm working on a ios app minimum ios 5 and i have just hit a weird issue with uilabels. or maybe im missing something obvious. anyway the problem im having is that i have a uilabel which is to be centered text aligned. all works fine on ios 5 but on ios 6 it is always is left aligned. I seen that the old way of doing uilabel text align has deprecated and that setting it as so should work.
self.topComment.textAlignment = NSTextAlignmentCenter;
But no even this way it still only is center aligned on ios 5 and left aligned on ios 6.
I do have some code that resizes the font of the text in the label to try make it fit with a min and max size.
UIFont *font = self.topComment.font;
for(int i = maxFont; i > minFont; i--)
{
// Set the new font size.
font = [font fontWithSize:i];
CGSize constraintSize = CGSizeMake(self.topComment.frame.size.width, 1000);
CGSize labelSize = [topString sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
if(labelSize.height <= self.topComment.frame.size.height ) {
fits = YES;
break;
}
//self.topComment.text = topString;
}
self.topComment.font = font;
self.topComment.text = topString;
So that is the only thing i am doing to the label but it always is left aligned in ios 6. Important to note that if i drop in a uilabel with text and center align it and dont use the above code then it is centered on both ios 5 and 6.

Ok looks like turning on Tighten Letter Spacing has a different result on ios 5 and 6. I honestly cant explain why the difference but just turning this off gives the desired centering of labels for me. Just leaving this here as an answer in case anyone else makes the same silly mistake.
For example:
lbl.adjustsLetterSpacingToFitWidth = NO;
Note this is deprecated in iOS 7.0.

For iOS 6 or later:
label.textAlignment = NSTextAlignmentLeft;

Related

Dynamically changing font size of text without changing UILabel size

I am developing an application in which I am having fixed size UILabel which may contain text of any size.
I need to resize font size without changing UILabel size.
Following is how I have tried to achieve this:
UILabel *errorMessageLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,50,300,30)];
errorMessageLabel.lineBreakMode =NSLineBreakByTruncatingTail;
errorMessageLabel.numberOfLines = 0;
errorMessageLabel.adjustsFontSizeToFitWidth=YES;
errorMessageLabel.adjustsLetterSpacingToFitWidth=YES;
if ([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0) {
[errorMessageLabel setTextAlignment:NSTextAlignmentCenter];
}
else{
[errorMessageLabel setTextAlignment:UITextAlignmentCenter];
errorMessageLabel.minimumFontSize=5.0;
}
[errorMessageLabel setTag:405];
[errorMessageLabel setFont:[UIFont systemFontOfSize:13]];
[backgroundView addSubview:errorMessageLabel];
Above solution works for iOS 7 but when I try to run this on iOS 6 it changes font size properly but sets text alignment to left ignoring its centre alignment.
Can any one please let me know how I can achieve this? I tried to search form other solution but nothing is working. I cannot change UILabel's size
errorMessageLabel.numberOfLines = 1;
errorMessageLabel.adjustsFontSizeToFitWidth = YES;
errorMessageLabel.minimumFontSize = MIN_FONT_SIZE;

UITextView text is missing when adjust frame according to text in iOS7?

Hi I am trying to set UITextView height as per content. Its working fine for adjusting frame of UITextView.
But coming to text is missing as like below image shown. This happn only in iOS7 . Its displaying well in iOS6. If i tap on textview i started to edit the textview complete text is dispalying.
I wrote the fllowing lines of code` notesField.text = [editWellObject objectForKey:#"Notes"];
// Adjusting Frame as per text
CGSize size = [notesField sizeThatFits:CGSizeMake(notesField.frame.size.width, FLT_MAX)];
CGRect frame = notesField.frame;
float difference = size.height - frame.size.height;
frame.size.height = size.height;
notesField.frame = frame;
Please help me in this issue. Thanks in Advance.
Few days back I was struggling with the same issue and I have found solution with resetting property scrollEnable from NO to YES. Following is my code snippet
internalTextView.scrollEnabled = NO;
if (newSizeH >= maxHeight)
internalTextView.scrollEnabled = YES;

UITextView Right Align Margin/Rendering Issue

In my UITextView, I remove all padding and set the text to align to the right. On iOS 7, this works without a hitch. However, on iOS 6, the padding is still present on the right for an unknown reason. And when the text is just a certain size, it doesn't wrap properly because the height is calculated to fit on one line, but it is being displayed on two lines. Compare these screenshots (and to note, this occurs on both device and simulator):
I'm also setting zero margins on each of my text views using a 'utility' function as seen below:
+ (void) setZeroInsetsForTextView:(UITextView *) textView {
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
// iOS 6
textView.contentInset = UIEdgeInsetsMake(-11.0, -8.0, -11.0, -8.0); // top, left, bottom, right
} else {
// iOS 7
textView.textContainer.lineFragmentPadding = 0.0;
textView.textContainerInset = UIEdgeInsetsZero;
}
}
Any ideas? I'm not quite sure what I need to do to address this or if I just forget iOS 6 people, as they are an ever-shrinking market.
Thank you. :)

Corner Radius is not working in iOS7

I want to make my tableViewCell with rounded corner. It is work fine upto iOS6. But in iOS7 rounded corners are not shown.
I used
((UIView*)[self viewWithTag:200]).layer.cornerRadius = 8;
((UIView*)[self viewWithTag:200]).layer.masksToBounds = YES;
Please help me.
Thanks.
Try this category. For me it is working for all iOS.
- (void)setRoundedBorder:(float) radius borderWidth:(float)borderWidth color:(UIColor*)color
{
CALayer * l = [self layer];
[l setMasksToBounds:YES];
[l setCornerRadius:radius];
// You can even add a border
[l setBorderWidth:borderWidth];
[l setBorderColor:[color CGColor]];
}
iOS 7 does not support rounded corners in grouped tables anymore.
iOS 7 is a major overhaul of the whole GUI. Many things have changed, including the appearance of the UITableViews.
You can try to create a custom cell which draws a rounded rect. You have to identifiy the first and last cell in your TableView and only draw the custom View, Background, whatever for those cells.
Here is a link that may help you, although it is targeted for iOS 6:
changing corner radius of uitableview grouped in iOS6

NSAttributedString boundingRectWithSize gives different heights between iOS 6 and iOS 7

Recently updated my App to iOS 7 using XCode 5 and found that boundingRectWithSize gives different heights (in the size part) calculating the bounds of attributed Strings.
The following line gives me different Results between iOS 6 and iOS 7:
CGRect rect = [self boundingRectWithSize:CGSizeMake(inWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
"self" is an NSAttributedString and "inWidth" is the maximum width in pixels the string should fit in.
I think thats because iOS 7 has a different font handling than iOS 6.
Anyone got a working solution to calculate the height of a string on both iOS versions?
As we cant use sizeWithAttributes for all iOS greater than 4.3 we have to write conditional code for 7.0 and previous iOS. So I suggest to use given solution
UILabel *gettingSizeLabel = [[UILabel alloc] init];
gettingSizeLabel.font = [UIFont fontWithName:[AppHandlers zHandler].fontName size:16];
gettingSizeLabel.text = #"YOUR TEXT HERE";
gettingSizeLabel.numberOfLines = 0;
CGSize maximumLabelSize = CGSizeMake(310, 9999); // this width will be as per your requirement
CGSize expectedSize = [gettingSizeLabel sizeThatFits:maximumLabelSize];
The option is quite well and working smoothly in all iOS without conditional code.
I had the same problem, for me a simple ceil() on the height solved it.
Also be sure to set the right attributes for youre attributed string e.g.
#{NSParagraphStyleAttributeName: paragraphStyle, NSFontAttributeName : label.font}