Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a horizontal line

I have two labels that are stacked. If I want a horizontal line between them, is there any other way besides using an image with a UIImageView?

like image 885
4thSpace Avatar asked Mar 15 '10 16:03

4thSpace


People also ask

How do you create a horizontal line in HTML?

HTML <hr> Tag. The <hr> tag in HTML stands for horizontal rule and is used to insert a horizontal rule or a thematic break in an HTML page to divide or separate document sections. The <hr> tag is an empty tag, and it does not require an end tag.


3 Answers

Create a UIView with a black background that is 1 pixel high and 320 pixels wide.

like image 115
Jasarien Avatar answered Sep 18 '22 11:09

Jasarien


Use a UIView:

UIView * separator = [[UIView alloc] initWithFrame:CGRectMake(x, y, 320, 1)]; separator.backgroundColor = [UIColor colorWithWhite:0.7 alpha:1]; [self.view addSubview:separator]; [separator release]; 
like image 37
Tom Irving Avatar answered Sep 22 '22 11:09

Tom Irving


While Jasarien's solution is nice and simple, it doesn't create an actual 1 pixel hairline, but a 2 pixel wide line on 2X devices.

I found a blog post on how to create the real 1 pixel thin hairline. We need a utility UIView subclass. For Swift that would be:

import UIKit

class HairlineView: UIView {
    override func awakeFromNib() {
        guard let backgroundColor = self.backgroundColor?.CGColor else { return }
        self.layer.borderColor = backgroundColor
        self.layer.borderWidth = (1.0 / UIScreen.mainScreen().scale) / 2;
        self.backgroundColor = UIColor.clearColor()
    }
}
like image 29
Phantrast Avatar answered Sep 21 '22 11:09

Phantrast