2011-02-08 1 views

Répondre

3

Il n'y a pas de soulignement simple pour UILabel. Donc, il y a deux approches:

  1. Utilisation UIWebView où vous pouvez souligner le texte dont vous avez besoin avec <span style="text-decoration:underline">underlined html text<span>.
  2. Personnalisé UILabel - c'est une bonne idée pour les étiquettes courtes à une seule ligne. Vous devez créer une classe CUILabel qui hérite UILabel et de le remplacer est la méthode drawRect dans @implementation section avec le code suivant:

`

- (void)drawRect:(CGRect)rect { 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextSetRGBStrokeColor(ctx, 0.0f/255.0f, 0.0f/255.0f, 255.0f/255.0f, 1.0f); // Your underline color 
    CGContextSetLineWidth(ctx, 1.0f); 

    UIFont *font = [UIFont systemFontOfSize:16.0f]; 
    CGSize constraintSize = CGSizeMake(MAXFLOAT, MAXFLOAT); 
    CGSize labelSize; 
    labelSize = [self.text sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 

    CGContextMoveToPoint(ctx, 0, self.bounds.size.height - 1); 
    CGContextAddLineToPoint(ctx, labelSize.width + 10, self.bounds.size.height - 1); 

    CGContextStrokePath(ctx); 

    [super drawRect:rect]; 
} 
1

En plus de la réponse de NR4TR une autre option serait d'utiliser CoreText.

0

j'ai changé le code de NR4TR pour être plus automatique:

#import "UILabelUnderlined.h" 

@implementation UILabelUnderlined 

@synthesize underlineWidth; 

- (id)initWithFrame:(CGRect)frame 
{ 
self = [super initWithFrame:frame]; 
if (self) { 
    self.underlineWidth = 1.0f; 
} 
return self; 
} 

- (void)drawRect:(CGRect)rect { 
CGContextRef ctx = UIGraphicsGetCurrentContext(); 

CGFloat red; 
CGFloat green; 
CGFloat blue; 
CGFloat alpha; 
[self.textColor getRed:&red green:&green blue:&blue alpha:&alpha]; 

CGContextSetRGBStrokeColor(ctx, red, green, blue, 1.0f); // Your underline color 
CGContextSetLineWidth(ctx, self.underlineWidth); 

CGSize constraintSize = CGSizeMake(MAXFLOAT, MAXFLOAT); 
CGSize labelSize; 
labelSize = [self.text sizeWithFont:self.font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 

CGContextMoveToPoint(ctx, 10, self.bounds.size.height - 1); 
CGContextAddLineToPoint(ctx, labelSize.width + 10, self.bounds.size.height - 1); 

CGContextStrokePath(ctx); 

[super drawRect:rect]; 
} 

@end 
+0

La valeur alpha de la couleur du texte a-t-elle été supprimée? –

0

vous pouvez utiliser WebView & coche "liens" case à cocher Détection & donner texte html

htmlContentTxt = @"<a title="XYZ" href="http://www.XYZ.com">www.XYZ.com</a>"; 
    [webview loadHTMLString:htmlContentTxt baseURL:nil]; 
Questions connexes