How do I create a horizontal line with text in the middle? - objective-c

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.

Related

UILabel remove bottom margin

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/

sizeWithFont:constrainedToSize doesn't work with custom font

I'm trying to resize a UITextView to the size the text within it.
The problem is that Im using a custom font and it the text doesnt fit within the UITextView.
NSString *textToFit = #"pretty long text";
UIFont *customFont = [UIFont fontWithName:#"Museo-100" size:15];
CGSize sizeText = [textToFit sizeWithFont:customFont constrainedToSize:CGSizeMake(textFrame.size.width, 1000)];
Where textFrame is the frame of the UITextView I want to adjust its height.
Im trying different fonts and also different files of the same font and still it never adjusts its height to the height that the text fills.
I've been searching and I dont find a solution. I've tried a workaround using a UILabel and the method textRectForBounds, but still no success, something on this lines.
UILabel *auxLabel = [[UILabel alloc]init];
auxLabel.numberOfLines = 0;
auxLabel.font = [UIFont fontWithName:#"Museo-100" size:15];
auxLabel.text = //THE TEXT I WANT TO FIT IN
CGRect textSize = CGRectMake(0.0, 0.0, textDescription.frame.size.width, FLT_MAX);
CGRect frame = [auxLabel textRectForBounds:textSize limitedToNumberOfLines:0];
I think
UIView : sizeToFit
Should solve your problem.
sizeToFit Resizes and moves the receiver view so it just encloses its
subviews.
Discussion: Call this method when you want to resize the current view so that it uses the most appropriate amount of space.
Specific UIKit views resize themselves according to their own internal
needs. In some cases, if a view does not have a superview, it may size
itself to the screen bounds. Thus, if you want a given view to size
itself to its parent view, you should add it to the parent view before
calling this method.
https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/uiview/uiview.html#//apple_ref/occ/instm/UIView/sizeToFit

Vertical line in front of text in a UILabel. Like blockquotes

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];

reproduce UITableViewCellSeparatorStyleSingleLineEtched in UITableViewStylePlain

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];

How to mask a UILabel in iOS - Objective-C

I want to sub-class a UIView and place four UILabels over top one another ; top label will be the MASK, 2nd label will be a normal label with text, the 3rd label is a label with solid background with no text. the bottom label will the same as the top 2nd label with a different color font. when i sent the width of the third label it will cover up the bottom label showing a partial view of the text. I want to have the 2nd text be one color while the uncoverd bottom label display another color font.
Is this possibe? If someone can explain how to mask in objective-C that will help too.
I trying to build a UIView that acts like a progress bar, as the bar fill to 60%, I want to top text to show in white font color, when the bottom text shows in a different color.
You could do it with two UILabels, one on the bottom, and one embedded in another view on top.
UILabel *bottomLabel = ...;
[self.view addSubview:bottomLabel];
UIView *topContainer = [[UIView alloc] initWithFrame:bottomLabel.frame];
topContainer.clipsToBounds = YES;
topContainer.opaque = NO;
topContainer.backgroundColor = [UIColor clearColor];
UILabel *topLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, bottomLabel.frame.size.width, bottomLabel.frame.size.height)];
topLabel.text = bottomLabel.text;
topLabel.opaque = NO;
topLabel.backgroundColor = [UIColor clearColor];
[topContainer addSubview:topLabel];
[self.view addSubview:topContainer];
Then, when you want to change the progress, you'd set the width of topContainer. This should clip topLabel.
Rather than using four UILabels, why not subclass UILabel and draw it yourself in the drawRect: method? It would look something like:
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
// Set the mask
CGContextClipToMask(context, self.bounds, /* mask image */);
// Draw the text in a different font
[self.text drawInRect:rect withFont:/* alternate font */];
// Draw a solid background
CGContextSetRGBFillColor(context, ...);
CGContextFillRect(context, rect);
// Draw the text normally
[super drawRect:rect];
}
You could make the masking image and the alternate font properties of your subclass, for convenience.