Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为UIlabel加下划线,使下划线始终达到边界_Ios_Swift_Uilabel - Fatal编程技术网

Ios 为UIlabel加下划线,使下划线始终达到边界

Ios 为UIlabel加下划线,使下划线始终达到边界,ios,swift,uilabel,Ios,Swift,Uilabel,我想要实现的是在UILabel上加下划线,但要以特定的方式 我知道如何使UILabel带下划线,但由于这将是一个动态文本,我不知道它需要多长时间 每当标签进入一个新行,我想让下划线与上面的对齐,而不管文本长度 我画了一个草图,让大家更好地了解我实际想要实现的目标: 你的看法是什么,如何处理这样的问题 我是否应该在文本跳转到另一行时将白线添加为UIView 或者,当文本长度小于当前行的边界时,可能会在代码中添加一些空格?我提出了自定义标签类的解决方案,并在UIlabel的自定义类中重写drawR

我想要实现的是在
UILabel
上加下划线,但要以特定的方式

我知道如何使
UILabel
带下划线,但由于这将是一个动态文本,我不知道它需要多长时间

每当标签进入一个新行,我想让下划线与上面的对齐,而不管文本长度

我画了一个草图,让大家更好地了解我实际想要实现的目标:

你的看法是什么,如何处理这样的问题

我是否应该在文本跳转到另一行时将白线添加为
UIView

或者,当文本长度小于当前行的边界时,可能会在代码中添加一些空格?

我提出了自定义标签类的解决方案,并在UIlabel的自定义类中重写drawRect方法

CustomLabel.h

#import <UIKit/UIKit.h>

@interface CustomLabel : UILabel

@end
在您的类中,只需导入此自定义类

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

首先需要为标签设置文本,然后调用此方法:

- (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;
}

您的意思是要在文本前加下划线?请使用NSMutableAttributedString。浏览此链接属性字符串将仅在我的文本下划线,不会与左边界对齐,如果我错误,请纠正我,因为您必须创建自定义行view@Anilsolanki你能提供一些示例代码吗?我可以要求快速实现吗?这不起作用,它在最后一个字符处加下划线,直到行尾不起作用,只需在UILabelcheck out屏幕截图周围绘制矩形,并使用ObjectC代码。我可以发送邮政编码
- (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;
}
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
}