2010-04-01 33 views
2

我需要敲擊多行標籤的文字。有沒有辦法做到這一點? 任何建議都會非常有幫助。謝謝,通過文字敲擊

+0

你是一個UILabel? – kennytm 2010-04-01 07:20:17

回答

3

,如果你想用的UILabel爲iPhone做它,你不能:(

所以有3種方式:

  1. (簡單)使用的UIWebView:

    // set html header with styles, you can certainly use some other attributes  
    NSString * htmlWrap = @"<html><head><style>body{text-align:left; background-color:transparent; color:black; font-weight:bold; text-decoration:line-through; font-size:%dpt}`</style></head>`<body>%@</body`></html>"; 
    NSStrring * myText = @"My sample strikethrough text"; 
    webView.backgroundColor = [UIColor clearColor]; 
    [webView setOpaque:NO]; 
    NSString * htmlText = [NSString stringWithFormat:htmlWrap, 12, myText]; 
    [webView loadHTMLString:htmlText baseURL:nil]; 
    
  2. 使用unicode結合變音符號(這適用於任何對象標籤,文本字段等)

    「長劃線覆蓋」(U + 0336)或
    「合併低線」(U + 0332)
    您的字符串中的每個字符。使用

    -(void)getCharacters:(unichar *)buffer range:(NSRange)aRange 
    

    從字符串創建單字符陣列(分配字符串長度的兩倍大小),然後重新排列陣列,並且每個字符之前添加U + 0336或U + 0332,然後將其轉換爲unichar陣列回的NSString與

    -(id)initWithCharacters:(const unichar *)characters length:(NSUInteger)length 
    

    但在大多數情況下,這很糟糕

  3. 繪製manualy上下文。

2

這將適用於單行標籤。

@interface UILabelStrikeThrough : UILabel { 
} 
@end 

@implementation UILabelStrikeThrough 
- (void)drawRect:(CGRect)rect { 
    CGContextRef c = UIGraphicsGetCurrentContext(); 

    CGFloat black[4] = {0.0f, 0.0f, 0.0f, 1.0f}; 
    CGContextSetStrokeColor(c, black); 
    CGContextSetLineWidth(c, 2); 
    CGContextBeginPath(c); 
    CGFloat halfWayUp = (self.bounds.size.height - self.bounds.origin.y)/2.0; 
    CGContextMoveToPoint(c, self.bounds.origin.x, halfWayUp); 
    CGContextAddLineToPoint(c, self.bounds.origin.x + self.bounds.size.width, halfWayUp); 
    CGContextStrokePath(c); 

    [super drawRect:rect]; 
} 

@end 
+0

但是當我們setText被調用時,我用setText試過,但沒有工作。 – sole007 2011-06-10 08:55:59

2

改進了@RefuX代碼來處理刪除線的位置和寬度。 我將很快添加多行支持。

多行現在支持

Gist for UILabelStrikethrough

+0

蘋果不支持更改任何UI控件....它的工作正常,但蘋果拒絕應用程序,如果我們使用此代碼... – Raj 2012-07-18 05:59:47

+0

蘋果爲什麼拒絕?我用這段代碼發佈了一個應用程序。 – 2014-10-31 11:50:18