tags:

views:

115

answers:

2

Hi All, I need to strike through the text of a multi-line label. Is there a way to do it? Any suggestion would be greatly helpful. Thanks,

A: 

UILabel has both strikethrough & underline feature. See via IB -> UILabel-> attributes -> Font -> Top first 2 buttons are about it.

mihirpmehta
quetion tagged "iPhone" - but you can't set strikethrough font attribute for iPhone.
Vladimir
A: 

if you want do it with UILabel for iPhone you can't :(

so there are 3 ways:

  1. (simplest) use 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. use unicode combining diacritic (this works with any objects labels, textfields etc.)

    "long stroke overlay" (U+0336) or
    "combining low line" (U+0332) before
    each charecter in your string. Use

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

    to create unichar array from string (allocate double size of string length), then rearrange array and add U+0336 or U+0332 before each character, then convert unichar array back to NSString with

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

    but in most cases this looks bad

  3. Draw it manualy on context.

Vladimir