-1

What I want to achieve is to have UILabel underlined but in a specific way.

I know how to make UILabel underlined, but since this is going to be a dynamic text, I don't know how long it will be.

Anytime the label enters a new line, I'd like to make the underlining align with the one above regardless of the text length.

I sketched it up to give you a better notion of what I actually try to achieve:

enter image description here

What is your opinion, how to approach such problem?

Should I add the white line as UIView anytime text skips to another line?

Or maybe add some whitespace in code when the text lengths is shorter than bounds of current line?

Nilesh
  • 701
  • 5
  • 14
theDC
  • 6,364
  • 10
  • 56
  • 98

3 Answers3

0

enter image description hereI have came up with solution with custom label class and override drawRect Method in that custom class of UIlabel.

CustomLabel.h

#import <UIKit/UIKit.h>

@interface CustomLabel : UILabel

@end

CustomLabel.m

#import "CustomLabel.h"

@implementation CustomLabel

- (void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(ctx, 1.0f);
    float Left = self.center.x - self.frame.size.width/2.0;
    float Right = self.center.x + self.frame.size.width/2.0;
    CGContextMoveToPoint(ctx, Left, self.bounds.size.height - 1);
    CGContextAddLineToPoint(ctx, Right, self.bounds.size.height - 1);
    CGContextStrokePath(ctx);
    [super drawRect:rect];
}

@end

In Your Class Just import this custom Class.

 #import "CustomLabel.h"

 ////// you can create labels now which are having underline to bounds.

-(void)CreateCustomLabel
{
    CustomLabel *custom = [[CustomLabel alloc] initWithFrame:CGRectMake(0, 150, SCREEN_WIDTH-40, 50)];
    custom.text = @"Your Text Here";
    custom.backgroundColor = [UIColor redColor];
    [self.view addSubview:custom];
}
Anil solanki
  • 962
  • 4
  • 20
0

first you need to set text for you label then call this method :

- (void)underlineLabel:(UILabel*)lbl {

if (![lbl respondsToSelector:@selector(setAttributedText:)]) {
    return;
}
NSMutableAttributedString *attributedText;
if (!lbl.attributedText) {
    attributedText = [[NSMutableAttributedString alloc] initWithString:lbl.text];
} else {
    attributedText = [[NSMutableAttributedString alloc] initWithAttributedString:lbl.attributedText];
}
long len = [lbl.text length];

    [attributedText addAttribute:NSUnderlineColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0,len)];


[attributedText addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:1] range:NSMakeRange(0, len)];//Underline color

lbl.attributedText = attributedText;
}
Shubham bairagi
  • 943
  • 7
  • 25
0
func underlineLabel(label: UILabel) {

    if !lbl.respondsToSelector("setAttributedText:") {
        return
    }

    var attributedText = NSMutableAttributedString()


    if !(lbl.attributedText != nil) {
        attributedText = NSMutableAttributedString(string:label.text!)
    }

    else {
        attributedText = NSMutableAttributedString(attributedString: label.attributedText!)
    }

    let str = label.text;

    let len = str?.characters.count;


    attributedText.addAttribute(NSUnderlineColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, len!))

    attributedText.addAttribute(NSUnderlineStyleAttributeName , value:1, range: NSMakeRange(0, len!))
    //Underline color
    lbl.attributedText = attributedText
}
Shubham bairagi
  • 943
  • 7
  • 25