ios怎么实现带删除线的label

1.自己新建一个类名字为StrikeLabel,是UILabel的子类;
2.在StrikeLabel.h里@property(nonatomic)BOOL strikeThroughEnabled;
在StrikeLabel.m里
- (void)drawRect:(CGRect)rect{
[super drawTextInRect:rect];
CGSize textSize = [[self text] sizeWithFont:[self font]];
CGFloat strikeWidth = textSize.width;
CGRect lineRect;
if ([self textAlignment] == UITextAlignmentRight) {
lineRect = CGRectMake(rect.size.width - strikeWidth, rect.size.height/2, strikeWidth, 1);
} else if ([self textAlignment] == UITextAlignmentCenter) {
lineRect = CGRectMake(rect.size.width/2 - strikeWidth/2, rect.size.height/2, strikeWidth, 1);
} else {
lineRect = CGRectMake(0, rect.size.height/2, strikeWidth, 1);
}
if (_strikeThroughEnabled) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextFillRect(context, lineRect);
}}
3.在需要声明label的时候
StrikeLabel *label=[[StrikeLabel alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
[email protected]"wo ai ni ";
label.strikeThroughEnabled=YES;
[self.view addSubview:label];
搞定了

时间: 2024-08-02 20:36:04

ios怎么实现带删除线的label的相关文章

iOS_绘制带删除线的Label

效果图例如以下: 一个带删除线的文本标签,继承自UILabel 自绘代码过程例如以下: 1,重写控件的drawRect方法 2,首先得到上下文对象 3,设置颜色,并指定是填充(Fill)模式还是笔刷(Stroke)模式 4,在上下文中指定删除线的起点(含x,y) 5,依据标签文字的字体,确定文字的长度(即将被画的线条的长度) 6,指定删除线的终点(含x,y) 7,渲染到上下文,完毕路径的绘制

iOS下划线/虚线/删除线等等

1.实线,(下划线/删除线) 写一个新类 UnderLineLabel : UILabel - (void)drawRect:(CGRect)rect { // Drawing code [super drawRect:rect]; CGContextRef ctx = UIGraphicsGetCurrentContext(); CGSize fontSize =[self.text sizeWithFont:self.font forWidth:self.frame.size.width l

使用NSMutableAttributedString添加下划线、删除线、阴影、填充、不同字体颜色等

在iOS开发中,有时会遇到需要添加下划线,或者设置字符串中某几个字的颜色的情况,最常见的栗子就是注册页面,如图所示: 几乎所有注册页面中都会出现这么一句话 "点击下一步表示您已同意<用户服务协议>",而且可以看到,"<用户服务协议>"几个字是橙色的,并且它们下面还有下划线.这是怎么实现的呢? 有的同学可能会说:"不同颜色就设置两个label,让左边的label显示前半句并设置为黑色,让右边的label显示后半句并设置为橙色就行了.&

【iOS知识学习】_iOS Label添加删除线

在做优惠价格的时候需要用到删除线,但是网上的删除线千篇一律,都是大抄小抄,其实苹果的NSAttributedString就可以实现这一点啦. 代码如下: NSString *oldPrice = @"¥ 12345"; NSUInteger length = [oldPrice length]; NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:oldPrice];

iOS Label添加删除线

@import url(/css/cuteeditor.css); 在做优惠价格的时候需要用到删除线,但是网上的删除线都是创建一个类继承自UILabel,然后重写drawRect方法重绘Label,其实苹果的NSAttributedString就可以实现这一点. 代码如下: NSString *lastPrice = @"¥12.25"; NSUInteger length = [lastPrice length]; NSMutableAttributedString *attri =

iOS 为label添加删除线

UILabel *testLabel = [[ UILabel alloc] initWithFrame:CGRectMake(10, 100, 200, 100)]; testLabel.numberOfLines = 0; NSString* strText = @"测试画删除线测试画删除线测试画删除线测试画删除线测试画删除线"; NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initW

iOS &#183; UILabel加删除线

创建自定义子类DeleteLineLabel,继承自UILabel,然后在自定义子类DeleteLineLabel中 方法一(上下文): 1 - (void)drawRect:(CGRect)rect { 2 [super drawRect:rect]; 3 4 CGContextRef ref = UIGraphicsGetCurrentContext(); 5 6 //绘制起点 7 CGContextMoveToPoint(ref, 0, rect.size.height * 0.5); 8

ios UITableView自带划动删除效果

说实话,UITableView cell自带的滑动删除效果,在ios7以前比较丑,但ios扁平化后,这个滑动删除还是非常好看的.而且实现起来也是非常容易的. 实现这个效果主要是在UITableView协议里面实现. 如下: 设置可以编辑 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ return YES; } 删除按钮点击 - (void)tableView:(

iOS中字体样式的设置、颜色、空心、删除线、阴影、斜体、扁平化

不多说直接上代码! 这些知识基本的文字设置,实际上iOS中文字的设置有很多,比如说颜色.空心.删除线.阴影.斜体.扁平化等, NSStrokeWidthAttributeName这个属性所对应的值是一个 NSNumber 对象(小数).该值改变描边宽度(相对于字体size 的百分比).默认为 0,即不改变.正数只改变描边宽度.负数同时改变文字的描边和填充宽度.例如,对于常见的空心字,这个值通常为3.0. 同时设置了空心的两个属性,并且NSStrokeWidthAttributeName属性设置为