2009-11-18 100 views

回答

3

不能做到标准。在UITextField中只允许一种样式。

最好的主意:摆脱UITextField中的$,用空白替换它,然后在上面放置一个UILabel。

第二个好主意:子类UITextField。

编辑:

UITextField有一个drawTextInRect:方法,它可能是所有你需要重写。 已记录here。如果您需要更多帮助,请尝试使用Google或如何继承不同的UI类,以便搜索,并获得如何重写绘画方法的感觉(如drawRect:UIView

+0

好点肯尼!虽然,我会选择继承:) – prakash 2009-11-18 10:18:16

+0

我subclassed UITextField。在我的drawTextInRect方法中,我添加了一个字符串在文本字段中绘制。但不调用drawTextInRect。 – diana 2009-11-18 10:45:44

0

这里是我的LinethruLabel的@implementation的一个子类的UILabel。它表示一个 “划伤” 项目与红色的内衬通过文本。在我的情况下,我打电话[super drawRect:rect];和UILabel绘制文字和背景等,但我不知道是否你想要,所以如果它不按你想要的方式工作,就把它拿出来。所以我想你需要使用NSString UIKit类别的drawAtPoint:withFont:方法的一些变体。

@implementation LineThruUILabel 

- (void)dealloc { 
    [super dealloc]; 
} 


- (id)initWithFrame:(CGRect)frame { 
    if (self = [super initWithFrame:frame]) { 
     // Initialization code 
     drawLine = NO; 
    } 
    return self; 
} 


- (void) setDrawLine:(BOOL) newValue 
{ 
    if(drawLine != newValue){ 
     drawLine = newValue;   
     [self setNeedsDisplay]; 
    } 
} 


- (CGSize) calculateLineWidth 
{ 
    NSString *newText = self.text; 
    CGFloat actualFontSize; 
    CGFloat width = self.frame.size.width; 
    CGSize size = [newText sizeWithFont: self.font minFontSize:self.minimumFontSize 
          actualFontSize:&actualFontSize 
           forWidth:width 
          lineBreakMode:self.lineBreakMode ]; 
    return size;  
} 

- (void)drawRect:(CGRect)rect { 

    [super drawRect:rect]; 

    CGFloat horizOffset = 0.0; 

    if(drawLine){ 

     CGSize lineSize = [self calculateLineWidth]; 

     CGFloat offset = 1.0; 

     // Drawing code 
     CGContextRef ctx = UIGraphicsGetCurrentContext(); 
     //CGFloat w = rect.size.width; 
     CGFloat h = rect.size.height; 

     // just match the color and size of the horizontal line 
     CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0); 
     CGContextSetLineWidth(ctx, 2.0); 

     CGContextMoveToPoint(ctx, horizOffset , h/2 + offset); 
     CGContextAddLineToPoint(ctx, lineSize.width + horizOffset + 1.0, h/2 + offset); 

     CGContextStrokePath(ctx); 
    } 


} 
相关问题