sem*_*mix 82 text underline uikit uilabel ios
如何为可能是多行字符串的文本加下划线?我发现有些人建议使用UIWebView,但对于文本渲染而言,它显然太重了.
我的想法是弄清楚每一行中每个字符串的起点和长度.并在其下划一条线.
我遇到了如何计算字符串的长度和起点的问题.有人可以帮我这个吗?
我尝试使用-[UILabel textRectForBounds:limitedToNumberOfLines:],这应该是文本的绘图边界矩形吧?然后我必须进行对齐?当中心对齐且右对齐时,如何获得每条线的起点?
我是新来的,所以提前谢谢你.
kov*_*pas 135
您可以从UILabel继承并覆盖drawRect方法:
- (void)drawRect:(CGRect)rect {
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(ctx, 207.0f/255.0f, 91.0f/255.0f, 44.0f/255.0f, 1.0f); // RGBA
    CGContextSetLineWidth(ctx, 1.0f);
    CGContextMoveToPoint(ctx, 0, self.bounds.size.height - 1);
    CGContextAddLineToPoint(ctx, self.bounds.size.width, self.bounds.size.height - 1);
    CGContextStrokePath(ctx);
    [super drawRect:rect];  
}
UPD:
 
从iOS 6开始,Apple为UILabel添加了NSAttributedString支持,所以现在它更加容易,适用于多行:
NSDictionary *underlineAttribute = @{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)};
myLabel.attributedText = [[NSAttributedString alloc] initWithString:@"Test string" 
                                                         attributes:underlineAttribute];
如果你仍然希望支持iOS 4和iOS 5,我建议使用TTTAttributedLabel而不是手动下划线标签.但是,如果您需要为一行UILabel加下划线并且不想使用第三方组件,那么上面的代码仍然可以解决问题.
小智 37
这就是我做的.它就像黄油一样.
1)将CoreText.framework添加到您的框架.
2)在需要带下划线标签的类中导入<CoreText/CoreText.h>.
3)编写以下代码.
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:@"My Messages"];
    [attString addAttribute:(NSString*)kCTUnderlineStyleAttributeName
              value:[NSNumber numberWithInt:kCTUnderlineStyleSingle]
              range:(NSRange){0,[attString length]}];
    self.myMsgLBL.attributedText = attString;
    self.myMsgLBL.textColor = [UIColor whiteColor];
小智 35
在Swift中:
let underlineAttriString = NSAttributedString(string: "attriString",
                                          attributes: [NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue])
label.attributedText = underlineAttriString
Pau*_*ira 19
使用属性字符串:
NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:@"Your String"]
[attrString addAttribute:(NSString*)kCTUnderlineStyleAttributeName 
                   value:[NSNumber numberWithInt:kCTUnderlineStyleSingle] 
                   range:(NSRange){0,[attrString length]}];
然后覆盖标签 - (void)drawTextInRect:(CGRect)aRect并以如下方式呈现文本:
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrString);
drawingRect = self.bounds;
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, drawingRect);
textFrame = CTFramesetterCreateFrame(framesetter,CFRangeMake(0,0), path, NULL);
CGPathRelease(path);
CFRelease(framesetter);
CTFrameDraw(textFrame, ctx);
CGContextRestoreGState(ctx);
或者更好的,而不是压倒一切只是使用OHAttributedLabel由Olivier Halligon创建
Gun*_*nds 15
我已经结合了一些提供的答案,以创建更好的(至少对我的要求)UILabel子类,它支持:
https://github.com/GuntisTreulands/UnderLineLabel
kar*_*rim 11
那些不想继承视图(UILabel/UIButton)等的人......'forgetButton'也可以替换为任何标签.
-(void) drawUnderlinedLabel {
    NSString *string = [forgetButton titleForState:UIControlStateNormal];
    CGSize stringSize = [string sizeWithFont:forgetButton.titleLabel.font];
    CGRect buttonFrame = forgetButton.frame;
    CGRect labelFrame = CGRectMake(buttonFrame.origin.x + buttonFrame.size.width - stringSize.width, 
            buttonFrame.origin.y + stringSize.height + 1 , 
            stringSize.width, 2);
    UILabel *lineLabel = [[UILabel alloc] initWithFrame:labelFrame];
    lineLabel.backgroundColor = [UIColor blackColor];
    //[forgetButton addSubview:lineLabel];
    [self.view addSubview:lineLabel];
}
小智 8
NSString *tem =self.detailCustomerCRMCaseLabel.text;
if (tem != nil && ![tem isEqualToString:@""]) {
    NSMutableAttributedString *temString=[[NSMutableAttributedString alloc]initWithString:tem];
    [temString addAttribute:NSUnderlineStyleAttributeName
                      value:[NSNumber numberWithInt:1]
                      range:(NSRange){0,[temString length]}];
    self.detailCustomerCRMCaseLabel.attributedText = temString;
}
另一个解决方案可能是(因为iOS 7)给出了负值NSBaselineOffsetAttributeName,例如你的NSAttributedString可能是:
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:@"my text goes here'
                                                            attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Helvetica-Regular" size:12],
                                                                         NSForegroundColorAttributeName: [UIColor blackColor],
                                                                         NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle), NSBaselineOffsetAttributeName: @(-3)}];
希望这会有所帮助;-)
NSMutableAttributedString *text = [self.myUILabel.attributedText mutableCopy];
[text addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(0, text.length)];
self.myUILabel.attributedText = text;
| 归档时间: | 
 | 
| 查看次数: | 118539 次 | 
| 最近记录: |