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
                               lineBreakMode:NSLineBreakByTruncatingTail];
    
    //线颜色,此处颜色和字体一致
    CGContextSetStrokeColorWithColor(ctx, self.textColor.CGColor);  // set as the text's color
   // 0.6f == 线宽
    CGContextSetLineWidth(ctx, 0.6f);
    //起点坐标
    CGPoint leftPoint = CGPointMake(0,
                                    self.frame.size.height/2);
   //终点坐标
    CGPoint rightPoint = CGPointMake(fontSize.width,
                                     self.frame.size.height/2);
    CGContextMoveToPoint(ctx, leftPoint.x, leftPoint.y);
    CGContextAddLineToPoint(ctx, rightPoint.x, rightPoint.y);
    CGContextStrokePath(ctx);
}

在需要的地方用UnderLineLabel初始化生成即可.

2.虚线

首先要导入

#import <QuartzCore/QuartzCore.h>

//虚线
        CAShapeLayer *shapeLayer = [CAShapeLayer layer];
        [shapeLayer setBounds:self.bounds];
        [shapeLayer setPosition:self.center];
        [shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
        // 设置虚线颜色为black
        [shapeLayer setStrokeColor:[[UIColor colorWithRed:223/255.0 green:223/255.0 blue:223/255.0 alpha:1.0f] CGColor]];
        // 3.0f设置虚线的宽度
        [shapeLayer setLineJoin:kCALineJoinRound];
        // 3=线的宽度 1=每条线的间距
        [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:1],nil]];
        // Setup the path
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathMoveToPoint(path, NULL, 100, 67);       //100 ,67 初始点 x,y
        CGPathAddLineToPoint(path, NULL, 310,67);     //67终点x,y
        [shapeLayer setPath:path];
        CGPathRelease(path);
        //添加到容器中
        [[self layer] addSublayer:shapeLayer];

此处 self是一个view

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-03 14:46:23

iOS下划线/虚线/删除线等等的相关文章

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

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

文字排版--字体、字号、颜色、粗体、斜体、下划线、删除线

我们可以使用css样式为网页中的文字设置字体.字号.颜色等样式属性.下面我们来看一个例子,下面代码实现:为网页中的文字设置字体为宋体. body{font-family:"宋体";} 这里注意不要设置不常用的字体,因为如果用户本地电脑上如果没有安装你设置的字体,就会显示浏览器默认的字体.(因为用户是否可以看到你设置的字体样式取决于用户本地电脑上是否安装你设置的字体.)现在一般网页喜欢设置"微软雅黑",如下代码: body{font-family:"Micr

ios下划线变量:为什么变量前要加下划线才有用?

先看一段代码. 复制代码 appdelegate.h @property (weak) IBOutlet NSMatrix *StockType; @property (weak) IBOutlet NSMatrix *market; appdelegate.m NSCell *st=[market selectedCell]; 编译时,总是提示,找不到market变量,但是StockType却没问题. 如果根据系统建议,在market前加上下划线,变成_market却可以正常编译和执行.  但

iOS 为label添加删除线

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

ios 下创建,删除文件夹的方法

NSString *imageDir = [NSString stringWithFormat:@"%@/Caches/%@", NSHomeDirectory(), dirName]; BOOL isDir = NO; NSFileManager *fileManager = [NSFileManager defaultManager]; BOOL existed = [fileManager fileExistsAtPath:imageDir isDirectory:&is

iOS 为label加删除线

NSString *oldPrice = [NSString stringWithFormat:@"原价 %@",_item.previousPrice.stringValue]; NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:oldPrice]; [attr addAttribute:NSStrikethroughStyleAttributeName value:@

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 strikeWidt

css删除线,下划线等

<style> .p1 { text-decoration:overline; //上划线 } .p2 { text-decoration:line-through; //删除线 }  .p3 { text-decoration:underline;  //下划线 } .p4 { text-decoration:blink; } </style> </head>   <body>   <p class="p1">这是文本内容&

【转】Android TextView SpannableStringBuilder 图文混排颜色斜体粗体下划线删除线

spannableStringBuilder 用法详解: SpannableString ss = new SpannableString("红色打电话斜体删除线绿色下划线图片:.");           //用颜色标记文本         ss.setSpan(new ForegroundColorSpan(Color.RED), 0, 2,                   //setSpan时需要指定的 flag,Spanned.SPAN_EXCLUSIVE_EXCLUSIV