2009-11-18 90 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); 
    } 


} 
相關問題