UITableViewStyleGrouped default header view? - uitableview

I'm trying to implement a control for deleting entire sections, and it would look best in my app if the delete button was in the header, as opposed to an overlay like a UIPopoverView.
In the process of writing this question, I found the answer. Easy enough, once there's a starting point.

I got the bulk of the code from this blog which has only two posts, both from 2010.
Then I went back to this site just for the font color, since it's more trouble to break apart.
Three minor problems, all with the label.
- Font is too narrow
- Text color is too dark
- Label origin is wrong
The default font is known, so that comes first.
label.font = [UIFont boldSystemFontOfSize:17.0];
Color is next, since that's easy. Used an image editor's Eyedropper tool for this.
label.textColor = [UIColor colorWithRed:0.298 green:0.337 blue:0.423 alpha:1];
// Is there a difference between alpha:1 and alpha:1.000?
Then the hard part. A close guess, and then some tweaking for a perfect match.
label.frame = CGRectMake(54, 4, headerView.frame.size.width-20, 22);
And now we have a custom implementation that perfectly matches the current Grouped header.
Finished code:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)];
tableView.sectionHeaderHeight = headerView.frame.size.height;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(54, 4, labelSize.width, labelSize.height)];
[label setBackgroundColor:[UIColor clearColor]];
[label setFont:[UIFont boldSystemFontOfSize:17.0]];
[label setShadowColor:[UIColor whiteColor]];
[label setShadowOffset:CGSizeMake(0, 1)];
[label setText:[self tableView:tableView titleForHeaderInSection:section]];
[label setTextColor:[UIColor colorWithRed:0.298 green:0.337 blue:0.423 alpha:1.000]];
[headerView addSubview:label];
return headerView;
}
Found this SO answer after finding the right font/color myself. Oh well.
Edit:
For a title label that allows an effectively unlimited amount of text:
// before label init
NSString *title = [self tableView:tableView titleForHeaderInSection:section];
NSUInteger maxWidth = headerView.frame.size.width-108;
CGSize labelSize = [title sizeWithFont:[UIFont systemFontOfSize:17.0]
constrainedToSize:CGSizeMake(maxWidth, CGFLOAT_MAX)];
if (labelSize.width < maxWidth) labelSize.width = maxWidth;
// after setFont:
[label setNumberOfLines:0];

Related

Is it possible to have a main title, subtitle, and a UITableViewStyleValue1 style title in a UITableView

Currently my tableView has a primary title and a two line subtitle using this:
cell.textLabel.text = [shiftLength objectAtIndex:indexPath.row];
cell.detailTextLabel.numberOfLines = 2;
cell.detailTextLabel.text = allDetail;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
How do you text on the right side of the cell in place of the accessory type?
I tried placing a label in the accessory view but nothing shows
UILabel *yourLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 20)];
yourLabel.text = #"TEXT:";
[yourLabel setTextColor:[UIColor blackColor]];
[yourLabel setBackgroundColor:[UIColor clearColor]];
[yourLabel setFont:[UIFont fontWithName: #"Trebuchet MS" size: 14.0f]];
[cell.accessoryView addSubview:yourLabel];
You could add a UILabel as the cell's accessoryView. Or you could just make a custom cell.
if (cell == nil) {
UILabel *yourLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 20)];
[yourLabel setTextColor:[UIColor blackColor]];
[yourLabel setBackgroundColor:[UIColor clearColor]];
[yourLabel setFont:[UIFont fontWithName: #"Trebuchet MS" size: 14.0f]];
cell.accessoryView = yourLabel;
}
((UILabel *)cell.accessoryView).text = #"TEXT:";
cell.textLabel.text = [shiftLength objectAtIndex:indexPath.row];
cell.detailTextLabel.numberOfLines = 2;
cell.detailTextLabel.text = allDetail;
I suggest you to use custom cell and make it accordingly to your requirement, instead of using default cell with its limited styles and different limitation.
Default cell only helpful, when you have to achieve some kind of minimal functionality.
Here is the good tutorial for making custom cell.
Hope that helps.

iOS Multiline Label NSLineBreakByTruncatingTail

I have a label with numberOfLines set to 2, lineBreakMode set to NSLineBreakByTruncatingTail. I was expecting that when the text extends beyond two lines, the label will truncate it with ellipses. But what is happening is that although the text is being truncated no ellipses are getting shown.
Why would this be? Thanks.
More details:
Following is the custom class for the UITableViewCell that contains the label (contentLabel) in question. The labels are updated in the loadConversation method.
#interface CPActivityStreamCell ()
#property(nonatomic, weak) CardDeck *cardDeck;
#property(nonatomic, weak) UIImageView *icon;
#end
#implementation CPActivityStreamCell
#synthesize contentLabel, eventLabel, avatarView;
- (void)awakeFromNib {
[super awakeFromNib];
self.backgroundColor = [UIColor clearColor];
// Initialize views
[contentLabel setLineBreakMode:NSLineBreakByTruncatingTail];
[contentLabel setNumberOfLines:2];
[contentLabel setTextAlignment:NSTextAlignmentLeft];
[contentLabel setTextColor:[UIColor colorWithWhite:0.55 alpha:1.0]];
contentLabel.backgroundColor = [UIColor clearColor];
[eventLabel setLineBreakMode:NSLineBreakByTruncatingTail];
[eventLabel setNumberOfLines:1];
[eventLabel setTextAlignment:NSTextAlignmentLeft];
[eventLabel setTextColor:[UIColor colorWithWhite:0.55 alpha:1.0]];
eventLabel.backgroundColor = [UIColor clearColor];
eventLabel.font = [UIFont fontWithName:#"Antonio-Regular" size:12];
// Create the icon view
UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(20.0, 15.0, 16.0, 16.0)];
[self addSubview:icon];
self.icon = icon;
CardDeck *deck = [[CardDeck alloc] initWithFrame:self.contentView.frame numberOfCards:1];
self.backgroundView = deck;
self.cardDeck = deck;
}
- (void)loadConversation:(CPConversation *)conversation {
[self loadAvatars:conversation.avatarUrls];
self.contentLabel.textColor = [UIColor blackColor];
self.eventLabel.text = [conversation.eventTitle uppercaseString];
self.eventLabel.textColor = [UIColor colorWithWhite:0.55 alpha:1.0];
self.contentLabel.text = conversation.content;
[self loadEventTypeIcon:conversation.eventType white:NO];
[self updateDeck:conversation withFrame:self.contentView.frame];
if ([conversation.typeString isEqualToString:#"image"]) {
[self loadConversationImage:conversation];
}
}
//other methods
#end
The same label in IB:
Output:
The top cell's contentLabel is set to Start of a really long text to test truncation and display in activity stream. It should be truncated with ellipses. End.:
The problem lies somewhere in the architecture of your initialization procedure, or possibly in something you are doing elsewhere (in code you have not shown in your question). You are saying
[contentLabel setLineBreakMode:NSLineBreakByTruncatingTail];
[contentLabel setNumberOfLines:2];
[contentLabel setTextAlignment:NSTextAlignmentLeft];
[contentLabel setTextColor:[UIColor colorWithWhite:0.55 alpha:1.0]];
contentLabel.backgroundColor = [UIColor clearColor];
But this code is not being applied to the label we see in the screen shot. That much is clear, because if it were being applied, the text would be a light gray color.
Thus we may conclude that this code is not running or, if it is running, contentLabel is not hooked up property (perhaps it is nil) and the label is unaffected by it.
Finally, I may point out that on my machine I simply ran your code as given, directly, on a UILabel (not in a table view or anything), and it worked:
[contentLabel setLineBreakMode:NSLineBreakByTruncatingTail];
[contentLabel setNumberOfLines:2];
[contentLabel setTextAlignment:NSTextAlignmentLeft];
[contentLabel setTextColor:[UIColor colorWithWhite:0.55 alpha:1.0]];
contentLabel.backgroundColor = [UIColor clearColor];
contentLabel.text = #"Start of a really long text to test truncation and display in activity stream. It should be truncated with ellipses. End.";
Thus I suggest that either your code is not running or not being applied to the label, or that some other code is coming along and changing the line break mode for this label. It is always best to convince oneself that something works generally for an extremely simple case (like mine, in the preceding code) and then try to figure out why particular results seem to differ from that...

iOS sizeToFit not showing text

I'm creating these labels for a Storyboard view. I have autolayout set to OFF and I'm setting the numberoflines to 0 but the text is only displayed if I comment out sizeToFit. Then, of course, the text gets cut off when its height is greater than 40.
- (void)viewDidLoad
{
[super viewDidLoad];
first = #"This text fits in the label";
second = #"This large text is too large for the label but because the words are normal sized, shows the more button correctly at the bottom right";
third = #"Doesn't show the more button correctly because WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW";
firstlbl = [[UILabel alloc]initWithFrame:CGRectMake(13, 57, 295, 40)];
secondlbl = [[UILabel alloc]initWithFrame:CGRectMake(13, 152, 295, 40)];
thirdlbl = [[UILabel alloc]initWithFrame:CGRectMake(13, 225, 295, 40)];
[firstlbl setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:14]];
[secondlbl setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:14]];
[thirdlbl setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:14]];
firstlbl.lineBreakMode = NSLineBreakByWordWrapping;
secondlbl.lineBreakMode = NSLineBreakByWordWrapping;
thirdlbl.lineBreakMode = NSLineBreakByWordWrapping;
firstlbl.numberOfLines = 0;
secondlbl.numberOfLines = 0;
thirdlbl.numberOfLines = 0;
[firstlbl sizeToFit];
[secondlbl sizeToFit];
[thirdlbl sizeToFit];
[firstlbl setText:first];
[secondlbl setText:second];
[thirdlbl setText:third];
[self.view addSubview:firstlbl];
[self.view addSubview:secondlbl];
[self.view addSubview:thirdlbl];
}
You're calling sizeToFit before you set the text, so it's sizing down to CGRectZero. Switch those calls and it'll work.
sizeToFit doesn't mean "automatically adjust your size to fit your content," it means "update your size right now to fit your content." Subsequent updates to the text won't change the frame.
If you want the former behavior, so that it automatically sizes itself to its content, autolayout is probably the easiest road to take.

How add Multiple Lines in Same UILabel

Any way to have multiple lines of text in UILabel ?
I dont wish to more than 1 label in the view.
How to add multiple lines in a single UILabel??
Yes there is a way. Just you need to add two property of UILabel i.e.
NumberOfLines=0 It'll allow you to add multiple lines in a UILabel
LineBreakMode = NSLineBreakByWordWrapping It'll allow you to break your sentence by word. You can also change it according to your requirement.
[YourLabel setNumberOfLines:0];
[YourLabel setLineBreakMode:NSLineBreakByWordWrapping];
You can also set this two property form your interface builder
here is a sample code
UILabel *pHolder1 = [[UILabel alloc]initWithFrame:CGRectMake(5, 0, 245, 45)];
pHolder1.backgroundColor = [UIColor clearColor];
pHolder1.font = [UIFont systemFontOfSize:14.0f];
pHolder1.numberOfLines =0;
pHolder1.lineBreakMode = NSLineBreakByWordWrapping;
pHolder1.textAlignment = NSTextAlignmentCenter;
Dynamically calculate the height of UILabel
please refer the below post
Adjust UILabel height depending on the text
Here is sample code:
UILabel *lblUsername=[[UILabel alloc] init];
StoryTextSize = [storytext sizeWithFont:[UIFont fontWithName:#"Georgia" size:13.0f] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
lblUsername.frame=CGRectMake(20, 5, [[UIScreen mainScreen] bounds].size.width-40, StoryTextSize.height);
lblUsername.textColor=[UIColor blackColor];
lblUsername.text=[NSString stringWithFormat:#"%#",[[tblRecords objectAtIndex:indexPath.row] valueForKey:#"username"]];
lblStoryText.numberOfLines=nooflines;
lblStoryText.backgroundColor=[UIColor clearColor];
[self.view addSubview:lblStoryText];
make sure that your label height should be more so total no of lines become visible.

How to have a page with long text that is wrapped automatically based on the phone screen

I am trying to make a book application. I am stucked with the text that can show long string. Basically, I tried to use UI Label, but it is only showing one line and the remaining of the text is cut.
Any idea what kind of UI that I should use to make a book? Thanks.
Set the numberOfLines for the UILabel to 0, to let it use as many lines as it needs.
You have to use UITextView for such purpose check
UITextView *aboutStable = [[UITextView alloc] init];
[aboutStable setFont:[UIFont fontWithName:#"Helvetica" size:12]];
[aboutStable setText:#"Write your long text here............iphonemaclover is great"];
[aboutStable setTextColor:[UIColor grayColor]];
[aboutStable setBackgroundColor:[UIColor clearColor]];
[aboutStable setTextAlignment:UITextAlignmentCenter];
[aboutStable setFrame:CGRectMake(0, 302, 320, 79)];
[self addSubview:aboutStable];
And make it not editable so set
[aboutStable setUserInteractionEnabled:NO]
should do it
and if you need scrolling:
aboutStable.editable = NO;
or if you text is still more,then you can set scrollview in it too...
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 195, 280, 10)];
textView.text = #"your long text here";
textView.font = [UIFont fontWithName:#"Hevlatica" size:14];
[self.scrollView addSubview:textView];//add scrollview using Xib and set property of it.
CGRect descriptionFrame = textView.frame;
descriptionFrame.size.height = textView.contentSize.height;
textView.frame = descriptionFrame;
or you can check more detail here in this sample code
EDIT FOR DIFFERENT RETINA DISPLAY
CGRect screenRect = [[UIScreen mainScreen] bounds];
if (screenRect.size.height == 568.0f)
{
//condition for iphone 5 screen
}
else
{
//condition for retina display 3.5
}

Resources