I'm building a small app and wan't to display quotes in a UITableView. I'm using custom UITableViewCells with a bunch of UILabels, one for the headline and one for quote and more labels for more information.
I want to display the quote with a vertical line on the left side in front of the text. Like some blockquotes. Here is an example from Reeder-App, it does it exactly the way I want it.
Is there a simple way to do this, maybe with attributed strings? A hint would be awesome, because I haven't got an idea to solve it.
Get the textheight like:
CGSize textHeight = [textLabel.text sizeWithFont:textLabel.font constrainedToSize:CGSizeMake(textLabel.frame.size.width, 99999999)];
Then draw the line like:
UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 5, textHeight.height)];
lineView.backgroundColor = [UIColor grayColor];
[self.view addSubview:lineView];
Related
I'm very new to Objective C but I'm looking at the source code of an existing app so there's a lot here to sift through.
I'm trying to create something like this:
-------------- or --------------
...only with a solid horizontal line instead of the dashes.
I have the "or" text defined as this:
_orLabel = ^{
UILabel* label = [[UILabel alloc] init];
label.translatesAutoresizingMaskIntoConstraints = NO;
label.textColor = [UIColor blackColor];
label.font = [UIFont mainFontWithSize:[UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline].pointSize];
label.adjustsFontSizeToFitWidth = YES;
label.textAlignment = NSTextAlignmentCenter;
label.text = NSLocalizedString(#"or", nil);
[view addSubview:label];
return label;
}();
That's working great to get "or" to show up but I have no idea how to get the horizontal lines on either side.
I would subclass UILabel and override drawTextInRect:. The easiest way is to call super so that the text gets drawn. Now you are still in a graphics context (CGContext) so you can use ordinary Quartz drawing commands to draw your horizontal lines.
The simplest approach is to make use of certain Unicode characters that will give you a solid line.
label.text = #"───────── or ─────────";
That text is using a series of "BOX DRAWINGS LIGHT HORIZONTAL" characters (U+2500).
I would use IB to add a view on each side of the label. Make the views have a height of 1 or 2 with a black background, and use autolayout to make them size appropriately for the label and screen situation:
Make view #1 anchored to the left edge of the screen and the right side of the label.
Make view #2 anchored to the right edge of the screen and the left side of the label.
Make the 2 views and the label have the same vertical center.
I am having a trouble with a UILabel.
I am trying to have the UILabel so that there is no margin at all in the container.
I tried different things, like sizeToFit, boundsToRect and others, but never got the solution so far.
Here is my code:
UILabel* saveLabel = [[UILabel alloc] init];
saveLabel = [[UILabel alloc] init];
saveLabel.text = "USER SAVE";
saveLabel.textAlignment = NSTextAlignmentRight;
saveLabel.layer.borderWidth = 2;
[saveLabel sizeToFit];
When I add this to my view I would expect that the border at the bottom (from the border) touch the letter of the labels, but there is a space in between.
I have attached a picture of the bottom effect if you want to see.
http://oi62.tinypic.com/n62b0w.jpg
I really cant understand why this margin is there and how to get rid of it.
All the other margin, top, left and right are fine, just the bottom one.
Any help on this would be very appreciated,
Thanks
The frame includes space for lower case letters like g and y which 'descend' below the text's baseline. In your case, you're only using uppercase letters, so you may wish to remove the descender portion of the frame. You can access the height of the descender via saveLabel.font.descender and then subtract that from the height of the fitted frame.
[saveLabel sizeToFit];
saveLabel.frame = CGRectMake(saveLabel.frame.origin.x
saveLabel.frame.origin.y,
saveLabel.frame.size.width,
saveLabel.frame.size.height - saveLabel.font.descender);
The following article has a good diagram:
https://www.cocoanetics.com/2010/02/understanding-uifont/
I am trying to use the solution on the following page:
UITextView highlightedTextColor or similar option?
However, I am finding that when I call setTextColor on my UITextView and set the color to anything other than Color blackColor, the content in the UITextView appears shifted, and the color I do set it to doesn't take.
Playing around, I can repeat this behaviour by modifying the following initialization code in my UITableViewCell:
_notesTextView = [[UITextView alloc] initWithFrame:CGRectZero];
[_notesTextView setFont:[UIFont systemFontOfSize:12.0]];
[_notesTextView setTextColor:[UIColor redColor]];
[_notesTextView setUserInteractionEnabled:NO];
[self.contentView addSubview:_notesTextView];
The code above will cause the textView to display offset from what I expect, as compared to when I leave the color defaulted or set to blackColor, and the text doesn't show as red either.
This is so weird - any idea what could be wrong?
Do Not Use CGRectZero as it initiates the frame to some value you might know depending upon situation so if you have created a custom UITableViewCell then just set the frames in init or
-(void)layoutSubViews
{
}
I've confirmed that the textColor does indeed shift the text position (up and down for me). I've also tried it with initWithFrame(50, 20, 250, 31), and still experience the same problem.
What I've discovered fixes this issue is using the property textAlignment and setting it to one of the values: UITextAlignmentCenter, UITextAlignmentLeft, UITextAlignmentRight.
Is it possible to change the color of the divider? If so, how?
I've researched as much as possible here and on Google, without luck. I'm surprised it isn't more common...
Thanks
On iOS 7, the fix is to set the background color of your UISplitViewController to the same as the deep background color (probably black).
There is a quicker and better way of doing it. Just change spliViewController's view backgroundColor property:
splitViewController.view.backgroundColor = [UIColor greenColor];
In the detailViewController of the SplitViewController, I added the following code to cover up the black line. My custom header is blue and 88px tall.
//blue line that covers the vertical black separator in the header
UIView *blueHeaderSplitViewSeparatorMask = [[UIView alloc] initWithFrame:CGRectMake(320, 0, 2, 88)];
[blueHeaderSplitViewSeparatorMask setBounds:CGRectMake(320, 0, 2, 88)];
[blueHeaderSplitViewSeparatorMask setBackgroundColor:[UIColor colorWithRed:0.0f/255.0f green:96.0f/255.0f blue:182.0f/255.0f alpha:1.0f]];
[self.view.superview addSubview:blueHeaderSplitViewSeparatorMask];
You mean the one-point line between the left and right view controllers? No, UISplitViewController doesn’t expose a way to change that. You might try creating an opaque view with the same size as the split view controller’s view and its backgroundColor set to the color you want, then add it below the other two controllers’ views like this:
splitController.viewControllers = [NSArray arrayWithObjects:leftController, rightController, nil];
[splitController.view insertSubview:theBackgroundView atIndex:0];
How can one reproduce the same effect as Single Line Etched when using plain table and custom cells?
I think I need to add them as subview to each cell, excluding the last one. I want to know how to reproduce that without having to use images to that. Does anyone know?
Isn't the separator just a single pixel grey line, even for Single Line Etched?
In this case, create a UIView the width of the cell, but only one pixel high and then set it's background colour,then stick it at the bottom of the cell's content view.
UIView *lineView;
lineView = [[UIView alloc] initWithFrame:CGRectMake(0.0f,
cell.contentView.bounds.size.height-1.0f,
cell.contentView.bounds.size.width,
1.0f);
lineView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
lineView.backgroundColor = [UIColor grayColor];
[cell.contentView addSubview:lineView];