【问题标题】:Underline UIlabel so the underlining always reaches the bounds下划线 UIlabel 所以下划线总是到达边界
【发布时间】:2015-12-09 10:16:58
【问题描述】:

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

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

每当标签进入新行时,无论文本长度如何,我都想让下划线与上面的下划线对齐。

我把它画出来是为了让你更好地了解我实际尝试实现的目标:

你的意见是什么,如何解决这个问题?

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

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

【问题讨论】:

  • 你的意思是你想在你的文本之前加下划线?
  • 使用 NSMutableAttributedString。通过此链接stackoverflow.com/questions/13122207/…
  • 属性字符串将仅在我的文本下划线,并且不会与左边界对齐,如果我错了,请纠正我
  • 为此您必须创建自定义线视图
  • @Anilsolanki 你能提供一些示例代码吗?

标签: ios swift uilabel


【解决方案1】:

首先你需要为你的标签设置文本然后调用这个方法:

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

【讨论】:

  • 我可以要求快速实施吗?
【解决方案2】:
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
}

【讨论】:

  • 这不起作用,它下划线直到最后一个字符,而不是直到行尾
【解决方案3】:

我想出了自定义标签类的解决方案,并在 UIlabel 的自定义类中覆盖 drawRect 方法。

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

在你的类中只需导入这个自定义类。

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

【讨论】:

  • 不起作用,只是在 UILabel 周围绘制矩形
  • 查看带有客观 c 代码的屏幕截图。 .我可以发送 zip 的代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-25
  • 2015-03-19
  • 2011-08-21
  • 1970-01-01
  • 1970-01-01
  • 2013-08-22
  • 2016-02-19
相关资源
最近更新 更多