3

Objective C iPhoneでテキストに下線を引く方法は?? UILabel のテキストに下線を引く方法はありますか??

4

4 に答える 4

3

以下のようにメソッドをサブクラスUILabel化してオーバーライドdrawRectします。下線には、ラベルとしてとが含まれますsame text colortext alignment

- (void)drawRect:(CGRect)rect
{
    if([[self.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length])
    {
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        const CGFloat* colors = CGColorGetComponents(self.textColor.CGColor);
        CGContextSetRGBStrokeColor(ctx, colors[0], colors[1], colors[2], 1.0); // RGBA
        CGContextSetLineWidth(ctx, 1.0f);
        CGSize tmpSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(200, 9999)];

        // check text alignment
        if(self.textAlignment == UITextAlignmentLeft) {
            CGContextMoveToPoint(ctx, 0, self.bounds.size.height - 1);
            CGContextAddLineToPoint(ctx, tmpSize.width, self.bounds.size.height - 1);
        }else if(self.textAlignment == UITextAlignmentCenter) {
            CGFloat startPoint = (self.frame.size.width - tmpSize.width) / 2;
            CGContextMoveToPoint(ctx, startPoint, self.bounds.size.height - 1);
            CGContextAddLineToPoint(ctx, tmpSize.width + startPoint, self.bounds.size.height - 1);
        }else if (self.textAlignment == UITextAlignmentRight) {
            CGFloat startPoint = (self.frame.size.width - tmpSize.width);
            CGContextMoveToPoint(ctx, startPoint, self.bounds.size.height - 1);
            CGContextAddLineToPoint(ctx, self.frame.size.width, self.bounds.size.height - 1);
        }

        CGContextStrokePath(ctx);
    }
    [super drawRect:rect];
}
于 2012-08-17T18:50:55.230 に答える
2

メソッドをサブクラス化し、メソッド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];  
}
于 2011-04-25T10:45:04.123 に答える
1

簡単に言えば、UILabel で使用できる高度な書式設定はありません。

探しているのがリンクの場合は、UIWebView を使用して、「My Link」などの「自家製の HTML」をフィードすることをお勧めします。次に、webview のデリゲートで webview のクリックを処理できます。

于 2011-04-25T11:10:04.303 に答える
0

属性文字列を使用します。

NSAttributedString* attrString = [[NSAttributedString 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);

または、オーバーライドする代わりに、Olivier Halligon によって作成されたOHAttributedLabelを使用することをお勧めします。また、カスタム リンクとそれらのカスタム カラーもサポートしています。

于 2012-04-11T13:07:33.447 に答える