zoukankan      html  css  js  c++  java
  • CoreText实现图文混排

    1. 第一步,引入CoreText.framework框架。
    2. //CoreText  跨平台
    3. 以lable为例
    4.  
    5. NSDictionary *attributes = @{
    6.          NSFontAttributeName: [UIFont systemFontOfSize:14],
    7.          NSForegroundColorAttributeName: [UIColor blackColor],
    8.     };
    9. NSMutableAttributedString *attrtext = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];

    10.//设置文本属性

    1. [attrtext addAttribute:NSUnderlineStyleAttributeName
    2. value:@2
    3. range:NSMakeRange(0, 3)];

    15.//Foundation  ---> CoreFoundation

    1. NSString *s = @"tt";
    2. CFStringRef s2 = (__bridge CFStringRef)s;

    18.//1.通过NSMutableAttributedString 文本属性对象  构造--->CTFramesetterRef

    1. CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrtext);

    20.//2.构造CGPath

    1. CGMutablePathRef path = CGPathCreateMutable();

    22.//    CGRectInset(<#CGRect rect#>, <#CGFloat dx#>, <#CGFloat dy#>)

    1. CGPathAddRect(path, NULL, self.bounds);

    24.//3.通过CTFramesetterRef和CGPath 构造出一个CTFrameRef

    1. CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);

    26.//4.取得绘制的上下文

    1. CGContextRef ctx = UIGraphicsGetCurrentContext();
    2. //坐标系统向下平移当前视图的高度
    3. CGContextTranslateCTM(ctx, 0, CGRectGetHeight(self.bounds));
    4. //缩放倍数为-1,使用坐标指向反方向
    5. CGContextScaleCTM(ctx, 1, -1.0);

    32.//5.绘制frame

    1. CTFrameDraw(frame, ctx);
    2. //取得frmae中所有的行
    3. CFArrayRef lines = CTFrameGetLines(frame);
    4. //绘制一行

    38.//    CTLineDraw(<#CTLineRef line#>, <#CGContextRef context#>)

    1. CTLineRef line;
    2. CFArrayRef runs = CTLineGetGlyphRuns(line);

    42.//    CTRunDraw(<#CTRunRef run#>, <#CGContextRef context#>, <#CFRange range#>)

    45.常用开源框架

    46.FTCoreText

    47.DTCoreText

  • 相关阅读:
    WPF 登录窗口关闭时打开主窗口
    WPF Expander获得ToggleButton
    .NET Framework 4 与 .NET Framework 4 Client Profile
    WPF 根据枚举值名称 获得枚举值
    WPF KeyDown不响应方向键、Home/End/PgUp/PgDn等功能键
    C# MemoryStream和BinaryFormatter
    VB INET控件的全部用法
    C#写文件方法总结
    C#实现ADSL拨号(源代码例程)
    使用C#实现ADSL自动拨号(原理及封闭类)
  • 原文地址:https://www.cnblogs.com/SilverWinter/p/SilverWinter_CoreText.html
Copyright © 2011-2022 走看看