Multiline Text On UIsegment Control - iphone

I have a UISegmentControl with default style (White). I want to add text on it. But the text that i want to put on it is a long text.
I have to show the text in 2 lines of a segment. But i dont have to raise the width of the segment Because of screen width limit & no of segments.
I had tried to put a label on segment control programmatically, but my label is not displayed. Although we can put a label on segment control using XIB. but due to dynamic nature of text & segment control, I have to draw the segment control programmatically & also put the text on it.
Guidance will be appreciated.

Hi friend segment controller already have the label as the subview, so this code is helpful to achieve the multiline text to segment control
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;
}
}
}
Have a Good day

Related

Aligning label relative to another dynamically created label

I am writing a code that dynamically creates labels and realigns the labels relative to the last label created. I am using the following code to create and resize the height of the label to fit the content.
// Create label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.text = SomeVariatingTextContent;
[self.scrollView addSubview:label];
// Resize Label
UIFont* font = label.font;
CGSize constraintSize = CGSizeMake(label.frame.size.width, MAXFLOAT);
CGSize labelSize = [label.text sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
label.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y, 280, labelSize.height);
How do find the identifier to the previous label such that I can realign the the next label being created such that it is always 8 points below the previous label created?
Try to use CGRectGetMinY etc. (look here: CGGeometry Reference to get the edges of previous UILabel, for example:
CGFloat *yPos = CGRectGetMinY(previousLabelRect);
nextLabel.frame = CGRectMake(x,yPos+8,width,height);
EDIT
I can assume that the last UILabel is the last one you added to the view so you can:
UILabel lastLabel = [[self.view subviews]lastObject];
Br aware that if you have other subviews added later you need to filter the array to get the UILabels only. if the order is the problem you can get all the labels and check their y position to get the last one.
An other way is too store save a pointer to the UILabel at the end of the loop that creates the labels, and update that pointer every time a new label is created.

Horizontal scrolling text?

Can anyone provide any tips when it comes to implementing some text that scrolls horizontally?
Right now I have a ScrollView *scrollView with a UILabel *textLabel inside of it.
I have the position of the label changing within the view until it is outside the bounds of the screen. The label is then reset to its original position and it starts scrolling again.
The problem(s) I am having are when I say: [textLabel sizeToFit];
This takes away the labels ability to handle multiple lines of text because once I say [textLabel sizeToFit]; it changes it into one big string.
Is there a simpler way to achieve the desired effect?
ANY help is greatly appreciated
P.S. The text inside of the label will be parsed from a website... so the size/length of the string will not be consistant.
try this
CGSize maximumLabelSize = CGSizeMake(widthOfLabel, 9999);
CGSize expectedLabelSize = [#"Text" sizeWithFont:[UIFont fontWithName:#"Helvetica" size:14] constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeWordWrap];
then you can use expectedLabelSize.height or expectedLabelSize.width and change the frame size of UILabel

Finding the number of lines in UITextView

I'm using UIScrollView in which I have placed a UIImageView and a UITextView. I make the UIScrollView to scroll both the images and text and it works fine, but my UITextView contains dynamic text (i.e number of lines is different for each time). So I can't find the way to assign contentSize of UITextView. Is there any way to do this?
This and this might help you.
You can put a condition that if width of the new CGSize is greater than textview width then number of lines = 2 else 1.
calculate the text width and define your text view width using below code
+(float) calculateHeightOfTextFromWidth:(NSString *) text: (UIFont *)withFont: (float)width
:(UILineBreakMode)lineBreakMode
{
[text retain];
[withFont retain];
CGSize suggestedSize = [text sizeWithFont:withFont constrainedToSize:CGSizeMake(215, 1000) lineBreakMode:lineBreakMode];
[text release];
[withFont release];
return suggestedSize.height;
}
and use when you want to display dynamic text as
float titleHeight;
titleHeight = [Your view controllre ViewController calculateHeightOfTextFromWidth:[NSString stringWithFormat:#"%#",[dict objectForKey:#"title"]] :[UIFont systemFontOfSize:14]:300 :UILineBreakModeTailTruncation];
give this titleHeight to your textview or do calculation dividation using titleheight ,you can get the number of lines
If you get problem then reply

Add tableview in scrollview with table scrolling disabled in Iphone

I have scroll view with label and a table view and a button . I just want to scroll the scrollview and the table view must display all the contents but tableview must not scroll. Below the tableview i have a button. How to set the frame of the button so it comes exactly below the table?
Thanks
Maybe you would like to set the YourTableView.userInteractionEnabled = NO?
Yes we can disable the scrolling the tableview.
Goto->xib->select table->Goto 1st tab->unselect the scrolling Enabled.
The answer for your Second Question.
Put the UiView in footer of your table and then place the button in that UIView you want to show in bottom.
It will always show at the bottom.
If you want to place button programmatically use following code in viewDidload method.
///--------Table Footer is Set here
UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 260, 44)];
UIButton *adddays = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 260, 44)];
[adddays setBackgroundImage:[UIImage imageNamed:#"abcd.png"] forState:UIControlStateNormal];
[adddays addTarget:self action:#selector(buttonaction) forControlEvents:UIControlEventTouchDown];
UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(75, 12, 250, 20)];
[text setBackgroundColor:CLEAR_COLOR];
[text setText:#"Title for your button"];
[text setTextColor:XDARK_BLUE];
text.font=[UIFont fontWithName:#"Arial-BoldMT" size:18.0f];
[footer addSubview:adddays];
[footer addSubview:text];
[table setTableFooterView:footer];
This is assuming you have created IBOutlets for your scrollView, tableView and button, and hooked them up appropriately.
I find it useful to remember that we're only messing with the y-values of a CGRect (origin.y & size.height) - The x-values should be set up in the xib.
I've commented this profusely to illustrate my point better, usually I would only comment where appropriate
-(void)viewDidLoad {
[self.tableView setScrollEnabled:NO];
// Get the number of rows in your table, I use the method
// 'tableView:numberOfRowsInSection:' because I only have one section.
int numOfRows = [self.tableView numberOfRowsInSection:0];
// Get the height of your rows. You can use the magic
// number 46 (44 without including the separator
// between rows) for the height of your rows, but because
// I was using a custom cell, I had to declare an instance
// of that cell and exctract the height from
// cell.frame.size.height (adding +2 to compensate for
// the separator). But for the purpose of this demonstration
// I'm going to stick with a magic number
int rowHeight = 46; //Eww, Magic numbers! :/
// Get a reference to the tableViews frame, and set the height
// of this frame to be the sum of all your rows
CGRect frame = self.tableView.frame;
frame.size.height = numOfRows * rowHeight;
// Now we have a frame with the exact size of our table,
// so set the 'tableView.frame' AND the 'tableView.contentSize'
// to that. (Because we want ALL rows visible as you
// disabled scrolling for the 'tableView')
self.tableView.frame = frame;
self.tableView.contentSize = frame.size;
// Now we want to set up the button beneath the table.
// We still have the 'frame' variable, which gives us
// the tableView's Y-origin and height. We just add these
// two together (with +20 for padding) to get the origin of the button
CGRect buttonFrame = self.button.frame;
buttonFrame.origin.y = frame.origin.y + frame.size.height + 20;
self.button.frame = buttonFrame;
// Finally, we want the `scrollView`'s `contentSize` to
// encompass this entire setup (+20 for padding again)
CGRect scrollFrame = self.scrollView.frame;
scrollFrame.size.height = buttonFrame.origin.y + buttonFrame.size.height = 20;
self.scrollView.contentSize = scrollFrame.size;
}
You could stop the scrolling the table view. But you shouldn't be adding a tableview inside a scrollview. UITableView is subclass of UIScrollView and adding one scrollView on another will create problem. I suggest you to remove the scrollview and use the tableview alone ( as the tableview itself is a scrollview).

Draw rectangle in custom table cell

How would I draw a rectangle in a custom table cell class? The cell currently has a background image with a few text labels. I would like to draw a rectangle behind each of the labels so they are easier to read over the detailed background image.
I know I could just set the background colour of the label but I would like to have padding between the background colour and the text. If that is possible, I'd love to know how! :)
I'm subclassing a TTTableMessageItemCell in Three20, a method below gets called in which you can play with subviews of the cell,
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat padding = 16;
CGFloat boxWidth = self.contentView.width - 2*padding;
CGFloat textWidth = boxWidth - (padding*2);
CGFloat textHeight = 100;
CGFloat top = kTableCellSmallMargin;
// Position Heading Text
_titleLabel.frame = CGRectMake(padding, top, textWidth, _titleLabel.font.ttLineHeight);
top += _titleLabel.height;
// Position Detail Text
[self.detailTextLabel sizeToFit];
self.detailTextLabel.top = top+2*padding;
self.detailTextLabel.left = 2*padding;
self.detailTextLabel.width = textWidth;
self.detailTextLabel.height = 100;
}
I would like the rectangles to be placed behind the _titleLable and detailTextLabel labels.
edit
I have been able to add the right box using the following,
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor whiteColor];
view.frame = CGRectMake(padding, top, textWidth, textHeight+2*padding);
[self insertSubview:view belowSubview:self.detailTextLabel];
It is laying on top of the label and I cant seem to get it behind it...
edit
I was adding the view to the wrong subview, fixed it with,
[[self.subviews objectAtIndex:0] insertSubview:view atIndex:0];
You can add the labels to views and these to the cell.
You could use insertSubview:belowSubview: to add views behind your labels. With backgroundColor and the right frame they will do what you intend to.
You can also bring detailLabel to front