Changing an embedded UITextView width on rotation? - ios

I have a UITextView embedded inside a grouped table view cell that I can't seem to update on rotation. It basically keeps the same width (assumed portrait) when it becomes landscape, causing about 40% of the view to be blank on the left side.
Is there a way to update this easily? I was looking at willRotateToInterfaceOrientation but I wasn't sure if that was the right thing to update the view on rotation.
Specifically, I'm thinking the width value should be updated, since it's based on the width.
if (indexPath.section == 1) {
switch (indexPath.row) {
case 0:
{
NSString *label = [self.pedal detail];
CGFloat width = [UIScreen mainScreen].bounds.size.width * .95;
CGSize stringSize = [label sizeWithFont:[UIFont systemFontOfSize:15]
constrainedToSize:CGSizeMake(320, 9999)
lineBreakMode:UILineBreakModeWordWrap];
UITextView *textField = [[UITextView alloc] init];
[textField setFrame:CGRectMake(50,0, width-10, stringSize.height+55)];
[textField setEditable:NO];
textField.backgroundColor = [UIColor clearColor];
textField.font = [UIFont systemFontOfSize:15];
textField.textAlignment = UITextAlignmentLeft;
cell.accessoryView = textField;
textField.text = [self.pedal detail];
[textField release];
break;
}
}
}

You need to set textField.autoresizingMask = UIViewAutoresizingFlexibleWidth, which streches the object on its width on view size change.
Read more at the UIView documentation

You have tried setting the sizing properties in IB right? The way the object scales and what points are anchors should be able to be set there.

Related

Text is not properly visible in UITextField

UITextField *theLastNameTF = [[UITextField alloc]init];
theLastNameTF.borderStyle = UITextBorderStyleNone;
theLastNameTF.font = [UIFont fontWithName:#"Helvetica-neue" size:24.0];
theLastNameTF.textColor = [UIColor grayColor];
theLastNameTF.textAlignment = NSTextAlignmentLeft;
[theScrollView addSubview:theLastNameTF];
I'm creating a textfield using the above code.I need to show a line under the textfield,so i'm adding a gray coloured UILabel to text field.
-(void)addLineToTextField:(UITextField *)theTextfield
{
UILabel *theSeparatorLine = [[UILabel alloc]initWithFrame:CGRectMake(0, theTextfield.frame.size.height-0.5, theTextfield.frame.size.width, 0.5)];
theSeparatorLine.frame = CGRectIntegral(theSeparatorLine.frame);
theSeparatorLine.autoresizingMask = UIViewAutoresizingFlexibleWidth;
theSeparatorLine.backgroundColor = kCS_LightGrayColor;
[theTextfield addSubview:theSeparatorLine];
theSeparatorLine = nil;
}
I'm facing a issue in textfield,while entering texts,the text are half visible and cursor is not moving towards left of text.
Please refer the image,I have entered "arun... arun 123",as expected the cursor should be at left of 123,but the cursor is not moving further.
How can i fix this this?
EDIT
This code is to reframe the UI
(call layoutViewForCurrentorientation () inside orientation change method)
-(void)layoutViewForCurrentorientation
{
if ([[UIApplication sharedApplication]statusBarOrientation] == UIInterfaceOrientationPortrait) {
[self potraitUI];
}
else
{
[self landscapeUI];
}
}
If i remove the line textField.font = [UIFont fontWithName:#"Helvetica-neue" size:24.0]; .I issue is resolved but i need bigger font in textfield.
I was using font [UIFont fontWithName:#"Helvetica-neue" size:24.0] and the textfield size was 25.I made my UITextfield height to 30 and the issue got resolved.Don't know the actual reason but it worked.
Please set the textfield frame
UITextField *theLastNameTF = [[UITextField alloc]initWithFrame:CGRectMake(50,50,250,30)];
I hope it helps you.
I'm seeing the same problem in 9.3. Appears to be an iOS bug.
When the UITextField is first responder, the UITextField contains a scrollview (UIFieldEditor) which contains the input view (a _UIFieldEditorContentView, -[UITextField inputView].) The bug is that the input view is the incorrect size. The input view is not wide enough and thus the scrollview's -contentSize is also incorrect. The UITextField is scrolling as far right as it can, but that's not far enough because the input view is too small.
My workaround is to reset the text field's text. You'll also probably need a flag to ignore delegate callbacks:
_ignoreTextDelegate = TRUE;
NSString *text = textField.text;
textField.text = nil;
textField.text = text;
_ignoreTextDelegate = FALSE;
Try this
UITextField *theLastNameTF = [[UITextField alloc]initWithFrame:CGRectMake(20,50,250,30)];
theLastNameTF.borderStyle = UITextBorderStyleNone;
theLastNameTF.font = [UIFont fontWithName:kHelveticaneue size:15.0];
theLastNameTF.textColor = [UIColor grayColor];
theLastNameTF.textAlignment = NSTextAlignmentLeft;
[theScrollView addSubview:theLastNameTF];
And set the line frame's y value should be with respect to textField frame's y value
-(void)addLineToTextField:(UITextField *)theTextfield
{
UILabel *theSeparatorLine = [[UILabel alloc]initWithFrame:CGRectMake(0, theTextfield.frame.origin.y + 0.5, theTextfield.frame.size.width, 0.5)];
theSeparatorLine.frame = CGRectIntegral(theSeparatorLine.frame);
theSeparatorLine.autoresizingMask = UIViewAutoresizingFlexibleWidth;
theSeparatorLine.backgroundColor = kCS_LightGrayColor;
[theTextfield addSubview:theSeparatorLine];
theSeparatorLine = nil;
}

Center vertically in UILabel with autoshrink

This is a little different from all the other "How do I center the text in a UILabel" questions here...
I have a UILabel with some text in it, I want to center the text vertically in the UILabel. What's the big deal, right? That's the default. The problem comes because my text is dynamic and I have autoshrink turn on. As the text grows larger, the font size shrinks. You get this behavior.
Notice that the font baseline has not moved, I want it to move so the numbers are centered vertically in the UILabel's frame.
Easy, right? I just remember the frame's original center in viewDidLoad
self.workoutTimeCenter = _workoutTimeLabel.center;
and then I call sizeToFit after I change the the text, right?
[_workoutTimeLabel sizeToFit];
_workoutTimeLabel.center = _workoutTimeCenter;
Well, sizeToFit did, I guess, exactly what it was supposed to do, resize the frame so the text fits without shrinking!
How can I vertically center the text in a UILabel while respecting baselines and autoshrink? (Note, an iOS5 and later solution is fine and I can even deal with an iOS6 and later solution.)
In my experience you can just set the -[UILabel baselineAdjustment] property to UIBaselineAdjustmentAlignCenters to achieve the effect you're describing.
From the docs:
baselineAdjustment
Controls how text baselines are adjusted when text
needs to shrink to fit in the label.
#property(nonatomic) UIBaselineAdjustment baselineAdjustment
Discussion
If the adjustsFontSizeToFitWidth property is set to YES,
this property controls the behavior of the text baselines in
situations where adjustment of the font size is required. The default
value of this property is UIBaselineAdjustmentAlignBaselines. This
property is effective only when the numberOfLines property is set to
1.
and
UIBaselineAdjustmentAlignCenters
Adjust text based relative to the center of its bounding box.
EDIT: adding a full view-controller that demonstrates this:
#interface TSViewController : UIViewController
#end
#implementation TSViewController
- (void) addLabelWithFrame: (CGRect) f baselineAdjustment: (UIBaselineAdjustment) bla
{
UILabel* label = [[UILabel alloc] initWithFrame: f];
label.baselineAdjustment = bla;
label.adjustsFontSizeToFitWidth = YES;
label.font = [UIFont fontWithName: #"Courier" size: 200];
label.text = #"00";
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor lightGrayColor];
label.userInteractionEnabled = YES;
[self.view addSubview: label];
UIView* centerline = [[UIView alloc] initWithFrame: CGRectMake(f.origin.x, f.origin.y+(f.size.height/2.0), f.size.width, 1)];
centerline.backgroundColor = [UIColor redColor];
[self.view addSubview: centerline];
UITapGestureRecognizer* tgr = [[UITapGestureRecognizer alloc] initWithTarget: self action: #selector(onTap:)];
[label addGestureRecognizer: tgr];
}
- (void) viewDidLoad
{
[super viewDidLoad];
[self addLabelWithFrame: CGRectMake(0, 0, 320, 200)
baselineAdjustment: UIBaselineAdjustmentAlignCenters];
[self addLabelWithFrame: CGRectMake(0, 220, 320, 200)
baselineAdjustment: UIBaselineAdjustmentAlignBaselines];
}
- (void) onTap: (UITapGestureRecognizer*) tgr
{
UILabel* label = (UILabel*)tgr.view;
NSString* t = [label.text stringByAppendingString: #":00"];
label.text = t;
}
#end
when working in IB, be sure to set align baselines to center
Note: line break CANNOT be word wrap for this to work, so it will NOT work multiline (good to set the line break to Truncate tail)
-(void)fitVerticallyToLabel:(UILabel *)lbl
{
CGFloat fontSize = lbl.frame.size.width / lbl.text.length;
[lbl setFont:[UIFont fontWithName:#"Helvetica-Bold" size:fontSize]];
CGRect rect = lbl.frame;
rect.origin.y += rect.size.height - fontSize;
rect.size.height = fontSize;
[lbl setFrame:rect];
}
How to Use: Call this method after setting the text to your label.
label.text = #"text";
[self fitVerticallyToLabel:label];
Note: I ahev taken UILabel from xib. You can take it programmatically too in that case you will have to set its text alignment NSTextAlignMentCenter
Try to implement this logic:
-(void)adjustLabel1Text1:(NSString *)text1
{
UILabel *lbl_first = [UIFont fontWithName:#"Helvetica" size:12];
text1 = [text1 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
float hedingLblHeight = [self calculateHeightOfTextFromWidth:text1 : [UIFont fontWithName:#"Helvetica" size:12] :118 :UILineBreakModeWordWrap];
lbl_first.text=text1;
[lbl_first setFrame:CGRectMake(lbl_first.frame.origin.x, lbl_first.frame.origin.y, 118, hedingLblHeight)];
lbl_first.lineBreakMode = UILineBreakModeWordWrap;
lbl_first.numberOfLines = 0;
[lbl_first sizeToFit];
//////////Adjust the lable or any UIControl below this label accordingly.
float endResultHeight=[self calculateHeightOfTextFromWidth:text2 : [UIFont fontWithName:#"Helvetica" size:15] :299 :UILineBreakModeWordWrap];
if(hedingLblHeight>secondImgTitleHeight)
{
[lbl_endResult setFrame:CGRectMake(lbl_endResult.frame.origin.x, lbl_first.frame.origin.y+lbl_first.frame.size.height+5, 299, endResultHeight)];
}
else
{
[lbl_endResult setFrame:CGRectMake(lbl_endResult.frame.origin.x, lbl_first.frame.origin.y+lbl_first.frame.size.height+5, 299, endResultHeight)];
}
lbl_endResult.lineBreakMode=UILineBreakModeWordWrap;
lbl_endResult.numberOfLines = 0;
[lbl_endResult sizeToFit];
}
-(float) calculateHeightOfTextFromWidth:(NSString*)text : (UIFont*) withFont:(float)width :(UILineBreakMode)lineBreakMode
{
CGSize suggestedSize = [text sizeWithFont:withFont constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:lineBreakMode];
return suggestedSize.height;
}
It has helped me a lot.Hope it works for you.
Try
yourLabel.textAlignment = UITextAlignmentCenter;

Resize text in a UILabel (subview of UITableViewCell) to fit width

I have a UITableViewCell that is customized, sometime, text in UILabel (UILabel is subview in UITableViewCell) is exceeds size, and it get some dots (like ...) in label.
I want to adjust font size to fit width. But it does not work.
Here is my code
In Class implement for UITableviewCell
-(void) awakeFromNib{
myLabel.minimumFontSize = 8;
myLabel.adjustsFontSizeToFitWidth = YES;
}
Please help me!!!
Try this simple code
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
label.backgroundColor=[UIColor clearColor];
label.text= yourString;
label.numberOfLines=0;
label.adjustsFontSizeToFitWidth=YES;
label.minimumFontSize=6;
label.textAlignment=UITextAlignmentCenter;
[self.View addSubview:label];
CGFloat fontSize = 20;
while (fontSize > 0.0)
{
CGSize size = [yourString sizeWithFont:[UIFont fontWithName:#"Rockwell-Bold" size:fontSize] constrainedToSize:CGSizeMake(label.frame.size.width, 10000) lineBreakMode:UILineBreakModeWordWrap];
if (size.height <= label.frame.size.height) break;
fontSize -= 1.0;
}
//set font size
label.font = [UIFont fontWithName:#"Rockwell-Bold" size:fontSize];
[label release];
try this one...
myLabel.numberOfLines=0;
[myLabel sizeToFit];
at First. In your TableViewClass, in heightForRowAtIndexPath you have to calculate the height of your text according to your label and return it.
and make your label's autoLayout property flexible height. You do not need to reset your label's frame. try this it will work....

Multiple lines of text in UISegmentedControl

is there an easy way, such that each item in my UISegmentedControl for instance,
has multiple lines of text?
Thanks.
ps. I've also checked there's no easy way to change the height of UISegmentedControl?
Say in Code? Changing the style to "Bar" does not suit me, and setFrame does not
seem to work in my case too... :((
pps. This is the approach I tried as recommended by Siba but still have some issues.
for (id segment in [segmentedControl subviews])
{
for (id label in [segment subviews])
{
if ([label isKindOfClass:[UILabel class]])
{
UILabel *label2 = label;
//hear u add any of delegate function to increase the height and other label functionality in this
[label2 setTextAlignment:UITextAlignmentCenter];
[label2 setFont:[UIFont boldSystemFontOfSize:12]];
//to adjust the label size manually with respect to text use below code
CGSize labelSize = CGSizeMake(100, 80);
CGSize theStringSize = [label2.text sizeWithFont:label2.font constrainedToSize:labelSize];
CGRect frame = label2.frame;
frame.size = theStringSize;
label2.lineBreakMode = UILineBreakModeWordWrap;
label2.numberOfLines = 0;
[label2 setText:#"text \n 10%"];
}
}
}
1.You can try the following.
create a multiline UILabel
fill the label with N lines of text
convert the label into an UIImage
set the image as a segments content
Like one Done in This Answer :
UISegmentedControl text with multiple lines?
2.You can Take An ImageView in the back of Segmented Control with Same frame And Change images as per SelectedSegmentedIndex Like below.
But Don't forget to set Alpha of SegmentedControl to 0.05. At ViewDidLoad.
segment_Control.alpha=0.05;
The Coding will be Like this.
- (IBAction)segmented_Changed:(id)sender {
if (segmented_control.selectedSegmentIndex==0) {
segment_image.image=[UIImage imageNamed:#"tab_Act1.png"];
}else
if (segmented_control.selectedSegmentIndex==2) {
segment_image.image=[UIImage imageNamed:#"tab_Act3.png"];
}
else if (segmented_control.selectedSegmentIndex==1) {
segment_image.image=[UIImage imageNamed:#"tab_Act2.png"];
}
}
3.Also You Can try This :
for (id segment in [segmentedControl subviews])
{
for (id label in [segment subviews])
{
if ([label isKindOfClass:[UILabel class]])
{
//hear u add any of delegate function to increase the height and other label functionality in this
[label setTextAlignment:UITextAlignmentCenter];
[label setFont:[UIFont boldSystemFontOfSize:12]];
//to adjust the label size manually with respect to text use below code
CGSize labelSize = CGSizeMake(100, 80);
CGSize theStringSize = [label.text sizeWithFont:label.font constrainedToSize:labelSize];
CGRect frame = label.frame;
frame.size = theStringSize;
}
}
}
Source : Multiline Text On UIsegment Control

How to get the header view of a grouped table view?

I want to get all the views of a grouped table view to change the label color and to set the background color.
I found the answer, it's not possible to get the header view of a table view section. But you can implement the delegate tableView:viewForHeaderInSection: to recreate the header view and the label. The following code will give you the same header view and the exact label.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}
// Create label with section title
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20.0f, 5.5f, 300.0f, 30.0f)];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:16.5];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0.0, 1.0);
label.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
label.text = sectionTitle;
// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 44.0f)];
view.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
[view addSubview:label];
return view;
}
That's great you figured out your solution.
A couple of suggestions:
Don't hardcode the CGRect for the width of your frame, but rather use self.view.size.width for the width (e.g. in case you're in landscape orientation or if Apple ever introduces an iPhone with a different screen size);
You probably want to use autoresizingMask for both the label and the view that holds the label, so that they'll resize as the screen orientation changes, or make sure you invoke [self.tableview reloadData] on orientation changes; and
This is obviously a single line label ... if that works for you great, otherwise you'd want to use sizeWithFont:constrainedToSize:lineBreakMode to determine the height, both for creating the label/view as well as responding to tableView:heightForHeaderInSection:.
You also need to add the textColor:
label.textColor = [UIColor colorWithRed:0.265 green:0.294 blue:0.367 alpha:1.000];

Resources