iOS UILabel 改变文字方向

作者: zwwuchn | 来源:发表于2019-07-31 17:24 被阅读2次

    本文介绍利用BezierPath, 通过CoreText重新绘制文字的方向.

    具体操作 简单介绍如下

    文字显示的方向

    typedef NS_ENUM(NSUInteger, TransformLayerDirectionType) {
        TransformLayerDirectionUp = 0, //文字方向朝上(默认正常)
        TransformLayerDirectionLeft,//文字方向朝左
        TransformLayerDirectionDown,//文字方向朝下
        TransformLayerDirectionRight//文字方向朝右
    };
    

    根据传进来的属性, 设置Path的属性

    CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef)font.fontName, font.pointSize, NULL);
        
        if (!ctFont) return nil;
        
        NSDictionary *attrs = @{ (__bridge id)kCTFontAttributeName:(__bridge id)ctFont };
        
        NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:text attributes:attrs];
    

    获取行数组

    CFArrayRef runs = CTLineGetGlyphRuns(line);
    

    循环遍历 绘制单行 确定位置

    for (CFIndex iRun = 0, iRunMax = CFArrayGetCount(runs); iRun < iRunMax; iRun++) {
            
            CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runs, iRun);
            
            CTFontRef runFont = CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName);
            
            for (CFIndex iGlyph = 0, iGlyphMax = CTRunGetGlyphCount(run); iGlyph < iGlyphMax; iGlyph++) {
                
                CFRange glyphRange = CFRangeMake(iGlyph, 1);
                
                CGGlyph glyph;
                
                CGPoint position;
                
                CTRunGetGlyphs(run, glyphRange, &glyph);
                
                CTRunGetPositions(run, glyphRange, &position);
                
                CGPathRef glyphPath = CTFontCreatePathForGlyph(runFont, glyph, NULL);
             
            }
        }
    

    旋转每行中单独的字

    if (glyphPath) {
                    
                    CGAffineTransform transform1 = CGAffineTransformMakeRotation(M_PI*textDirection*0.5);
                    
                    CGAffineTransform transform2 =  CGAffineTransformMakeTranslation(position.x, position.y);
                    
                    CGAffineTransform transform = CGAffineTransformConcat(transform1, transform2);
                    
                    CGPathAddPath(cgPath, &transform, glyphPath);
                    
                    CGPathRelease(glyphPath);
                }
    

    根据路径创建layer 设置属性

     // 创建文字路径
        UIBezierPath *path;
        path = [UIBezierPath bezierPathWithText:self font:aFont andWithDirection:directionStr];
        
        // 创建路径图层
        CAShapeLayer *pathLayer = [CAShapeLayer layer];
        pathLayer.frame = aRect;
        pathLayer.bounds = CGPathGetBoundingBox(path.CGPath);
        pathLayer.geometryFlipped = NO;
        pathLayer.path = path.CGPath;
        pathLayer.strokeColor = [aColor CGColor];
        pathLayer.fillColor = [aColor CGColor];
        pathLayer.lineWidth = 1.0f;
        pathLayer.lineJoin = kCALineJoinBevel;
        [aView.layer addSublayer:pathLayer];
    

    调用

    _shapeLayer = [_contentLabel.text animateOnView:_contentLabel atRect:_contentLabel.bounds forFont:_contentLabel.font withColor:_contentLabel.textColor andDuration:0.0 andWithIsNeedAnimation:false andDirection:TransformLayerDirectionUp];
        [_contentLabel.layer addSublayer:_shapeLayer];
    

    效果如下

    • 文字方向朝上(默认状态)


      文字方向朝上.png
    • 文字方向朝下


      文字方向朝下.png
    • 文字方向朝左


      文字方向朝左.png
    • 文字方向朝右
    文字方向朝右.png

    代码路径:代码传送门

    相关文章

      网友评论

        本文标题:iOS UILabel 改变文字方向

        本文链接:https://www.haomeiwen.com/subject/buxtdctx.html