开发者

Strike-through font in objective C

How can I use strike-through font in objective C??? More specifically in UITableVie开发者_StackOverflow中文版wCell

cell.textLabel.text = name;
cell.detailTextLabel.text = quantity ;
cell.XXX = ??


this is Marin, the author of the attributed strings chapter in "iOS6 by Tutorials".

Since iOS6 there is actually a native support for a bunch of different text attributes, including strike-trough.

Here's a short example, which you can use for your cell text label:

NSDictionary* attributes = @{
  NSStrikethroughStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleSingle]
};

NSAttributedString* attrText = [[NSAttributedString alloc] initWithString:@"My Text" attributes:attributes];
cell.textLabel.attributedText = attrText;

That's all. Good luck!


EDIT: This answer is out of date as of iOS 6. Please see the more recent answers below

There is not native support for strike-through or underline fonts. You have to draw the lines yourself over the views for the labels.

This is silly since the font inspector for IB has options to set strike-through and underline, but these are promptly ignored if you try to set them.


CGRect frame = sender.titleLabel.frame;
UILabel *strikethrough = [[UILabel alloc] initWithFrame:frame];
strikethrough.opaque = YES;
strikethrough.backgroundColor = [UIColor clearColor];
strikethrough.text = @"------------------------------------------------";
strikethrough.lineBreakMode = UILineBreakModeClip;
[sender addSubview:strikethrough];


here is how you strikethrough your label. But remember, it only works after ios 6.0

NSNumber *strikeSize = [NSNumber numberWithInt:2]; 

NSDictionary *strikeThroughAttribute = [NSDictionary dictionaryWithObject:strikeSize 
forKey:NSStrikethroughStyleAttributeName];

NSAttributedString* strikeThroughText = [[NSAttributedString alloc] initWithString:@"Striking through it" attributes:strikeThroughAttribute];

strikeThroughLabel.attributedText = strikeThroughText;


1-Get the size of text which needs to strikethrough

CGSize expectedLabelSize = [string sizeWithFont:cell.titleLabel.font constrainedToSize:cell.titleLabel.frame.size lineBreakMode:UILineBreakModeClip];

2-Create an line and add it to the text

UIView *viewUnderline = [[UIView alloc] init];
viewUnderline.frame = CGRectMake(20, 12, expectedLabelSize.width, 1);
viewUnderline.backgroundColor = [UIColor grayColor];
[cell addSubview:viewUnderline];
[viewUnderline release]; 


Strikethrough is possible by using NSStrikeThroughAttributes and attributedText of UILabel. Here is the solution in Swift

    let strikeThroughAttributes = [NSStrikethroughStyleAttributeName : 1]
    let strikeThroughString = NSAttributedString(string: "Text of Label", attributes: strikeThroughAttributes)
    strikeThroughLabel.attributedText = strikeThroughString


NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"Your String here"];
[attributeString addAttribute:NSStrikethroughStyleAttributeName
                    value:@2
                    range:NSMakeRange(0, [attributeString length])];
yourLabel.attributedText = attributeString;


Have you thought about finding your own strikethrough font and loading it yourself? It isn't that hard, just add the UIFont to your Info.plist file and put the name of the font in there. Then you can manually set the text to that new strikethrough font.

See this post on loading custom font.

Can I embed a custom font...


UIView *strikeView = [[UIView alloc] initWithFrame:ccr(0, 0, myLabel.bounds.size.width, 1)];
strikeView.backgroundColor = [UIColor redColor];
strikeView.center = ccp(myLabel.bounds.size.width/2, myLabel.bounds.size.height/2);
[myLabel addSubview:strikeView];


This will strike through the whole cell. In case you need it to look like completed task:

CGSize size = cell.contentView.frame.size; // you'll draw the line in whole cell.

UIView *line = [[UIView alloc] initWithFrame:CGRectMake(15,size.height / 2,size.width - 30, 1)];
line.backgroundColor = [UIColor grayColor]; // set your preferred color
[cell addSubview:line];

You may indicate some other value in CGRectMake instead of 15 - it's offset by X. In this case you'd better then decrease width by doubled value (in my case it's 15*2 = 30) in order it look nice.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜