标签: core-text

通过 CTFontRef 或 CGFontRef 对象中的字形索引获取 unicode 字符

CTFontRef 提供了很好的方法,例如CTFontGetGlyphsForCharacters将字符映射到字形。我的问题是,是否有任何反转映射的方法?也就是说,我可以通过给定的字形获得字符吗?由于我发现有一个CTFontCopyCharacterSet用于获取所有支持的字符,我认为会有一些不错的解决方案。

objective-c core-text

6
推荐指数
2
解决办法
2968
查看次数

核心文本视图具有黑色背景颜色


我有一个UIView使用CoreText渲染一些文本,一切正常,除了整个视图有黑色背景颜色的事实.我尝试过最基本的解决方案

[self setBackgroundColor:[UIColor clearColor]];
Run Code Online (Sandbox Code Playgroud)

要么

CGFloat components[] = {0.f, 0.f, 0.f, 0.f };
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
CGColorRef transparent = CGColorCreate(rgbColorSpace, components);
CGContextSetFillColorWithColor(context, transparent);
CFRelease(transparent);
Run Code Online (Sandbox Code Playgroud)

但这根本没有帮助.
我只是在这个问题上喋喋不休,因为我不太明白真正的核心文本是如何工作的,这个纯C层如何与所有其他Objective-c相关.这对解决这个问题没什么帮助,所以我问你们大家,你们能不能给我一个解决方案,而且(最重要的是)对它有一个解释.

FIY我也发布了CoreText视图的代码.
这一切都基于两个功能:parseText它建立了CFAttributedString和 drawRect中,是以绘图部分的护理.

-(void) parseText {
    NSLog(@"rendering this text: %@", symbolHTML);
    attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
    CFAttributedStringReplaceString (attrString, CFRangeMake(0, 0), (CFStringRef)symbolHTML);

    CFStringRef fontName = (CFStringRef)@"MetaSerif-Book-Accent";
    CTFontRef myFont = CTFontCreateWithName((CFStringRef)fontName, 18.f, NULL); 
    CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
    CGFloat components[] = { 1.0, 0.3f, 0.3f, 1.f };
    CGColorRef redd = CGColorCreate(rgbColorSpace, components);
    CFAttributedStringSetAttribute(attrString, CFRangeMake(0,0), …
Run Code Online (Sandbox Code Playgroud)

core-graphics objective-c core-text

6
推荐指数
1
解决办法
3876
查看次数

NSAttributedString kCTParagraphStyleSpecifierParagraphSpacing无效

应用kCTParagraphStyleSpecifierParagraphSpacing样式时,它对渲染文本没有视觉效果.其他属性(如行间距和文本对齐)完美地工作.我能做错什么?

CTTextAlignment theAlignment = kCTRightTextAlignment;
CGFloat paragraphSpacingFloat = 150.0;
CGFloat paragraphSpacingBeforeFloat = 150.0;
CGFloat lineSpacing = CTFontGetLeading(baseFont)*5.0;

CFIndex theNumberOfSettings = 4;
CTParagraphStyleSetting theSettings[4] = {
  { kCTParagraphStyleSpecifierParagraphSpacing, sizeof(CGFloat), &paragraphSpacingFloat },
  { kCTParagraphStyleSpecifierParagraphSpacingBefore, sizeof(CGFloat), &paragraphSpacingBeforeFloat },
  { kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment), &theAlignment },
  { kCTParagraphStyleSpecifierLineSpacing, sizeof(CGFloat), &lineSpacing }
};

CTParagraphStyleRef theParagraphRef = CTParagraphStyleCreate(theSettings, theNumberOfSettings);
[attr addAttribute:(id)kCTParagraphStyleAttributeName value:(id)theParagraphRef range:r];
[attr addAttribute:(id)kCTFontAttributeName value:(id)baseFont range:r];
CFRelease(theParagraphRef);
Run Code Online (Sandbox Code Playgroud)

我使用渲染文本

CTFrameSetter frameSetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attr);
CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake([[attr string] length], 0), the_drawing_cgrect, NULL);
CTFrameDraw(frame, context);
Run Code Online (Sandbox Code Playgroud)

objective-c core-text ios

6
推荐指数
1
解决办法
3689
查看次数

CoreText是已弃用的CGContext文本函数的替代品

在iOS 7中,不赞成使用几个CGContext文本绘图函数来支持Core Text:

不推荐使用的CGContext函数

Apple有时会建议使用已弃用的函数的特定替代方法(例如"使用x代替"),但在这种情况下,"x"只是"核心文本",并非完全具体.

所以,如果我只是在-drawRect:,我已经CGContextRef准备好了CGContextShowGlyphsAtPoint(),我准备打电话了,我该怎么做呢?什么是最佳实践,什么是最简单的核心文本替换,不需要重构除该调用之外的任何绘图代码(如果存在这样的事情)?

对于该页面上每个已弃用的文本函数,我们最简单地回答了这个问题的答案的奖励积分 - 我最感兴趣的是CGContextShowGlyphsAtPoint(),但这可以作为其他人寻求替代任何这些函数的良好参考.

core-graphics core-text ios ios7

6
推荐指数
1
解决办法
1623
查看次数

绘制有角度/旋转的多线文本 - 核心文本+核心图形

经过几个博客和论坛后,我没有找到一个合适的解决方案,用于在视图上下文中使用核心文本绘制倾斜/倾斜文本.

所以这是怎么回事.

我有一个视图,它- (void)drawRect:(CGRect)rect被调用来在屏幕上绘制一个字符串(多行或单行文本).

码:

- (void)drawRect:(CGRect)rect
{
    NSString *text = @"This is some text being drawn by CoreText!\nAnd some more text on another line!";

    //Core Text (Create Attributed String)

    UIColor *textColor = [UIColor blackColor];
    CGColorRef color = textColor.CGColor;

    CTFontRef font = CTFontCreateWithName((CFStringRef) @"HelveticaNeue", 20.0, NULL);

    CTTextAlignment theAlignment = kCTTextAlignmentLeft;

    CFIndex theNumberOfSettings = 1;
    CTParagraphStyleSetting theSettings[1] =
    {
        { kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment),
            &theAlignment }
    };

    CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(theSettings, theNumberOfSettings);

    NSDictionary *attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:
                                    CFBridgingRelease(font), (NSString …
Run Code Online (Sandbox Code Playgroud)

iphone core-graphics objective-c core-text ios

6
推荐指数
1
解决办法
3831
查看次数

以最高效的方式在曲线上绘制文本并为其制作动画?

我猜这是用单个CATextLayers制作一个字符串,然后根据需要在曲线上定位它们,然后制作动画.因为那就是我现在的工作,但它失去了科宁.这是如何做:

为什么我的弯曲文字不能居中?

但是核心文本是否更具高效性并且能够避免整个"绘制成上下文"的废话,与精简,平均核心动画的做事方式相比,减慢一切,并尊重字距?即避免使用drawRect:以及所有其他方面都会大大减慢速度,例如以这种方式绘制到屏幕上:

https://github.com/darcyliu/CocoaSampleCode/tree/master/CoreTextArcCocoa

想象一下200个字符的字符串,围绕一个圆圈弯曲,能够动画字符之间的间距,希望稳定在60fps.这可以通过Core Animation实现,但这可以通过将字符串分解为单个字符并将它们以相等的间距放置在圆周围,这会导致字距调整信息完全丢失.

我希望有一种方法可以做到这一点,而不会丢失字距调整信息,仍然能够动态调整60fps的间距.

core-animation kerning core-text ios

6
推荐指数
1
解决办法
3834
查看次数

NSForegroundColorAttributeName不适用于UILabel

我在使用"NSForegroundColorAttributeName"更改子字符串颜色时遇到问题.我很困惑,因为正在应用相同子字符串的其他属性(下划线).

还尝试使用弃用属性"UITextAttributeTextColor"和其他建议属性"kCTForegroundColorAttributeName"并获得相同的效果.

我正在为iOS 7编译.

在此输入图像描述

    NSString *freeText = [NSString stringWithFormat:@"(%@)", self.me.activity.text];

    int lblMaxWidth = arrImgView.origin.x - WPRConstraints.BorderOffset;
    int lblMaxHeight = self.activityView.size.height - WPRConstraints.BorderOffset * 2;

    RevUILabel *addActivityLbl = [[RevUILabel alloc] initWithFontNameMultiLine:[WPRFonts LattoBold]
                                                                          size:16
                                                                 sizeConstrain:CGSizeMake(lblMaxWidth,lblMaxHeight)];

    addActivityLbl.text = [NSString stringWithFormat:@"%@ %@", Translate(self.me.activity.activityKey), freeText] ;
    addActivityLbl.textColor = BlackColor;

    NSMutableAttributedString *str = [addActivityLbl.attributedText mutableCopy];



    [str addAttribute:NSUnderlineColorAttributeName  value:[UIColor redColor] range:[addActivityLbl.text rangeOfString:freeText]];
    [str addAttribute:NSUnderlineStyleAttributeName  value:[NSNumber numberWithInteger:1] range:[addActivityLbl.text rangeOfString:freeText]];

    [str addAttribute:NSForegroundColorAttributeName
                        value:[UIColor redColor]
                range:[addActivityLbl.text rangeOfString:freeText]];


    addActivityLbl.attributedText = str;


    addActivityLbl.frame = CGRectMake(WPRConstraints.BorderOffset,
                                      WPRConstraints.BorderOffset,
                                      addActivityLbl.size.width,
                                      addActivityLbl.size.height);
    [self.activityView addSubview:addActivityLbl];
Run Code Online (Sandbox Code Playgroud)

objective-c nsattributedstring core-text ios

6
推荐指数
2
解决办法
8155
查看次数

NSLayoutManager:如何在仅存在可渲染字形的情况下填充背景颜色

默认布局管理器填充没有文本(最后一行除外)的背景颜色(通过 NSAttributedString .backgroundColor 属性指定)。

在此输入图像描述

我已经通过子类化 NSLayoutManager 并覆盖来实现我想要的效果,func drawBackground(forGlyphRange glyphsToShow: NSRange, at origin: CGPoint)如下所示:

override func drawBackground(forGlyphRange glyphsToShow: NSRange, at origin: CGPoint) {
    guard let textContainer = textContainers.first, let textStorage = textStorage else { fatalError() }

    // This just takes the color of the first character assuming the entire container has the same background color.
    // To support ranges of different colours, you'll need to draw each glyph separately, querying the attributed string for the
    // background color attribute …
Run Code Online (Sandbox Code Playgroud)

nslayoutmanager core-text ios textkit swift

6
推荐指数
1
解决办法
2540
查看次数

iOS:在矩形中的某个点绘制文本

我正在使用核心文本API在UI上绘制文本.(我不知道其他方法,因为大多数Google搜索结果都会引导我使用核心文本api).

我在线阅读了一些关于使用核心文本API的教程.在那些教程中,我逐步看到从矩阵,属性字符串到框架集的配置......但是它们都没有仔细解释每个步骤的含义,所以我不能自己修改.

下面的代码是屏幕上的功能绘制文本,(x,y)是我想要绘制的位置.这段代码一目了然地在屏幕上绘制.不过我不知道在哪里放x和y参数,所以文本将开始在矩形中绘制.

// draw text on screen.
+ (void)drawText:(CGContextRef)context bound:(CGRect)rect text:(NSString *)text x:(float)x y:(float) y color:(UIColor *)color size:(float)textSize{
    CGContextSaveGState(context);
    // always remember to reset the text matrix before drawing.
    // otherwise the result will be unpredictable like using uninitialize memory
    CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0f, -1.0f));
    // Flip the coordinate system. because core Text uses different coordinate system with UI Kit
    CGContextTranslateCTM(context, 0, rect.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    // step 1: prepare attribute string
    NSDictionary *attributes;
    attributes = @{ …
Run Code Online (Sandbox Code Playgroud)

objective-c core-text ios

5
推荐指数
1
解决办法
1万
查看次数

Swift - 游乐场 - 核心图形/核心文本/自定义视图

我正试图在swift playground中创建一个自定义的UIView组件.我已经能够弄清楚除正确渲染文本之外的所有事情.我不确定我是否应该通过Core Graphics或Core Text执行此操作,或者确切地知道如何使其正常工作.

正如您在下图中所看到的,我想在自定义View Component的任意一侧上下颠倒和正面渲染文本

在此输入图像描述

我已经尝试了各种方法来使文本工作,但我一直搁浅.如果有人能够告诉我如何修改我的游乐场以添加一些非常棒的文本渲染.

游乐场代码

// Playground - noun: a place where people can play

import Foundation
import UIKit

class RunwayView : UIView {


    var northText : String?
    var southText : String?


    ///Draws the "runway"
    override func drawRect(rect: CGRect) {

        // Setup graphics context
        var ctx = UIGraphicsGetCurrentContext()


        // clear context
        CGContextClearRect(ctx, rect)
        let parentVieBounds = self.bounds
        let width = CGRectGetWidth(parentVieBounds)
        let height = CGRectGetHeight(parentVieBounds)


        // Setup the heights
        let endZoneHeight : CGFloat = 40


        /* …
Run Code Online (Sandbox Code Playgroud)

xcode core-graphics core-text ios swift

5
推荐指数
1
解决办法
9861
查看次数